在 MVVM WPF 中切换两个视图 (Windows)
我正在开发一个具有 2 种模式的应用程序
- 配置模式
- 执行模式
两种模式都将在新窗口中打开。我已经做好了配置窗口。
我需要通过按 Key F12
或类似的东西在两种模式之间切换....此外,我还必须在从执行模式切换到配置模式时提示用户输入密码(仅在会话期间一次)已经制作了密码屏幕并在配置中实现。
我也担心,因为我使用了 Messenger(Meaditor 模式),所以关闭和打开 2 个不同的窗口将再次注册代表......并且我正在启动模态窗口
。
另外,我们是否需要保持两个视图都处于活动状态,或者我可以在切换时杀死其中一个视图。
对实现完全困惑......
我的 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);
}
}
I am developing an application which has 2 Modes
- Configuration Mode
- Execution Mode
Boths modes will open in new window. I have already made the configuration Window.
I need to switch between 2 Modes by pressing Key F12
or something like that.... also i have to prompt the user for password while switching from Execution to configutation mode (Just once durnig the session) i have made the password screen and implemented in configuration.
I am also worried as i have used Messenger(Meaditor pattern) so closing and opening of 2 different windows will register the delegates again nad again... and i am launching Modal windows
.
Also do we need to keep both the Views alive or i can kill one of them on toggling.
Totally confused about the implementation...
My App.Xaml Code
/// <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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,你的实现并不完全错误。
如果我需要通过按键在模态窗口上的两个视图之间切换,那么我就会采用 MVVM 方式...
窗口上有一个
ContentControl
。将Content
绑定到DataContext
。创建两个数据模板作为资源。一个数据模板对应“配置”视图,另一个数据模板对应“执行”视图。
您可以将这些数据模板保存在不同的资源文件中,并将资源字典合并到窗口资源中。
创建单个 ViewModel实现了可写的
Mode
属性。在上面的代码中,
DelegateCommand
并未在 .Net API 内部找到。从互联网上获取它们的实现。为窗口上的
F12
注册一个KeyBinding
。 F12KeyBinding
的Command
来自ViewModel
。在 .Net 4.0 之前的版本中,
Command
属性不可绑定。为此,请使用 CommandReference 。OnModeChange() 对于
F12
键,从ViewModel
类切换Mode
并引发属性更改通知。在
Window
上的ContentControl
上有数据触发器。在数据触发器中,检查Mode
是否为“Config”,并将ContentTemplate
更改为“Config”数据模板,否则更改为“Execution”数据模板。我希望我的方法在某种意义上对你有帮助。
Your implementation doesnt look totally wrong to me.
If I am given a requirement to swtich between two views on a modal window by a key press then I would have gone the MVVM way...
Have a
ContentControl
on the Window. Bind theContent
to theDataContext
.Create two data templates as resources. One data template corresponds to the view of "Config" and other data template is for 'Execution" view.
You can keep these data templates in different resources files and merge the resource dictionaries to the window resources.
Create a single ViewModel with a writeable
Mode
property. Have INotifyPropertyChanged implemented.In the code above
DelegateCommand
is not internally found in .Net APIs. Get their implementation from the internet.Register a
KeyBinding
forF12
on the window. TheCommand
for F12KeyBinding
comes from theViewModel
.In versions prior to .Net 4.0, the
Command
property is not bindable. use CommandReference for this.OnModeChange() for
F12
key, switch theMode
fromViewModel
class and raise property changed Notification.Have data triggers on the
ContentControl
on theWindow
. In the data triggers checkMode
if it is "Config" and change theContentTemplate
to "Config" data template othwerise change to "Execution" datatemplate.I hope my way helps you in some sense.