wpf - 从屏幕关闭导体窗口
问题描述
我有一个名为 WindowViewModel 的 ViewModel,它继承自Conductor<IScreen>.Collection.OneActive
. 从这个 ViewModel,我打开 ChildView(带有WindowManager.ShowWindow(ChildViewModelInstance)
)。我想要做的是从 ChildViewModel 关闭 WindowView。
这是我的 WindowViewModel:
public class WindowViewModel : Conductor<IScreen>.Collection.OneActive, IHandle<bool>
{
public WindowViewModel()
{
}
public void OpenChildView()
{
//some code
}
public void CloseItem(Screen item)
{
item.TryClose();
if (Items.Count == 0)
TryClose();
}
public void Handle(bool message)
{
//for some reason this isn't handled
TryClose();
}
}
这是我的 ChildViewModel:
public class ChildViewModel : Screen
{
public ChildViewModel()
{
//getting eventaggregator...
}
public void CloseWindow()
{
eventAgregator.PublishOnUIThread(true);
}
}
ChildView是WindowView中tabcontrol的tabItem。当我按下x
特定的 tabItem 标题时(如果只有一个 tabitem),WindowView 将关闭(因为调用了 closeitem)。
我试过PublishOnCurrentThread
andPublishOnbackgroundThread
而不是PublishOnUIThread
,但它们也不起作用。
解决方案
通过调用它的 Subscribe 方法注入WindowViewModel
并IEventAggregator
订阅它:
public class WindowViewModel : Conductor<IScreen>.Collection.OneActive, IHandle<bool>
{
private readonly IEventAggregator _eventAggregator;
public MainViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
public void Handle(bool message)
{
TryClose();
}
...
}
推荐阅读
- pointers - 检查指针是否指向 Nil Slice
- python - 我怎样才能使def函数不显示两次
- stringtemplate - 如何防止额外的分隔符?
- sql-server - 加入计数表。每天需要每个 Cust_ID
- python - 在 django 中创建登录会话
- cookies - 我遇到了一个小的 IT 问题
- error-handling - Rust:从标准输入读取和映射行并处理不同的错误类型
- java - Linux Mint 中的 JAVA 问题
- python - 针对 dataframe2 中的所有行对 dataframe1 的每一行进行操作
- javascript - 这个代理总是在代理上抛出“设置”是否有原因:陷阱为属性“主要”返回错误但仍然运行完美?