/// <summary>
        /// Register your ViewModel to be presented at a IFragmentHost. Backingstore for this is a
        /// Dictionary, hence you can only have a ViewModel registered at one IFragmentHost at a time.
        /// Just call this method whenever you need to change the host.
        /// </summary>
        /// <typeparam name="TViewModel">Type of the ViewModel to present</typeparam>
        /// <param name="host">Which IFragmentHost (Activity) to present it at</param>
        public void RegisterViewModelAtHost <TViewModel>(IFragmentHost host)
            where TViewModel : IMvxViewModel
        {
            if (host == null)
            {
                Mvx.Warning("You passed a null IFragmentHost, removing the registration instead");
                UnRegisterViewModelAtHost <TViewModel>();
            }

            _dictionary[typeof(TViewModel)] = host;
        }
        private bool ResolveHost(Type typeOfViewModel, out IFragmentHost host)
        {
            foreach(var hst in _registeredHosts)
            {
                if (hst.Value.SupportedPresenterRegistrations.ContainsViewModel(typeOfViewModel))
                {
                    host = hst.Value;
                    return true;
                }
            }

            host = null;
            return false;
        }
        public static MvxViewModelResponse Show(MvxViewModelRequest request, IFragmentHost host)
        {
            var response = new MvxViewModelResponse();

            //Resolve viewFinder
            var viewFinder = Mvx.Resolve<IMvxViewsContainer>();
            var presenter = Mvx.Resolve<ICustomPresenter> ();

            //Resolve viewtype of viewmodel
            var viewType = viewFinder.GetViewType (request.ViewModelType);
            if (viewType == null)
                throw new MvxException ("View Type not found for " + request.ViewModelType);

            //Create instance of view
            var viewObject = Activator.CreateInstance (viewType);
            if (viewObject == null)
                throw new MvxException ("View not loaded for " + viewType);

            var viewModelLoader = Mvx.Resolve<IMvxViewModelLoader> ();
            IMvxFragmentView fragment = viewObject as IMvxFragmentView;

            //We only host fragments
            if (fragment!=null) { // View is a fragment, that should be hosted

                fragment.ViewModel = viewModelLoader.LoadViewModel (request, null);
                Android.Support.V4.App.Fragment frag = viewObject as Android.Support.V4.App.Fragment;

                //Do we know where to put this fragment?
                if(!host.SupportedPresenterRegistrations.ContainsViewModel(request.ViewModelType))
                    throw new MvxException ("Unable to host fragment " + viewType + ". Because it's host does not support this region.");

                //Resolve regionId
                int regionId = host.SupportedPresenterRegistrations.GetRegionId(request.ViewModelType);

                //Push fragment to the view
                var trans = host.CurrentFragmentManager.BeginTransaction ();
                host.OnTransactionCreated(request,trans);
                trans.Replace (regionId,frag).Commit ();

                response.RegionId = regionId;
                response.Fragment = fragment;

            } else {
                throw new MvxException ("Unable to host view, since this view is not a fragment " + viewType);
            }

            return response;
        }
 /// <summary>
 /// Registers the host that is provided.
 /// </summary>
 /// <param name="host">Host.</param>
 public void RegisterHost(IFragmentHost host)
 {
     //Make sure each host can only be registered once. Otherwise it will be overwritten.
     _registeredHosts [host.GetType ()] = host;
 }
Exemple #5
0
 public void Register(Type viewModelType, IFragmentHost host)
 {
     this._dictionary[viewModelType] = host;
 }
 public void Register(Type viewModelType, IFragmentHost host)
 {
     _dictionary[viewModelType] = host;
 }
        /// <summary>
        /// Closes the specified view model.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        public override void Close(IMvxViewModel viewModel)
        {
            //We implement this code to auto close fragments where the user makes a selection
            //This is actually stupid app behaviour
            IFragmentHost host = null;

            //var orgVm = viewModel as OrganisationsViewModel;
            //if (orgVm != null)
            //{
            //	if (_dictionary.TryGetValue(typeof(OrganisationsViewModel), out host))
            //	{
            //		host.Close(viewModel);
            //	}
            //}

            //if (host == null)
            //{
            //	var siteVm = viewModel as SitesViewModel;
            //	if (siteVm != null)
            //	{
            //		if (_dictionary.TryGetValue(typeof(SitesViewModel), out host))
            //		{
            //			host.Close(viewModel);
            //		}
            //	}
            //}

            //if (host == null)
            //{
            //	var siteVm = viewModel as ServersViewModel;
            //	if (siteVm != null)
            //	{
            //		if (_dictionary.TryGetValue(typeof(ServersViewModel), out host))
            //		{
            //			host.Close(viewModel);
            //		}
            //	}
            //}
            //if (host == null)
            //{
            //	var siteVm = viewModel as MeasurementTypesViewModel;
            //	if (siteVm != null)
            //	{
            //		if (_dictionary.TryGetValue(typeof(MeasurementTypesViewModel), out host))
            //		{
            //			host.Close(viewModel);
            //		}
            //	}
            //}

            //if (host == null)
            //{
            //	var siteVm = viewModel as MapRefreshRatesViewModel;
            //	if (siteVm != null)
            //	{
            //		if (_dictionary.TryGetValue(typeof(MapRefreshRatesViewModel), out host))
            //		{
            //			host.Close(viewModel);
            //		}
            //	}
            //}

            //if (host == null)
            //{
            //	var siteVm = viewModel as NotificationRuleViewModel;
            //	if (siteVm != null)
            //	{
            //		if (_dictionary.TryGetValue(typeof(NotificationRuleViewModel), out host))
            //		{
            //			host.Close(viewModel);
            //		}
            //	}
            //}

            if (host == null)
            {
                base.Close(viewModel);
            }
        }
Exemple #8
0
 public void Register(Type viewModelType, IFragmentHost host)
 {
     _typeToHostMap[viewModelType] = host;
 }