Wpf 从屏幕关闭导体窗口
我有一个名为WindowViewModel的ViewModel,它继承自Wpf 从屏幕关闭导体窗口,wpf,caliburn.micro,Wpf,Caliburn.micro,我有一个名为WindowViewModel的ViewModel,它继承自Conductor.Collection.OneActive。从这个视图模型中,我打开了ChildView(使用WindowManager.ShowWindow(ChildViewModelInstance))。我要做的是从ChildViewModel关闭WindowView 这是我的WindowViewModel: public class WindowViewModel : Conductor<IScreen>
Conductor.Collection.OneActive
。从这个视图模型中,我打开了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();
}
}
public class ChildViewModel : Screen
{
public ChildViewModel()
{
//getting eventaggregator...
}
public void CloseWindow()
{
eventAgregator.PublishOnUIThread(true);
}
}
ChildView是WindowView中tabcontrol的tabItem。当我在特定的tabItem标题上按x
时(如果只有一个tabItem),WindowView关闭(因为调用了closeitem)
我尝试了
PublishOnCurrentThread
和PublishOnbackgroundThread
而不是PublishOnIthread
,但它们也不起作用。用IEventAggregator
注入WindowViewModel
,并通过调用其订阅方法进行订阅:
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();
}
...
}
公共类WindowViewModel:Conductor.Collection.OneActive,IHandle
{
私有只读ieventagegrator\u事件聚合器;
公共主视图模型(IEventAggregator事件聚合器)
{
_eventAggregator=eventAggregator;
_eventAggregator.Subscribe(此);
}
公共无效句柄(bool消息)
{
TryClose();
}
...
}
我在代码中做了一些更改,当我发现我忘了写_eventAggregator.Subscribe(这个);