简体   繁体   English

如何从 WPF 中的 ViewModel 将窗口设置为所有者

[英]How to set a window as owner from ViewModel in WPF

how can I set a window as owner that is declared, initialised and opened from ViewModel?如何将窗口设置为从 ViewModel 声明、初始化和打开的所有者?

Here is the code:这是代码:

public class ViewModel : INotifyPropertyChanged

{
  // declaration
  static nextWindow nw;
  ...

  public ICommand OpenNextWindow { get { return new RelayCommand(OpenNextWindowExecute, CanOpenNextWindowExecute); } }

  bool CanOpenNextWindowExecute(object parameter)
  {
      return true;
  }

  void OpenNextWindowExecute(object parameter)
  {
      nw = new nextWindow();
      nw.WindowStartupLocation = WindowStartupLocation.CenterScreen;
      // Set this window as owner before showing it...
      nw.Show();
  }
 }

In code-behind-file of nextWindow I can set nextWindow as owner with this code:在 nextWindow 的代码隐藏文件中,我可以使用以下代码将 nextWindow 设置为所有者:

nw.Owner = this;

How can I realize it from the viewmodel ?我怎样才能从视图模型中实现它?

To be honest I wouldn't do anything that's related with displaying windows in the ViewModel.老实说,我不会做任何与在 ViewModel 中显示窗口相关的事情。 What you can do though, is send a message (for example using MVVMLight's Messenger service) to the View, where you can do the show and set owner shinanigaz不过,您可以做的是向视图发送消息(例如使用 MVVMLight 的 Messenger 服务),您可以在其中进行表演并设置所有者 shinanigaz

The whole idea behind MVVM is that you want a clean separation between your views and business logic, which enables better maintenance, scalability, testing etc. Your view model should thus ALMOST always be unaware of the existence of any view. MVVM 背后的整个想法是,您希望视图和业务逻辑之间完全分离,从而实现更好的维护、可扩展性、测试等。因此,您的视图模型应该几乎始终不知道任何视图的存在。

Thus make use of a messenger service which views or some view handler can listen to and then decide if it wants to process the message or not.因此,使用视图或某些视图处理程序可以侦听的信使服务,然后决定是否要处理消息。 Thus leaving the viewhandler to decide on which other view to call next or what message box to show.因此让视图处理程序决定接下来调用哪个其他视图或显示哪个消息框。

There are numerous options available but as @Oyiwai said MVVMLight's Messenger service is quick an easy to use.有很多可用的选项,但正如@Oyiwai 所说,MVVMLight 的 Messenger 服务快速且易于使用。

In your Package Manager Console run install-package MvvmLightLibs.在您的包管理器控制台中运行 install-package MvvmLightLibs。 MvvmLightLibs also has an added bonus in the fact that it already has an implementation of INotifyPropertyChanged which I saw you implemented. MvvmLightLibs 还有一个额外的好处,因为它已经有一个 INotifyPropertyChanged 的​​实现,我看到你已经实现了。

Note this is a quick example.请注意,这是一个快速示例。 I don't recommend using the case statement as I did here which hardcodes he view names.我不建议使用 case 语句,因为我在这里对他查看的名称进行了硬编码。

Create a WindowMessage class..创建一个 WindowMessage 类..

public class RaiseWindowMessage
{
    public string WindowName { get; set; }
    public bool ShowAsDialog { get; set; }
}

In your viewmodel在您的视图模型中

public class ViewModel : ViewModelBase
{
    public RelayCommand<string> RaiseWindow => new RelayCommand<string>(raiseWindow, canRaiseWindow);

    private bool canRaiseWindow(string nextWindowName)
    {
        // some logic
        return true;
    }

    private void raiseWindow(string nextWindowName)
    {
        RaiseWindowMessage message = new RaiseWindowMessage();
        message.WindowName = nextWindowName;
        message.ShowAsDialog = true;

        MessengerInstance.Send<RaiseWindowMessage>(message);
    }
}

In your view or preferably in some view handler class..在您的视图中或最好在某些视图处理程序类中..

public class ViewHandler
{
    public ViewHandler()
    {
        Messenger.Default.Register<RaiseWindowMessage>(this, raiseNextWindow);
    }        

    private void raiseNextWindow(RaiseWindowMessage obj)
    {
        // determine which window to raise and show it
        switch (obj.WindowName)
        {
            case "NextWindow":
                NextWindow view = new NextWindow();
                if (obj.ShowAsDialog)
                    view.ShowDialog();
                else
                    view.Show();
                break;
            // some other case here...
            default:
                break;
        }
    }
}
nw.Owner = Application.Current.MainWindow;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM