2011-05-27 63 views
1

Currenlty,我使用如下。什么是从ViewModle打开xaml窗口并等待DialogResult的最佳方式?

在XAML,

<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> 

在xaml.cs,

public UICommandCallerCallback UIDenyCallBackCommand 
     { 
      get; 
      private set; 
     } 
public UserControlName() 
     { 

      this.UIDenyCallBackCommand = this.UIAccessDenyCallBack; 
      this.InitializeComponent(); 

     } 

public void UIAccessDenyCallBack(object commandParameter, object callbackData) 
     { 
      ShowADenyMsgBox(); 
     } 

private void ShowDenyMsgBox() 
{ 
      RightsDenied win = new RightsDenied(); //xaml window 
      win.Owner = GetImmediateWindow(); 
      win.WindowStartupLocation = WindowStartupLocation.CenterScreen; 
      win.ShowDialog(); 
} 

在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); 

CommandCallback类如下所示。

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

它的正常工作,如果它只是弹出的对话框中,但我想等待对话的结果,并希望继续AccessDeny()函数。例如。

public void AccessDeny(ICommandState commandState) 
      { 
       1. processs 
       2. open xaml window and wait for the dialogresult. (i.e Yes No or Cancel) 
       3. Based on the result, continue processing. 

      } 

什么可能是最好的方式来做这个工作流程?请指教。谢谢。

+0

为什么从视图模型向视图发送消息?如果要在视图模型中显示对话框,该怎么办?这是执行此工作流程的最直接方式。 – vorrtex 2011-05-27 16:19:51

+0

根据我们的项目工作流程,我不能这样做。如果我使用EventHandler,它可以作为我的工作流程。但是,每当usercontrol被卸载时,我需要分离事件并且速度很慢。而且,我正在使用winform来弹出。 – TNA 2011-05-30 01:27:10

+0

sry,我的意思是我用xaml窗口弹出,而不是窗体。 – TNA 2011-05-30 02:20:25

回答

相关问题