WPF暂时禁用用户输入功能 [英] WPF Temporarily disable user input abilities
问题描述
大家好,
我又回来了另一个问题,不过在这个问题上我还没有尝试过。这是因为我真的不知道从哪里开始。
情景
我有一个WPF应用程序基于MVVM原则构建(虽然我认为它不是100%MVVM)。
该应用程序具有以下结构:
Hi Guys,
I'm back again with another question, however on this one I've not actually tried anything yet. This is because I don't really have a clue where to start.
Scenario
I have an WPF application built on MVVM principles (although it is not 100% MVVM I think).
The application has the following structure:
Main Window
->ViewContainer (has a menu system in it that creates buttons based on viewmodel)
-> View A
-> View B
-> View C
问题/要求
查看C上有一个按钮在其底层viewmodel上调用命令,现在问题是这是一个长时间运行的命令,我想要做的是阻止用户进行任何用户交互但保持应用程序能够更新。该进程在单独的线程上运行,因此UI可以更新屏幕日志。但是用户仍然可以单击ViewContainer菜单中的按钮并更改视图。我需要防止这种情况,因为在此过程运行时其他选项不应该运行。
目前我可以禁用交互View C,但尝试adhear到MVVM我不知道如何将这种行为应用到父容器?
任何帮助/指针都将不胜感激。
需要更多信息,请问。
Problem/requirement
View C has a button on it that calls a command on its underlying viewmodel, now the issue is this is a long running command and what I want to do is prevent the user from doing any user interaction but keep the application able to update. The process runs on a separate thread so the UI can update an onscreen log. But the user can still click buttons in the ViewContainer menu and change views. I need to prevent this because the other options shouldn't run while this process is running.
Currently I am able to disable interaction View C, but with trying to adhear to MVVM I'm not sure how I can apply this behavior to parent containers?
Any help/pointers would be appreciated.
Need more info, just ask.
推荐答案
尝试为负责更改视图的按钮定义一个命令。 br />
你应该使用一个委托命令。
Try to define a command for the buttons which are responsible for the change of the view.
You should use a Delegate Command for this.
class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute)
{
_execute = execute;
}
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
它让您有机会定义命令是否可执行或不。
要做到这一点,尝试做这样的事情:
It gives you the opportunity to define whether the command is executable or not.
To do this, try doing something like this:
//
public ICommand SwitchViewCommand { get; set; }
public void SwitchView(object parameter)
{
//Your Logic here.
}
public bool AllowSwitchView(object parameter)
{
//Your Conditions here
}
并且不要忘记实例化你的命令:
And dont forget to instantiate your command:
SwitchViewCommand = new DelegateCommand(SwitchView, AllowSwitchView);
我希望这会对你有所帮助:)
抱歉我的英文不好。
I hope this will help you :)
And sorry for my bad english.
这篇关于WPF暂时禁用用户输入功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!