C# 从ViewModle打开xaml窗口并等待DialogResult的最佳方式是什么?
目前,我正在使用如下 在xaml中C# 从ViewModle打开xaml窗口并等待DialogResult的最佳方式是什么?,c#,wpf,c#-4.0,binding,C#,Wpf,C# 4.0,Binding,目前,我正在使用如下 在xaml中 <Button Content="X" Width="33" Height="16" Padding="1,-2,1,0" Command="{Binding ElementName=UserControlName, Path=DataContext.DenyCommand}" <Button.CommandParameter> <wpfext:UICommandParameter UICommandCallerC
<Button Content="X" Width="33" Height="16" Padding="1,-2,1,0"
Command="{Binding ElementName=UserControlName, Path=DataContext.DenyCommand}"
<Button.CommandParameter>
<wpfext:UICommandParameter UICommandCallerCallback="{Binding ElementName=UserControlName, Path=UIDenyCallBackCommand}"/>
</Button.CommandParameter>
</Button>
在ViewModel.cs中
internal ViewModel()
{
this.DenyCommand= new DenyCommand(this.AccessDeny);
}
public void AccessDeny(ICommandState commandState)
{
commandState.InvokeCallerCallback("AccessDenied");
}
public CommandCallback DenyCommand
{
get;
private set;
}
UICommandCallerCallback声明如下
public delegate void UICommandCallerCallback(object commandParameter, object callbackData);
public class CommandCallback:ICommand
{
private readonly Action<ICommandState> executeMethod;
private readonly Func<ICommandState, bool> canExecuteMethod;
public CommandCallback(Action<ICommandState> executeMethod)
: this(executeMethod, null)
{
}
public CommandCallback(Action<ICommandState> executeMethod, Func<ICommandState, bool> canExecuteMethod)
{
if (executeMethod == null)
{
throw new ArgumentNullException("executeMethod");
}
this.executeMethod = executeMethod;
this.canExecuteMethod = canExecuteMethod;
}
public bool CanExecute(object parameter)
{
return this.canExecuteMethod != null ? this.canExecuteMethod((ICommandState)parameter) : true;
}
public void Execute(object parameter)
{
if (parameter == null)
{
throw new ArgumentNullException("parameter","CommandCallback parameter cannot be null");
}
if (!(parameter is ICommandState))
{
throw new ArgumentException("expects a parameter of type ICommandState","parameter");
}
ICommandState state = (ICommandState)parameter;
this.executeMethod.Invoke(state);
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
}
CommandCallback类如下所示
public delegate void UICommandCallerCallback(object commandParameter, object callbackData);
public class CommandCallback:ICommand
{
private readonly Action<ICommandState> executeMethod;
private readonly Func<ICommandState, bool> canExecuteMethod;
public CommandCallback(Action<ICommandState> executeMethod)
: this(executeMethod, null)
{
}
public CommandCallback(Action<ICommandState> executeMethod, Func<ICommandState, bool> canExecuteMethod)
{
if (executeMethod == null)
{
throw new ArgumentNullException("executeMethod");
}
this.executeMethod = executeMethod;
this.canExecuteMethod = canExecuteMethod;
}
public bool CanExecute(object parameter)
{
return this.canExecuteMethod != null ? this.canExecuteMethod((ICommandState)parameter) : true;
}
public void Execute(object parameter)
{
if (parameter == null)
{
throw new ArgumentNullException("parameter","CommandCallback parameter cannot be null");
}
if (!(parameter is ICommandState))
{
throw new ArgumentException("expects a parameter of type ICommandState","parameter");
}
ICommandState state = (ICommandState)parameter;
this.executeMethod.Invoke(state);
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
}
完成此工作流程的最佳方法是什么?请告知。谢谢。在中通读用户交互模式。在中通读用户交互模式。为什么要将消息从视图模型发送到视图?如果要在视图模型中显示对话框窗口,该怎么办?这是做这个工作流程最直接的方法。根据我们的项目工作流程,我是不允许做的。如果我使用EventHandler,它就可以像我的工作流一样工作。但是每次卸载usercontrol时,我都需要分离事件,而且速度很慢。而且,我使用winform弹出.sry,我的意思是我使用xaml窗口弹出,而不是win form。为什么要将消息从视图模型发送到视图?如果要在视图模型中显示对话框窗口,该怎么办?这是做这个工作流程最直接的方法。根据我们的项目工作流程,我是不允许做的。如果我使用EventHandler,它就可以像我的工作流一样工作。但是每次卸载usercontrol时,我都需要分离事件,而且速度很慢。而且,我使用winform弹出.sry,我的意思是我使用xaml窗口弹出,而不是win窗体。