1

这是我当前的 App.xaml.cs

对于一两个人来说,它看起来很简单,但我有 7-8 个窗口。有没有一种聪明的方法可以让它变得更普通和更好?

public App()
{
    _ViewModel = new MyAppViewModel();

    _ViewModel.OpenXXXWindowEvent += new EventHandler(ViewModel_OpenXXXWindow);
    _ViewModel.OpenYYYWindowEvent += new EventHandler(ViewModel_OpenYYYWindow);
    ...
}

private void ViewModel_OpenXXXWindow(object sender, EventArgs e)
{
    _XXXWindow = new XXXWindow();
    _XXXWindow.DataContext = _ViewModel;
    _XXXWindow.ShowDialog();
}

private void ViewModel_CloseXXXWindow(object sender, EventArgs e)
{
    if (_XXXWindow != null)
        _XXXWindow.Close();
}

private void ViewModel_OpenYYYWindow(object sender, EventArgs e)
{
    _YYYWindow = new YYYWindow();
    _YYYWindow.DataContext = _ViewModel;
    _YYYWindow.ShowDialog();
}

private void ViewModel_CloseYYYWindow(object sender, EventArgs e)
{
    if (_YYYWindow != null)
        _YYYWindow.Close();
}

...
4

2 回答 2

2

太多的 XAML 代码隐藏表明您正在以某种方式破坏 MVVM 模式。接收 EventArgs 的 ViewModel 也是禁忌。

要打开/关闭对话框,我倾向于使用消息传递系统,例如MVVM Light提供的那个。

使用消息传递系统(使用 MVVM Light),您可以执行以下操作:

在您的视图模型中:

private void SomeMethodThatNeedsToOpenADialog()
{
    Messenger.Default.Send(new OpenDialogXMessage());
}

在你看来:

Messenger.Default.Register<OpenDialogXMessage>(this, (msg) => {

    new DialogX().ShowDialog();
});

一些相关链接:

于 2013-02-14T16:00:49.070 回答
0

好吧,它不是事件处理程序解决方案,但是Binding解决方案怎么样?不幸的是,当设置值时,您不能 bind Window.DialogResult,这会导致窗口关闭。但是您可以创建一个AttachedProperty可以绑定到底层属性的对象ViewModel,并在设置其值时设置不可绑定的属性。AttachedProperty外观是这样的。

    public class AttachedProperties
    {
        public static readonly DependencyProperty DialogResultProperty =
            DependencyProperty.RegisterAttached("DialogResult", typeof (bool?), typeof (AttachedProperties), new PropertyMetadata(default(bool?), OnDialogResultChanged));

        private static void OnDialogResultChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var wnd = d as Window;
            if (wnd == null)
                return;

            wnd.DialogResult = (bool?) e.NewValue; //here the not bindable property is set and the windows is closed
        }

        public static bool? GetDialogResult(DependencyObject dp)
        {
            if (dp == null) throw new ArgumentNullException("dp");

            return (bool?)dp.GetValue(DialogResultProperty);
        }

        public static void SetDialogResult(DependencyObject dp, object value)
        {
            if (dp == null) throw new ArgumentNullException("dp");

            dp.SetValue(DialogResultProperty, value);
        }
    }

AttachedProperty可以这样使用


<Window x:Class="AC.Frontend.Controls.DialogControl.Dialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:hlp="clr-namespace:AC.Frontend.Helper"
        hlp:AttachedProperties.DialogResult="{Binding DialogResult}">
  <!-- put your content here -->
</Window>

现在您可以使用 aCommand来设置DialogResultVM 的属性,DataContextWindow.

于 2013-02-14T16:09:18.637 回答