从MVVM动作打开一个窗口 - 我做得对吗?

时间:2017-04-25 08:48:14

标签: c# wpf mvvm

我试图找到一种从MVVM应用程序打开对话框的方法,但对建议的方法并不满意。

我最后写了一个行动:

public class OpenWindowAction : TriggerAction<DependencyObject>
{
    public OpenWindowAction()
    {
        ShowDialog = false;
    }

    [AutoDependencyProperty]
    public Type WindowType { get; set; }

    [AutoDependencyProperty]
    public object DataContext { get; set; }

    [AutoDependencyProperty]
    public bool ShowDialog { get; set; }

    protected override void Invoke(object parameter)
    {
        if (WindowType.IsSubclassOf(typeof(Window)))
        {
            var window = (Window)Activator.CreateInstance(WindowType);
            window.DataContext = DataContext;

            if (ShowDialog)
                window.ShowDialog();
            else
                window.Show();
        }
    }
}

用法:

<i:Interaction.Triggers>
  <i:EventTrigger EventName="MouseDoubleClick">
    <x:OpenWindowAction DataContext="{Binding ...}" ShowDialog="True" WindowType="x:EditWindow" />
  </i:EventTrigger>
</i:Interaction.Triggers>

我做得对吗?

1 个答案:

答案 0 :(得分:0)

  

我做得对吗?

嗯,是的。这与Prism处理对话/通知请求的方式非常相似。您可以在此处参考官方示例:difference

...以及此处的var arr = [ { Type: "Identicon", Code: "identicon" }, { Type: "MonsterID", Code: "monsterid" }, ]; var result = arr.some(element => element.Type.includes('Identicon')); console.log(result)课程:https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/25-NotificationRequest/UsingPopupWindowAction

以下博文可能也会有所帮助:https://github.com/PrismLibrary/Prism/blob/b9caefffa7cec59cfea7217cce244437408c87ad/Source/Wpf/Prism.Wpf/Interactivity/PopupWindowAction.cs