public static CustomRoutingParams GetValueOrDefault(object param, bool reuseExistingView = true) { CustomRoutingParams result = null; var routeParams = param.AsRoutableViewModel<IRoutableViewModel>(); if (routeParams != null) { result = routeParams.Item2 as CustomRoutingParams; } if (result == null) { result = new CustomRoutingParams { ReuseExistingView = reuseExistingView }; } return result; }
public static CustomRoutingParams GetValueOrDefault(object param, bool reuseExistingView = true) { CustomRoutingParams result = null; var routeParams = param.AsRoutableViewModel <IRoutableViewModel>(); if (routeParams != null) { result = routeParams.Item2 as CustomRoutingParams; } if (result == null) { result = new CustomRoutingParams { ReuseExistingView = reuseExistingView }; } return(result); }
// Routing #region public void OnNavigate(Tuple<IRoutableViewModel, IRoutingParams> viewModelWithParams) /// <summary> /// Called when [navigate]. /// </summary> /// <param name="viewModelWithParams">The view model with parameters.</param> public void OnNavigate(Tuple <IRoutableViewModel, IRoutingParams> viewModelWithParams) { this.Log().Debug("OnNavigate => {0} (StackCount: {1}; CurrentFragmentTag: {2})", Convert.ToString(viewModelWithParams.Item1), App.Current.AppModel.Router.NavigationStack.Count, _currentFragmentTag); if (viewModelWithParams != null) { var urlPathSegment = viewModelWithParams.Item1.UrlPathSegment; // Reusable View? var customRouteParams = CustomRoutingParams.GetValueOrDefault(viewModelWithParams.Item2); var fragment = SupportFragmentManager.FindFragmentByTag(urlPathSegment); IViewFor view = null; if (fragment == null || !customRouteParams.ReuseExistingView) { var viewType = typeof(IViewFor <>); this.Log().Debug("OnNavigate => Fragment not found for '{0}'. Create new", viewModelWithParams.Item1.GetType().Name); view = AttemptToResolveView(viewType.MakeGenericType(viewModelWithParams.Item1.GetType()), customRouteParams.Contract); } else { this.Log().Debug("OnNavigate => Fragment found for '{0}'!!", viewModelWithParams.Item1.GetType().Name); view = fragment as IViewFor; } this.Log().Debug("OnNavigate => {0}", urlPathSegment); if (view != null) { var ft = SupportFragmentManager.BeginTransaction(); // Set ViewModel if (view.ViewModel != viewModelWithParams.Item1) { view.ViewModel = viewModelWithParams.Item1; } else { this.Log().Debug("OnNavigate => ViewModel unchanged"); } // Detach current fragment if different if (!String.IsNullOrEmpty(_currentFragmentTag) && _currentFragmentTag != urlPathSegment) { RemoveCurrentFragment(ft); } // Attach var newFragment = view as AppFragment; if (newFragment != null) { if (!newFragment.IsAdded) { this.Log().Debug("OnNavigate => Add: {0}", urlPathSegment); ft.Add(global::Android.Resource.Id.Content, newFragment, urlPathSegment); } if (newFragment.IsDetached) { this.Log().Debug("OnNavigate => Attach: {0}", urlPathSegment); ft.Attach(newFragment); } } ft.Commit(); // Set currentFragmentTag _currentFragmentTag = urlPathSegment; } } }