How to close a UserControl View in mvvm-light?

▼魔方 西西 提交于 2019-12-25 01:03:20

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/3042741/how-to-close-a-usercontrol-view-in-mvvm-light

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!