WPF:使用命令绑定和线程控制按钮的启用/禁用状态时遇到问题

发布于 2024-09-12 04:11:03 字数 2287 浏览 1 评论 0原文

我有一个 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 代码(我使用 Josh Smith 的 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";
    }
}

I have a WPF app that simply contains a Button and a Textbox to display some output. When the user clicks the Button, a thread starts which disables the Button, prints stuff to the output Textbox, then the thread stops (at which point I want the Button to be enabled again).

The application appears to disable the Button properly, as well as update the Textbox properly. However, it always fails to re-enable the Button properly when the thread completes! Can anyone tell me what I'm doing wrong?

Here's a snippet of my 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>

Here's my ViewModel code (I'm using Josh Smith's MVVM design):

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";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

负佳期 2024-09-19 04:11:03

您需要帮助 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);
    }
}

您可以进一步重构它以使其变得更好,但您明白了。

You'll need to help WPF know that the executable state of the command has changed. The simple way to do this is:

CommandManager.InvalidateRequerySuggested()

inside CanExecuteThread:

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

EDIT (now that I have time): the actual problem is likely that you're not notifying when the CanExecuteThread property changes. It should raise PropertyChanged in order for WPF to detect the change:

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

The above assumes your ViewModel base class has an OnPropertyChanged method.

That said, I also wanted to point out that you could simplify things by simply using a 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);
    }
}

You could refactor this further to be even nicer, but you get the idea.

爱要勇敢去追 2024-09-19 04:11:03

这是肯特·布加特(Kent Boogaart)建议的答案,而且它有效。基本上,我必须通过将 CommandManager.InvalidateRequerySuggested 放置在 Dispatcher 调用调用中来在 UI 线程上调用 CommandManager.InvalidateRequerySuggested。另请注意,我能够删除 CanExecuteThread 属性上的 Set 访问器,因为此解决方案不再需要它。谢谢,肯特!

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();
    }
}

Here is the answer, as suggested by Kent Boogaart, and it works. Basically, I had to call CommandManager.InvalidateRequerySuggested on the UI thread by placing it within a Dispatcher invoke call. Also notice that I was able to get rid of the Set accessor on the CanExecuteThread property, as it was no longer required with this solution. Thank, Kent!

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文