MVVM WPF改变窗口并关闭以前 [英] MVVM WPF change window and close the previous

查看:164
本文介绍了MVVM WPF改变窗口并关闭以前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为MVVM这一切架构首发我有关于到另一个一个窗口之间的导航的几个疑点。我使用的框架MVVM光

As a starter in this all architecture of MVVM I'm having a few doubts regarding the navigation between one window to another. I'm using the Framework MVVM Light.

我期望的行为是在这样的WinForms:

The behavior I expect is in WinForms like this:

GeneralWindow GW =新GeneralWindow(); this.Hide(); //或接近
gw.Show();

GeneralWindow gw = new GeneralWindow(); this.Hide(); // or close gw.Show();

我已经失去了几个小时试图找到使用一些提示信使,但是这些方法,我发现我在视图中使用代码隐藏,这是不是很MVVMish。

I already lose a couple of hours trying to find some hints using the messenger, but the methods I found I have to use code-behind in the view and that is not very MVVMish.

最好的问候,并感谢你在前进。

Best regards and thank you in advance.

推荐答案

我期望的行为是在这样的WinForms:

GeneralWindow gw = new GeneralWindow(); this.Hide(); // or close gw.Show();



MVVM模式划分查看视图模型。因此,它是没有资格来创建新的查看视图模型创建窗口实例,并显示从视图模型窗口是违反MVVM的即可。因此,我建议你使用流行的技术,你可以改变查看使用 ContentControl中的DataTemplate

MVVM pattern divides View from ViewModel. So it is not eligible to create new View from ViewModel. Creating window instance and showing window from view model is violation of MVVM". So I suggest you to use the popular technique where you can change Views using ContentControl and DataTemplate.

让我们在这个技术潜水

<Window>
   <Window.Resources>
      <DataTemplate DataType="{x:Type ViewModelA}">
         <localControls:ViewAUserControl/>
      </DataTemplate>
      <DataTemplate DataType="{x:Type ViewModelB}">
         <localControls:ViewBUserControl/>
      </DataTemplate>
   <Window.Resources>
  <ContentPresenter Content="{Binding CurrentView}"/>
</Window>

如果 Window.DataContext 是实例 ViewModelA ,那么 ViewA 将被显示, Window.DataContext ViewModelB ,那么 ViewB 将被显示。

If Window.DataContext is an instance of ViewModelA, then ViewA will be displayed and Window.DataContext is an instance of ViewModelB, then ViewB will be displayed.

让我来告诉在那里可以看到你应该把的DataTemplates 一个例子:

Let me show an example where it can be seen where you should put DataTemplates:

<Window x:Class="SimpleMVVMExample.ApplicationView"
        ...The code omitted for the brevity...
        Title="Simple MVVM Example with Navigation" Height="350" Width="525">
    <Window.Resources>
       <DataTemplate DataType="{x:Type ViewModelA}">
         <localControls:ViewAUserControl/>
      </DataTemplate>
      <DataTemplate DataType="{x:Type ViewModelB}">
         <localControls:ViewBUserControl/>
      </DataTemplate>
    </Window.Resources>

    <DockPanel>
        <Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="0,0,1,0">
            <ItemsControl ItemsSource="{Binding ListOfViewModels}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}"
                                Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                CommandParameter="{Binding }"
                                Margin="2,5"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>

        <ContentControl Content="{Binding CurrentDataTemplateViewModel}" />
    </DockPanel>
</Window>



我见过和阅读它是由雷切尔林所做的最好的例子。 见的例子。

更新:

如果你想真正打开新的窗口,那么你应该建立一个中间层,使视图模型不依赖于具体的实现创造新的窗口。

If you want really open new Window, then you should create an intermediate layer to make ViewModel not dependent on a concrete implementation of creating new window.

public class YourViewModel
{
    private readonly IWindowFactory windowFactory;
    private ICommand openNewWindow;

    public YourViewModel(IWindowFactory _windowFactory)
    {
        windowFactory = windowFactory;

        /**
         * Would need to assign value to m_openNewWindow here, and 
         * associate the DoOpenWindow method
         * to the execution of the command.
         * */
        openNewWindow = null;  
    }

    public void DoOpenNewWindow()
    {
        windowFactory.CreateNewWindow();
    }

    public ICommand OpenNewWindow { get { return openNewWindow; } }
}

public interface IWindowFactory
{
    void CreateNewWindow();
}

public class ProductionWindowFactory: IWindowFactory
{

    #region Implementation of INewWindowFactory

    public void CreateNewWindow()
    {
       NewWindow window = new NewWindow
           {
               DataContext = new NewWindowViewModel()
           };
       window.Show();
    }

    #endregion
}

如何关闭窗口?

有很多的方法。。其中之一是:

Application.Current.MainWindow.Close()

这篇关于MVVM WPF改变窗口并关闭以前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆