WPF和放大器; MVVM光 - 通过信使关闭特定的子窗口 [英] WPF & MVVM Light - Closing a specific child window via Messenger
问题描述
在我的项目,我能够打开多个子窗口,显示并从他们返回的信息,然后通过单击按钮关闭它们。那我遇到的问题是,点击接受或取消按钮关闭所有打开的窗口。我需要找到一种方法,只关闭正确的窗口,我一直无法弄清楚如何。我使用MVVM轻,我想令牌可能是关键所在,但我还没有想出如何使他们的工作。如果有人可以帮助我,我会非常感激。
创建于主窗口的子窗口:
Messenger.Default.Register< OpenWindowMessage>(这一点,消息=方式>
{
VAR唯一键= System.Guid.NewGuid()的ToString();
VAR adventurerWindowVM = SimpleIoc.Default.GetInstance< AdventurerViewModel>(唯一键);
adventurerWindowVM.Adv = message.Argument;
变种adventurerWindow =新AdventurerView()
{
的DataContext = adventurerWindowVM,
所有者=这个
};
adventurerWindow.Closed + =(发件人,参数)=> SimpleIoc.Default.Unregister(唯一键);
adventurerWindow.Show() ;
});
这AdventurerViewModel发送关闭窗口消息:
私人无效ExecuteAcceptCommand()
{
Messenger.Default.Send(Adv.Name);
Messenger.Default.Send&所述; CloseWindowMessage>(新CloseWindowMessage());
}
接收的关闭窗口一封邮件中AdventurerView:
Messenger.Default.Register< CloseWindowMessage>(这一点,X =>关闭());
标记的方法:
您有一个设置唯一
生成您的每一个 AdventurerView
用。只需要使用发送时, CloseWindowMessage
作为标记。
首先,在 AdventurerViewModel
添加字符串类型的新属性说WindowKey
公共字符串WindowKey {搞定;组; }
接下来,添加一个构造函数来参加 AdventureView $的唯一键C $ C>。在
AdventurerView.xaml.cs
:
公共AdventurerView()
:这(的String.Empty){}
公共AdventurerView(字符串唯一键){
的InitializeComponent();
Messenger.Default.Register< CloseWindowMessage>(这一点,唯一键,S =>关闭());
}
接下来,在 MainWindow.xaml.cs
开关
Messenger.Default.Register< OpenWindowMessage>(这一点,消息=>
{
VAR唯一键= System.Guid.NewGuid()的ToString();
...
adventurerWindow.Show();
});
到
Messenger.Default.Register< OpenWindowMessage>(这一点,消息=>
{
VAR唯一键= System.Guid.NewGuid()的ToString();
VAR adventurerWindowVM = SimpleIoc.Default.GetInstance< AdventurerViewModel>(唯一键);
adventurerWindowVM.Adv = message.Argument;
adventurerWindowVM.WindowKey =唯一键;
变种adventurerWindow =新AdventurerView(唯一键)
{
的DataContext = adventurerWindowVM,
所有者=这个
};
adventurerWindow.Closed + =(发件人,参数)=> SimpleIoc.Default.Unregister< AdventurerViewModel>(唯一键) ;
adventurerWindow.Show();
});
最后,在 AdventurerViewModel.xaml.cs
:
开关
私人无效ExecuteAcceptCommand(){
Messenger.Default.Send(Adv.Name);
Messenger.Default.Send&所述; CloseWindowMessage>(新CloseWindowMessage());
}
到
私人无效ExecuteAcceptCommand(){
Messenger.Default.Send(Adv.Name);
Messenger.Default.Send< CloseWindowMessage>(新CloseWindowMessage(),WindowKey);
}
备选:
偶虽然上面会完全正常工作,有一个替代这种方法。您的信息已经是一个自定义的强类型( CloseWindowMessage
)。现在,你可以添加 WindowKey
作为消息的一部分,并且让每个窗口
当收到一个新的 CloseWindowMessage
检查 WindowKey
反对它自己的核心消息中和关闭()
因此。
In my project I am able to open multiple child windows, display and return information from them, and then close them with a button click. The problem that I am having is that clicking the "Accept" or "Cancel" button closes all open windows. I need to find a way to only close the correct window and I haven't been able to figure out how. I am using MVVM Light and I'm thinking tokens may be the key but I haven't figured out how to make them work. If anybody could help me I'd greatly appreciate it.
Creating the child window in MainWindow:
Messenger.Default.Register<OpenWindowMessage>(this, message =>
{
var uniqueKey = System.Guid.NewGuid().ToString();
var adventurerWindowVM = SimpleIoc.Default.GetInstance<AdventurerViewModel>(uniqueKey);
adventurerWindowVM.Adv = message.Argument;
var adventurerWindow = new AdventurerView()
{
DataContext = adventurerWindowVM,
Owner = this
};
adventurerWindow.Closed += (sender, args) => SimpleIoc.Default.Unregister(uniqueKey);
adventurerWindow.Show();
});
Sending the close window message from AdventurerViewModel:
private void ExecuteAcceptCommand()
{
Messenger.Default.Send(Adv.Name);
Messenger.Default.Send<CloseWindowMessage>(new CloseWindowMessage());
}
Receiving the close window messsage in AdventurerView:
Messenger.Default.Register<CloseWindowMessage>(this, x => Close());
Token approach:
You have a uniqueKey
you generate each of your AdventurerView
with. Just use that when sending the CloseWindowMessage
as the token.
Firstly in AdventurerViewModel
add a new property of type string say "WindowKey"
public string WindowKey { get; set; }
Next add a constructor to take the unique key in AdventureView
. In AdventurerView.xaml.cs
:
public AdventurerView()
:this(string.Empty) {}
public AdventurerView(string uniqueKey) {
InitializeComponent();
Messenger.Default.Register<CloseWindowMessage>(this, uniqueKey, s => Close());
}
Next in MainWindow.xaml.cs
switch
Messenger.Default.Register<OpenWindowMessage>(this, message =>
{
var uniqueKey = System.Guid.NewGuid().ToString();
...
adventurerWindow.Show();
});
to
Messenger.Default.Register<OpenWindowMessage>(this, message =>
{
var uniqueKey = System.Guid.NewGuid().ToString();
var adventurerWindowVM = SimpleIoc.Default.GetInstance<AdventurerViewModel>(uniqueKey);
adventurerWindowVM.Adv = message.Argument;
adventurerWindowVM.WindowKey = uniqueKey;
var adventurerWindow = new AdventurerView(uniqueKey)
{
DataContext = adventurerWindowVM,
Owner = this
};
adventurerWindow.Closed += (sender, args) => SimpleIoc.Default.Unregister<AdventurerViewModel>(uniqueKey);
adventurerWindow.Show();
});
Finally in AdventurerViewModel.xaml.cs
:
switch
private void ExecuteAcceptCommand() {
Messenger.Default.Send(Adv.Name);
Messenger.Default.Send<CloseWindowMessage>(new CloseWindowMessage());
}
to
private void ExecuteAcceptCommand() {
Messenger.Default.Send(Adv.Name);
Messenger.Default.Send<CloseWindowMessage>(new CloseWindowMessage(), WindowKey);
}
Alternate:
Even-though the above would work perfectly fine, there is an alternate to this approach. Your "Message" is already a custom strong type(CloseWindowMessage
). Now you can just add WindowKey
as part of the message and have each Window
when it receives a new CloseWindowMessage
check the WindowKey
in the message against it's own key and Close()
accordingly.
这篇关于WPF和放大器; MVVM光 - 通过信使关闭特定的子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!