Views Locator or Navigation Service for MVVM

12 בNovember 2014

Many frameworks of MVVM provide Views Service Locator , which allow you to inject any view in

.any place without reference or dependencies between libraries

.Actually , it is not very hard to write one of your own if needed

:For example lets try that

:Base contract fro the service

public interface IAppNavigationService

    {

        object ResolveView(string key);

        void SignView(string key, Type viewType);

        List<string> getAllKeys();

    }

:A simple version of implementation

public class AppNavigationService : IAppNavigationService

    {

        internal Dictionary<string, object> views;

        internal Dictionary<string, Type> viewTypes;


        public AppNavigationService()

        {

            views = new Dictionary<string, object>();

            viewTypes = new Dictionary<string, Type>();

        }


        public object ResolveView(string key)

        {

            try

            {

                if (views[key] == null)

                {

                    views[key] = BuildObject (views[key].GetType());

                }

                return views[key];

            }

            catch (Exception)

            {

                throw;

            }

        }


        public void SignView(string key, Type viewType)

        {

            if (!this.views.ContainsKey(key))

            {

                this.views.Add(key, BuildObject (viewType));

                this.viewTypes.Add(key, viewType);

            }

        }


        public List<string> getAllKeys()

        {

            return this.views.Keys.ToList<string>();

        }


        public static object BuildObject(Type t)

        {

            try

            {

                return t.GetConstructor(new Type[] { }).Invoke(new object[] { });

            }

            catch

            {

                return null;

            }

        }

    }

.What I did here is to use reflection to create instance per type than hold it in internal dictionary

.Now to who will use that service

,In this demo me as client use IOC container to have this service than register View inside

IOC.Default.GetInstance<IAppNavigationService>().SignView("rightView", typeof(Views.RightViewStarterView));


But also we can work directly on the service or use other IOC mechanism

Than we can at any view Model to hold property of type UserControl/Object

:And extract instance of View like

private UserControl m_RightContent;

        public UserControl RightContent

        {

            get { return m_RightContent; }

            set

            {

                m_RightContent = value;

                RaisePropertyChanged(() => RightContent);

            }

        }

 RightContent = IOC.Default.

 GetInstance<IAppNavigationService>().ResolveView("rightView");

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *