在MVVM WPF中切换两个视图(窗口)

在MVVM WPF中切换两个视图(窗口),wpf,mvvm,Wpf,Mvvm,我正在开发一个有两种模式的应用程序 配置模式 执行模式 两种模式将在新窗口中打开。我已经创建了配置窗口 我需要按F12键或类似的键在两种模式之间切换。。。。此外,我还必须在会话期间从执行模式切换到配置模式时提示用户输入密码,这一次我已经进入了密码屏幕并在配置中实现了 我也很担心,因为我使用了Messenger编辑模式,所以关闭和打开两个不同的窗口将再次注册代表并再次注册。。。我正在启动模态窗口 我们还需要保持这两个视图的活力,否则我可以在切换时杀死其中一个视图 对实施完全困惑 我的App.Xam

我正在开发一个有两种模式的应用程序

配置模式 执行模式 两种模式将在新窗口中打开。我已经创建了配置窗口

我需要按F12键或类似的键在两种模式之间切换。。。。此外,我还必须在会话期间从执行模式切换到配置模式时提示用户输入密码,这一次我已经进入了密码屏幕并在配置中实现了

我也很担心,因为我使用了Messenger编辑模式,所以关闭和打开两个不同的窗口将再次注册代表并再次注册。。。我正在启动模态窗口

我们还需要保持这两个视图的活力,否则我可以在切换时杀死其中一个视图

对实施完全困惑

我的App.Xaml代码

 /// <summary>
    ///   Application_Startup
    /// </summary>
    /// <param name = "sender"></param>
    /// <param name = "e"></param>
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        log.Debug("Application_Startup  " + Utility.FUNCTION_ENTERED_LOG);
        try
        {

                if (e.Args.Length == 0)
                {
                    AboutDialog.SpashScreen(Utility.TOOL_NAME,
                                            Utility.TOOL_VERSION);
                    MainView mainForm = new MainView();
                    mainForm.ShowDialog();
                }
                else
                {
                    string key = null;
                    foreach (string arg in e.Args)
                    {
                        if (arg.StartsWith("-"))
                        {
                            //this is a key         
                            key = arg;
                            if (key.Equals("-config"))
                            {
                                CommandLineArgs.Add(key, "config");
                                break;
                            }
                            if (key.Equals("-start"))
                            {
                                CommandLineArgs.Add(key, "start");
                            }
                        }
                        else
                        {
                            //should be a value        
                            if (key == null)
                            {
                                throw new Exception(
                                    "The command line arguments are malformed.");
                            }
                            CommandLineArgs.Add(key, arg);
                            key = null;
                        }
                    }
                    string config = string.Empty;
                    foreach (object k in App.CommandLineArgs.Keys)
                    {
                        config += CommandLineArgs[k].ToString();
                    }

                    switch (config)
                    {
                        case "config":
                            AboutDialog.SpashScreen(
                                Utility.TOOL_NAME,
                                Utility.TOOL_VERSION);
                            MainView mainForm = new MainView();
                            mainForm.ShowDialog();
                            break;
                         case "start" :
                               ExecutionForm execuForm= new ExecutionForm();
                               execuForm.ShowDialog();
                               break;
                        default:
                            MessageBox.Show("Incorrect Parameters",
                                            Utility.TOOL_NAME);
                            Application.Current.Shutdown();
                            break;
                    }

            }
            log.Debug("Application_Startup" + Utility.FUNCTION_EXIT_LOG);
        }
        catch (Exception ex)
        {              
            log.Error("Application_Startup" + ex.Message, ex);
        }
    }

在我看来,你的实现并没有完全错

如果我被要求通过按键在一个模式窗口的两个视图之间切换,那么我会选择MVVM方式

使用单一模式窗口,在特定情况下承载多个内容。 在窗口上有一个ContentControl。将内容绑定到DataContext

在上面的代码中,DelegateCommand在.NETAPI内部找不到。从互联网上获取它们的实现

在窗口上注册F12的键绑定。F12键绑定的命令来自ViewModel


我希望我的方法能在某种意义上帮助你。

我觉得你的实现并没有完全错

如果我被要求通过按键在一个模式窗口的两个视图之间切换,那么我会选择MVVM方式

使用单一模式窗口,在特定情况下承载多个内容。 在窗口上有一个ContentControl。将内容绑定到DataContext

在上面的代码中,DelegateCommand在.NETAPI内部找不到。从互联网上获取它们的实现

在窗口上注册F12的键绑定。F12键绑定的命令来自ViewModel


我希望我的方式能在某种意义上帮助你。

谢谢你的否决票。。。。但至少有礼貌地告诉我为什么…这里没有真正的问题,除了一些代码…谢谢你的否决票。。。。但至少请有礼貌地告诉我为什么…这里没有真正的问题,除了一些代码…谢谢,我会尝试一下,然后cme返回给你当然不要忘记将ViewModel的实例设置为窗口的DataContext:-我试过你的方法,但是菜单栏对于两种视图都是不同的??如何处理。。。。每个视图或……的不同菜单栏。。。。。。还有一种方法是使用DockPanel包装ContentControl,并将菜单停靠在顶部。如果菜单项根据模式更改,则根据menu.Style.Thank下的类似数据触发器进行更改。谢谢,我会尝试,然后cme返回给您…:当然不要忘记将ViewModel的实例设置为窗口的DataContext:-我试过你的方法,但是菜单栏对于两种视图都是不同的??如何处理。。。。每个视图或……的不同菜单栏。。。。。。还有一种方法是使用DockPanel包装ContentControl,并将菜单停靠在顶部。如果菜单项根据模式更改,则根据menu.Style下的类似数据触发器进行更改。
<DockPanel LastChildFill="true">
    <Menu DockPanel.Dock="Top" ...>
    </Menu>
    <ContentControl Content="{Binding}" ... />
</DockPanel>
<Window.Resources>
     <DataTemplate x:Key="ConfigView" ... />
     <DataTemplate x:Key="ExecutionView" ... />
</Window.Resources>
public class ViewModel : INotifPropertyChanged
{
    private string mode;

    public string Mode
    {
         get  { return mode; }
         set  { mode = value; NotifPropertyChanged("Mode"); }
    }

    private IComamnd changeModeCommand;

    public ICommand ChangeModeCommand
    {
        get
        {
            if (changeModeCommand == null)
                 changeModeCommand
                    = new DelegateCommand(
                         OnModeChange,
                         CanModeChange);
            return changeModeCommand;
        }
    }

    //// Other properties and functions go here ...
}
<Window.InputBindings>
    <KeyBinding Command="{Binding ChangeModeCommand}"
                Key="F12" />
</Window.InputBindings>
   <ContentControl Content="{Binding}">
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Mode}" Value="Config">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource ConfigView}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Mode}" Value="Execution">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource ExecutionView}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
  </ContentControl>