private static IMvxViewModel LoadViewModel(this IMvxElement element)
        {
            if (MvxDesignTimeChecker.IsDesignTime)
            {
                return(new MvxNullViewModel());
            }

            var viewModelType = element.FindAssociatedViewModelTypeOrNull();

            if (viewModelType == typeof(MvxNullViewModel))
            {
                return(new MvxNullViewModel());
            }

            if (viewModelType == null ||
                viewModelType == typeof(IMvxViewModel))
            {
                MvxFormsLog.Instance.Trace("No ViewModel class specified for {0} in LoadViewModel",
                                           element.GetType().Name);
                return(null);
            }

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

            if (viewModel == null)
            {
                throw new MvxException("ViewModel not loaded for " + viewModelType);
            }
            return(viewModel);
        }
예제 #2
0
        public static void OnViewAppearing(this IMvxElement element)
        {
            var cache  = Mvx.Resolve <IMvxChildViewModelCache>();
            var cached = cache.Get(element.FindAssociatedViewModelTypeOrNull());

            element.OnViewCreate(() => cached ?? element.LoadViewModel());
        }
        private static void LoadViewModelForElement(IMvxElement element)
        {
            IMvxViewModel cached = null;

            if (!MvxDesignTimeChecker.IsDesignTime)
            {
                var cache = Mvx.IoCProvider.Resolve <IMvxChildViewModelCache>();
                cached = cache.Get(element.FindAssociatedViewModelTypeOrNull());
            }

            element.OnViewCreate(() => cached ?? element.LoadViewModel());
        }
예제 #4
0
        private static async ValueTask LoadViewModelForElementAsync(IMvxElement element)
        {
            IMvxViewModel?cached = null;

            if (!MvxDesignTimeChecker.IsDesignTime)
            {
                var cache = Mvx.IoCProvider.Resolve <IMvxChildViewModelCache>();
                cached = cache.Get(element.FindAssociatedViewModelTypeOrNull());
            }

            await element.OnViewCreate(async() =>
            {
                return(cached ?? await element.LoadViewModel().ConfigureAwait(false));
            }
                                       ).ConfigureAwait(false);
        }