如何关闭 mvvm-light 中的 UserControl 视图?

发布于 2024-09-05 16:13:58 字数 797 浏览 0 评论 0原文

我创建了一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-09-12 16:13:58

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文