コード例 #1
0
        private void View_Disappearing(object sender, EventArgs e)
        {
            Page view = sender as Page;

            if (view == null)
            {
                return;
            }
            var lifeCycleModel = view.BindingContext as IAtomViewLifeCycleModel;

            lifeCycleModel?.OnDisappearing();

            Device.BeginInvokeOnMainThread(async() => {
                try
                {
                    await Task.Delay(1000);
                    bool exists = !nav.NavigationStack.Contains(view) && !nav.ModalStack.Contains(view);
                    if (exists)
                    {
                        lifeCycleModel?.OnRemoved();

                        // also remove...
                        view.Appearing    -= View_Appearing;
                        view.Disappearing -= View_Disappearing;
                        AtomCoachMarks.SetCoachMark(view, null);
                    }
                }
                catch (Exception ex) {
                    UIAtomsApplication.Instance.LogException?.Invoke(ex);
                }
            });
        }
コード例 #2
0
        public async Task <Page> NewPage(Type type, object parameters)
        {
            Type pageType      = type;
            Type viewModelType = null;

            if (!IsPage(type))
            {
                pageType = await GetRelatedType(type, type.Name.Substring(0, type.Name.Length - "ViewModel".Length));

                viewModelType = type;
            }


            object v = Activator.CreateInstance(pageType);

            Page view = v as Page;

            if (viewModelType == null)
            {
                viewModelType = await GetRelatedType(type, pageType.Name + "ViewModel");
            }

            if (viewModelType != null)
            {
                object model = NewModel(viewModelType);

                if (parameters != null)
                {
                    foreach (var p in parameters.GetType().GetProperties())
                    {
                        if (!p.CanRead)
                        {
                            continue;
                        }
                        object value = p.GetValue(parameters);
                        if (value == null)
                        {
                            continue;
                        }
                        var pm = viewModelType.GetProperty(p.Name);
                        if (pm == null)
                        {
                            continue;
                        }
                        if (pm.PropertyType != value.GetType())
                        {
                            Type n = Nullable.GetUnderlyingType(pm.PropertyType);
                            if (n == null || n != value.GetType())
                            {
                                value = Convert.ChangeType(value, pm.PropertyType);
                            }
                        }
                        pm.SetValue(model, value);
                    }
                }
                view.BindingContext = model;

                // check if viewmodel has IsBusy property...

                /*var atomViewModel = model as IAtomViewModel;
                 * if (atomViewModel != null) {
                 *  view.SetBinding(Page.IsBusyProperty, new Binding("IsBusy"));
                 * }*/

                var lifeCycleModel = model as IAtomViewLifeCycleModel;
                if (lifeCycleModel != null)
                {
                    view.Appearing    += View_Appearing;
                    view.Disappearing += View_Disappearing;

                    //view.Appearing += (s, e) =>
                    //{
                    //    lifeCycleModel.OnAppearing();
                    //};
                    //view.Disappearing += async (s, e) =>
                    //{


                    //    lifeCycleModel.OnDisappearing();

                    //    await Task.Delay(100);

                    //    bool exists = !nav.NavigationStack.Contains(view) && !nav.ModalStack.Contains(view);
                    //    if (exists)
                    //    {
                    //        lifeCycleModel.OnRemoved();
                    //    }

                    //};
                }
            }

            // find coachmark....
            var ct = await GetRelatedType(pageType, "CoachMarks");

            if (ct != null)
            {
                AtomCoachMarks.SetCoachMark(view, ct);
            }

            return(view);
        }