我有一个名为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中tab控件的tabItem。当我在特定tabItem标头上按x
时(如果只有一个tabitem),则WindowView将关闭(因为调用了closeitem)。
我尝试使用PublishOnCurrentThread
和PublishOnbackgroundThread
而不是PublishOnUIThread
,但是它们也不起作用。
答案 0 :(得分:1)
为WindowViewModel
注入IEventAggregator
并通过调用其Subscribe方法来订阅它:
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();
}
...
}