Give some command to View in MVVM

前端 未结 5 1851
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 23:07

Let\'s imagine I have some user control. The user control has some child windows. And user control user wants to close child windows of some type. There is a method in user

5条回答
  •  温柔的废话
    2020-11-28 23:36

    A reasonable way for purists is creating a service that handles your navigation. Short summary: create a NavigationService, register your view at the NavigationService and use the NavigationService from within the view model to navigate.

    Example:

    class NavigationService
    {
        private Window _a;
    
        public void RegisterViewA(Window a) { _a = a; }
    
        public void CloseWindowA() { a.Close(); }
    }
    

    To get a reference to NavigationService you could make an abstraction on top of it (i.e. INavigationService) and register/get it via a IoC. More properly you could even make two abstractions, one that contains the methods for registration (used by the view) and one that contains the actuators (used by the view model).

    For a more detailed example you could check out the implementation of Gill Cleeren which heavily depends on IoC:

    http://www.silverlightshow.net/video/Applied-MVVM-in-Win8-Webinar.aspx starting at 00:36:30

提交回复
热议问题