如何关闭用户控件视图中MVVM光?视图、控件、用户、MVVM

2023-09-06 23:54:21 作者:单身求解放

我craeted与MAINVIEW和2个其它视图(用户控件)的样品。 我已经放在一个按钮上的孩子认为接近,我想关闭这一观点。 有连接到该按钮的命令,并在靠近时pressed我问ViewModelLocator进行清洁。

按钮的观点仍显示.. 我在做什么错了? 我怎样才能缩小与MVVM光一个用户控件看法?

 私人RelayCommand _closeCommand;
    公共RelayCommand CloseCommand
    {
        得到
        {
            如果(_closeCommand == NULL)
            {
                _closeCommand =新RelayCommand(()=>
                   ViewModelLocator.ClearAllChannels(),

                   );
            }
            返回_closeCommand;
        }

    }
 

ViewModelLocator功能:

 公共静态无效ClearAllChannels()
    {
        如果(_allChannels!= NULL)
        {
            _allChannels.Cleanup();
            _allChannels = NULL;
        }
    }
 

解决方案

在ViewModelLocator实际上并没有承载你的看法。它只是提供了一种方法来查找支持特定视图的视图模型。

所以,你需要问承载您的观点(大概窗口或框架)的控制来关闭它们。

怎样使用ListView控件展示数据

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;
        }
    }

解决方案

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.