如何关闭 mvvm-light 中的 UserControl 视图?
我创建了一个包含 mainView 和其他 2 个视图(用户控件)的示例。 我在子视图上放置了一个“关闭”按钮,我想关闭该视图。 该按钮附加了一个命令,当按下关闭时,我要求 ViewModelLocator 清理它。
但是-视图仍在显示.. 我做错了什么? 如何使用 mvvm-light 关闭 userControl 视图?
private RelayCommand _closeCommand;
public RelayCommand CloseCommand
{
get
{
if (_closeCommand == null)
{
_closeCommand = new RelayCommand(()=>
ViewModelLocator.ClearAllChannels(),
);
}
return _closeCommand;
}
}
ViewModelLocator 函数:
public static void ClearAllChannels()
{
if (_allChannels != null)
{
_allChannels.Cleanup();
_allChannels = null;
}
}
I've craeted a sample with mainView and 2 other views (usercontrols).
I've placed a button "close" on the child view and i want to close that view.
there is a command attached to that button, and when close is pressed i ask the ViewModelLocator to clean it.
BUt- the view still being displayed..
What i'm doing wrong?
How can i close a userControl view with mvvm-light?
private RelayCommand _closeCommand;
public RelayCommand CloseCommand
{
get
{
if (_closeCommand == null)
{
_closeCommand = new RelayCommand(()=>
ViewModelLocator.ClearAllChannels(),
);
}
return _closeCommand;
}
}
ViewModelLocator function:
public static void ClearAllChannels()
{
if (_allChannels != null)
{
_allChannels.Cleanup();
_allChannels = null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ViewModelLocator 实际上并不托管您的视图。它只是提供了一种查找支持特定视图的 ViewModel 的方法。
因此,您需要请求托管视图的控件(可能是窗口或框架)来关闭它们。
The ViewModelLocator does not actually host your views. It just provides a way to look up the ViewModel that supports a particular view.
So you need to ask the control that hosts your views (probably a Window or Frame) to close them.