WPF: проблема управления включенным / отключенным состоянием кнопки с помощью привязки команд и потока - PullRequest
5 голосов
/ 02 августа 2010

У меня есть приложение WPF, которое просто содержит кнопку и текстовое поле для отображения некоторого вывода. Когда пользователь нажимает кнопку, запускается поток, который отключает кнопку, печатает материал в текстовое поле вывода, затем поток останавливается (в этот момент я хочу, чтобы кнопка снова была включена).

Похоже, что приложение правильно отключает кнопку, а также корректно обновляет текстовое поле. Тем не менее, он всегда не может правильно включить кнопку, когда поток завершается! Может кто-нибудь сказать мне, что я делаю не так?

Вот фрагмент моего xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" HorizontalAlignment="Center" Command="{Binding ExecuteCommand}">E_xecute</Button>
    <Label Grid.Row="1" Content="Output Window:" HorizontalAlignment="Left"/>
    <TextBlock Grid.Row="2" Text="{Binding Output}"/>
</Grid>

Вот мой код ViewModel (я использую дизайн Джоша Смита для MVVM):

public class WindowViewModel : ViewModelBase
{
    private bool _threadStopped;
    private RelayCommand _executeCommand;
    private string _output;

    public WindowViewModel()
    {
        _threadStopped = true;
    }

    public string Output { get { return _output; } set { _output = value; OnPropertyChanged("Output"); } }

    public ICommand ExecuteCommand
    {
        get
        {
            if (_executeCommand == null)
            {
                _executeCommand = new RelayCommand(p => this.ExecuteThread(p), p => this.CanExecuteThread); 
            }
            return _executeCommand;
        }
    }

    public bool CanExecuteThread
    {
        get
        {
            return _threadStopped;
        }
        set
        {
            _threadStopped = value;
        }
    }

    private void ExecuteThread(object p)
    {
        ThreadStart ts = new ThreadStart(ThreadMethod);
        Thread t = new Thread(ts);
        t.Start();
    }

    private void ThreadMethod()
    {
        CanExecuteThread = false;
        Output = string.Empty;
        Output += "Thread Started:  Is the 'Execute' button disabled?\r\n";
        int countdown = 5000;

        while (countdown > 0)
        {
            Output += string.Format("Time remaining: {0}...\r\n", countdown / 1000);
            countdown -= 1000;
            Thread.Sleep(1000);
        }
        CanExecuteThread = true;
        Output += "Thread Stopped:  Is the 'Execute' button enabled?\r\n";
    }
}

Ответы [ 2 ]

1 голос
/ 02 августа 2010

Вам нужно помочь WPF узнать, что состояние исполняемой команды изменилось. Простой способ сделать это:

CommandManager.InvalidateRequerySuggested()

внутри CanExecuteThread:

set
{
    _threadStopped = value;
    CommandManager.InvalidateRequerySuggested()
}

РЕДАКТИРОВАТЬ (теперь, когда у меня есть время): реальная проблема, скорее всего, в том, что вы не уведомляете об изменении свойства CanExecuteThread. Он должен поднять PropertyChanged, чтобы WPF обнаружил изменение:

public bool CanExecuteThread
{
    get { return _threadStopped; }
    set
    {
        if (_threadStopped != value)
        {
            _threadStopped = value;
            this.OnPropertyChanged(() => this.CanExecuteThread);
        }
    }
}

Выше предполагается, что ваш базовый класс ViewModel имеет метод OnPropertyChanged.

Тем не менее, я также хотел отметить, что вы могли бы упростить вещи, просто используя BackgroundWorker:

public class WindowViewModel : ViewModel
{
    private readonly BackgroundWorker backgroundWorker;

    public WindowVieWModel()
    {
        backgroundWorker = new BackgroundWorker();
        backgroundWorker.DoWork += delegate
        {
            // do work here (what's currently in ThreadMethod)
        };
        backgroundWorker.RunWorkerCompleted += delegate
        {
            // this will all run on the UI thread after the work is done
            this.OnPropertyChanged(() => this.CanExecuteThread);
        };
    }

    ...

    public bool CanExecuteThread
    {
        get { !this.backgroundWorker.IsBusy; }
    }

    private void ExecuteThread(object p)
    {
        // this will kick off the work
        this.backgroundWorker.RunWorkerAsync();

        // this property will have changed because the worker is busy
        this.OnPropertyChanged(() => this.CanExecuteThread);
    }
}

Вы можете изменить этот код, чтобы сделать его еще лучше, но вы поняли.

0 голосов
/ 03 августа 2010

Вот ответ, предложенный Кентом Бугаартом, и он работает.По сути, мне пришлось вызвать CommandManager.InvalidateRequerySuggested в потоке пользовательского интерфейса, поместив его в вызов вызова Dispatcher.Также обратите внимание, что мне удалось избавиться от метода доступа Set в свойстве CanExecuteThread, поскольку это больше не требовалось в этом решении.Спасибо, Кент!

public class WindowViewModel : ViewModelBase
{
    private bool _threadStopped;
    private RelayCommand _executeCommand;
    private string _output;
    private Dispatcher _currentDispatcher;
    public WindowViewModel()
    {
        _threadStopped = true;
        _currentDispatcher = Dispatcher.CurrentDispatcher;
    }

    public string Output { get { return _output; } set { _output = value; OnPropertyChanged("Output"); } }

    public ICommand ExecuteCommand
    {
        get
        {
            if (_executeCommand == null)
            {
                _executeCommand = new RelayCommand(p => this.ExecuteThread(p), p => this.CanExecuteThread); 
            }
            return _executeCommand;
        }
    }

    private delegate void ZeroArgDelegate();

    public bool CanExecuteThread
    {
        get
        {
            return _threadStopped;
        }
    }

    private void ExecuteThread(object p)
    {
        ThreadStart ts = new ThreadStart(ThreadMethod);
        Thread t = new Thread(ts);
        t.Start();
    }

    private void ThreadMethod()
    {
        _threadStopped = false;
        Output = string.Empty;
        Output += "Thread Started:  Is the 'Execute' button disabled?\r\n";
        int countdown = 5000;

        while (countdown > 0)
        {
            Output += string.Format("Time remaining: {0}...\r\n", countdown / 1000);
            countdown -= 1000;
            Thread.Sleep(1000);
        }
        _threadStopped = true;
        _currentDispatcher.BeginInvoke(new ZeroArgDelegate(resetButtonState), null);
        Output += "Thread Stopped:  Is the 'Execute' button enabled?\r\n";
    }

    private void resetButtonState()
    {
        CommandManager.InvalidateRequerySuggested();
    }
}
...