Esempio n. 1
0
        public async Task Navigate(string viewKey, object args)
        {
            using (var releaser = await _lock.LockAsync())
            {
                // Do not navigate to the same view.
                if (viewKey == _navigationContainer.CurrentViewKey)
                {
                    return;
                }

                _navigationContainer.CurrentViewKey = viewKey;

                if (_viewsByKey.ContainsKey(viewKey))
                {
                    var type = _viewsByKey[viewKey];

                    var view = await _viewService.Build(type, args) as IView;

                    if (view == null)
                    {
                        throw new Exception(String.Format("Unable to build view {0}", type.ToString()));
                    }

                    if (_navigationContainer == null)
                    {
                        throw new Exception($"{nameof(INavigationContainer)} is null. Did you forget to call NavigationService.Init()?");
                    }

                    _navigationContainer.SetNavigationBar(false, view); //TODO: read from stack

                    var model = view.BindingContext as IViewModel;

                    if (model != null)
                    {
                        view.Appearing    += (s, e) => { model.OnAppearing(); };
                        view.Disappearing += (s, e) => { model.OnDisappearing(); };
                    }

                    await _navigationContainer.PushAsync(view);

                    ThreadHelper.RunOnUIThread(() =>
                    {
                        if (model != null)
                        {
                            model.OnNavigated(args); // Do not await.
                        }
                    });
                }
                else
                {
                    throw new ArgumentException(
                              string.Format(
                                  "No such key: {0}. Did you forget to call NavigationService.Map?",
                                  viewKey),
                              "key");
                }
            }
        }