コード例 #1
0
        public static IMvxViewModel ReflectionGetViewModel(this IMvxView view)
        {
            if (view == null)
            {
                return(null);
            }

#if NETFX_CORE
            var propertyInfo = view.GetType().GetTypeInfo().RecursiveGetDeclaredProperty("ViewModel");

            if (propertyInfo == null)
            {
                return(null);
            }

            return((IMvxViewModel)propertyInfo.GetValue(view, new object[] { }));
#else
            var propertyInfo = view.GetType().GetProperty("ViewModel");

            if (propertyInfo == null)
            {
                return(null);
            }

            return((IMvxViewModel)propertyInfo.GetGetMethod().Invoke(view, new object[] {}));
#endif
        }
コード例 #2
0
        internal static Type GetViewModelType(this IMvxView view)
        {
            var viewType = view.GetType();
            var props    = viewType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var prop     = props.Where(p => p.Name == "ViewModel").FirstOrDefault();

            return(prop?.PropertyType);
        }
コード例 #3
0
        public static IMvxViewModel ReflectionGetViewModel(this IMvxView view)
        {
            if (view == null)
            {
                return(null);
            }

            var propertyInfo = view.GetType().GetProperty("ViewModel");

            if (propertyInfo == null)
            {
                return(null);
            }

            return((IMvxViewModel)propertyInfo.GetGetMethod().Invoke(view, new object[] {}));
        }
コード例 #4
0
        public static Type FindAssociatedViewModelTypeOrNull(this IMvxView view)
        {
            if (view == null)
            {
                return(null);
            }

            IMvxViewModelTypeFinder associatedTypeFinder;

            if (!Mvx.TryResolve(out associatedTypeFinder))
            {
                MvxTrace.Trace(
                    "No view model type finder available - assuming we are looking for a splash screen - returning null");
                return(typeof(MvxNullViewModel));
            }

            return(associatedTypeFinder.FindTypeOrNull(view.GetType()));
        }
コード例 #5
0
        public static Type?FindAssociatedViewModelTypeOrNull(this IMvxView view)
        {
            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }

            IMvxViewModelTypeFinder associatedTypeFinder;

            if (!Mvx.IoCProvider.TryResolve(out associatedTypeFinder))
            {
                MvxLogHost.Default?.Log(LogLevel.Trace,
                                        "No view model type finder available - assuming we are looking for a splash screen - returning null");
                return(typeof(MvxNullViewModel));
            }

            return(associatedTypeFinder.FindTypeOrNull(view.GetType()));
        }
コード例 #6
0
        private static IMvxViewModel LoadViewModel(IMvxView androidView)
        {
            var viewModelType = androidView.GetType()
                                .GetProperties()
                                .Where(p => p.Name == nameof(androidView.ViewModel))
                                .First(vmp => vmp.PropertyType != typeof(IMvxViewModel))
                                .PropertyType;

            var request   = new MvxViewModelRequest(viewModelType);
            var loader    = Mvx.Resolve <IMvxViewModelLoader>();
            var viewModel = loader.LoadViewModel(request, null);

            if (viewModel == null)
            {
                Mvx.Resolve <IMvxLog>().Warn("ViewModel not loaded for {0}", request.ViewModelType.FullName);
            }

            return(viewModel);
        }
コード例 #7
0
        public static void OnViewCreate(this IMvxView view, Func <IMvxViewModel> viewModelLoader)
        {
            // note - we check the DataContent before the ViewModel to avoid casting errors
            //       in the case of 'simple' binding code
            if (view.DataContext != null)
            {
                return;
            }

            if (view.ViewModel != null)
            {
                return;
            }

            var viewModel = viewModelLoader();

            if (viewModel == null)
            {
                MvxTrace.Warning("ViewModel not loaded for view {0}", view.GetType().Name);
                return;
            }

            view.ViewModel = viewModel;
        }
コード例 #8
0
        public static async ValueTask OnViewCreate(this IMvxView view, Func <ValueTask <IMvxViewModel?> > viewModelLoader)
        {
            // note - we check the DataContent before the ViewModel to avoid casting errors
            //       in the case of 'simple' binding code
            if (view.DataContext != null)
            {
                return;
            }

            if (view.ViewModel != null)
            {
                return;
            }

            var viewModel = await viewModelLoader().ConfigureAwait(false);

            if (viewModel == null)
            {
                MvxLog.Instance.Warn("ViewModel not loaded for view {0}", view.GetType().Name);
                return;
            }

            view.ViewModel = viewModel;
        }
コード例 #9
0
        public static IMvxViewModel ReflectionGetViewModel(this IMvxView view)
        {
            var propertyInfo = view?.GetType().GetProperty("ViewModel");

            return((IMvxViewModel)propertyInfo?.GetGetMethod().Invoke(view, new object[] { }));
        }
コード例 #10
0
        public static IMvxViewModel?ReflectionGetViewModel(this IMvxView view)
        {
            var propertyInfo = view?.GetType().GetProperty("ViewModel");

            return(propertyInfo?.GetGetMethod().Invoke(view, Array.Empty <object>()) as IMvxViewModel);
        }