从屏幕关闭导体 Window
Close Conductor Window from screen
我有一个名为 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。当我在特定的 tabItem header 上按 x
(如果只有一个 tabitem),WindowView 关闭(因为调用了 closeitem)。
我试过 PublishOnCurrentThread
和 PublishOnbackgroundThread
而不是 PublishOnUIThread
,但它们也不行。
用 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 的 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。当我在特定的 tabItem header 上按 x
(如果只有一个 tabitem),WindowView 关闭(因为调用了 closeitem)。
我试过 PublishOnCurrentThread
和 PublishOnbackgroundThread
而不是 PublishOnUIThread
,但它们也不行。
用 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();
}
...
}