MVVM灯光放大器; WPF - 绑定窗口的多个实例到视图模型 [英] MVVM Light & WPF - Binding Multiple instances of a Window to a ViewModel
问题描述
我的工作我的MVVM的第一个项目,我已经选择使用MVVM光工具包。我有一个 GameViewModel
我的游戏的主屏幕上处理业务。我需要找出如何使用冒险家
的一个实例,打开一个新窗口( AdventurerView
)作为参数时,被执行的命令,有它必然 AdventurerViewModel
,显示并返回数据。这个窗口的实例将被打开并经常关闭。我一直停留在这几天的现在和它的驾驶我疯了。我想学习如何与MVVM光或纯XAML提供的工具pferably做到这一点在MVVM友好的方式,$ P $。
I am working on my first project in MVVM and I've chosen to use the MVVM Light Toolkit. I have a GameViewModel
that handles business on the main screen of my game. I need to find out how to open a new window (AdventurerView
) with an instance of Adventurer
as a parameter when a command is executed, have it bound to AdventurerViewModel
, and display and return data. Instances of this window will be opened and closed frequently. I have been stuck on this for a couple of days now and it's driving me crazy. I would like to learn how to do this in an MVVM-friendly way, preferably with the tools provided by MVVM Light or pure XAML.
我已经尝试过使用光MVVM的 ViewModelLocator
但因为 AdventurerView
是它不会工作的窗口;它说:不能把一个窗口的样式,虽然程序仍编译和运行。难道还有什么我可以改变,使这项工作?或者是有结合他们在XAML另一种方式?或者换个完全进场?我真的喜欢能够从该继续前进。我一直在使用MVVM光的使者无济于事(这仍然没有解决视图/视图模型的问题)也尝试过。
I've tried using MVVM Light's ViewModelLocator
but since AdventurerView
is a window it won't work; it says "Can't put a Window in a Style", though the program still compiles and runs. Could there be something I could change to make that work? Or is there another way to bind them in XAML? Or another approach entirely? I would really love to be able to move on from this. I have also tried using MVVM Light's messenger to no avail (which still doesn't tackle the View/ViewModel issue).
我只需要能够创建一个绑定到 AdventurerViewModel
和显示窗/返回相应的数据。
I just need to be able to create a window that is bound to AdventurerViewModel
and display/return the appropriate data.
AdventurerView.xaml是目前默认状态,但我觉得如果我能绑定相应的数据,可能会帮助(DataContext的)。
AdventurerView.xaml is in its default state at the moment, but I feel that if I could bind the appropriate data that might help (DataContext).
AdventurerViewModel是pretty裸机以及
AdventurerViewModel is pretty bare-bones as well
class AdventurerViewModel : ViewModelBase
{
#region Members
private Adventurer _adv;
#endregion
#region Properties
public Adventurer Adv
{
get { return _adv; }
set { _adv = value; }
}
#endregion
#region Construction
public AdventurerViewModel(Adventurer adv)
{
this._adv = adv;
}
#endregion
}
App.xaml中在底部的非工作的DataTemplate:
App.xaml with the non-working DataTemplate at the bottom:
<Application StartupUri="MainWindow.xaml"
xmlns:views="clr-namespace:AoW.Views"
xmlns:vm="clr-namespace:AoW.ViewModels"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AoW.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<DataTemplate DataType="{x:Type vm:GameViewModel}">
<views:GameView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:TitleViewModel}">
<views:TitleView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:AdventurerViewModel}">
<views:AdventurerView />
</DataTemplate>
</Application.Resources>
</Application>
在 GameViewModel
命令,将有望使这一切发生(在MessageBox只是证实了命令发射):
The command in GameViewModel
that will hopefully make this all happen (the messagebox just confirms that the command is firing):
private void ExecuteShowAdvCommand(Adventurer adv)
{
System.Windows.MessageBox.Show(adv.Name);
}
我真的不知道还有什么要包括
I don't really know what else to include.
推荐答案
好吧,我组建了一个演示,应该让这个希望更容易为你的下载链接
Ok I put together a demo that should make this hopefully easier for you Download Link
功能:
- 3的Windows在总(
主窗口
,ModalWindow
,NonModalWindow
) -
主窗口
有一个文本框
你可以输入任何你想要进去。 - 2个按钮的上方会相应打开莫代尔/非模态窗口
- 打开会显示在主窗口的
文本框的消息时,每个窗口
在的TextBlock
在他们里面。 - 在每个窗口,你可以勾选一个
复选框
更新主窗口
在搜索结果的文本块中的值(模态窗口在关闭模式窗口,这将一命呜呼对于非模态的变化可以看出尽快)
- 3 Windows in Total (
MainWindow
,ModalWindow
,NonModalWindow
) MainWindow
has aTextBox
you can type whatever you want into.- 2 buttons on the top will open the Modal / NonModal Window accordingly
- Each window when opened will display the message that was in MainWindow's
TextBox
in aTextBlock
inside them. - In each window you can tick a
CheckBox
to update the value in result's textblock inMainWindow
(For the Modal Window this will kick in when modal window is closed. For NonModal changes can be seen asap)
这就是它的功能,
概念:
- 注册多个虚拟机与
SimpleIoC
,并使用的GetInstance(...)
来要求他们。 - 信使类,使用一个自定义消息类型
OpenWindowMessage
- 从父虚拟机打开莫代尔/非模态的Windows坚守的原则MVVM
- 窗口(只是NONMODAL所示)之间传递数据
- Registering Multiple VM's with the
SimpleIoC
and usingGetInstance(...)
to request them out. - Messenger class usage with a custom message type
OpenWindowMessage
- Opening Modal / Non Modal Windows from a parent VM staying true to the MVVM principles
- Passing data between windows(just shown in NonModal)
重要提示:
- 在这个例子中,设置非DP使用的方法的DialogResult
从模态窗口没有MVVM友好COS它使用code-背后设置的DialogResult
上的应避免(如果需要将可测试),一个 Window.Closing
事件属性。我的preferred的做法是有点长,非常有据可查的<一个href=\"http://stackoverflow.com/questions/3801681/good-or-bad-practise-for-dialogs-in-wpf-with-mvvm\">HERE(Mixture问题和答案的)。因此,为什么我忽略了它为这个样品的缘故。
Important Note:
- The method used in this example to set the non DP DialogResult
from the modal window is not MVVM friendly cos it uses code-behind to set the DialogResult
property on a Window.Closing
event which should be avoided(If needing to be "testable"). My preferred approach is a bit long and is very well documented HERE(Mixture of question and answer). Hence why I ignored it for the sake of this sample.
这篇关于MVVM灯光放大器; WPF - 绑定窗口的多个实例到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!