protected virtual void InjectProperty(object toReturn, PropertyInfo injectableProperty, IMvxPropertyInjectorOptions options) { object propertyValue; if (Mvx.TryResolve(injectableProperty.PropertyType, out propertyValue)) { try { injectableProperty.SetValue(toReturn, propertyValue, null); } catch (TargetInvocationException invocation) { throw new MvxIoCResolveException(invocation, "Failed to inject into {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } } else { if (options.ThrowIfPropertyInjectionFails) { throw new MvxIoCResolveException("IoC property injection failed for {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } else { MvxLog.Instance.Warn("IoC property injection skipped for {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } } }
public MvxResizedImageViewLoader( Func <UIImageView> imageViewAccess, Func <CGRect> imageFrameAccess, Action <ImageState> afterImageChangeAction = null) { _imageFrameAccess = imageFrameAccess; _imageSetAction = (image, state) => { OnImage(imageViewAccess(), image); if (afterImageChangeAction != null) { UIApplication.SharedApplication .InvokeOnMainThread(() => afterImageChangeAction(state)); } }; if (!Mvx.TryResolve(out _imageHelper)) { MvxBindingTrace.Error("Can't resolve IMvxResizedImageHelper"); return; } var eventInfo = _imageHelper.GetType().GetEvent("ImageChanged"); _subscription = eventInfo.WeakSubscribe <UIImage>(_imageHelper, ImageHelperOnImageChanged); }
public void TryResolve_WithAttrInjection_AttrMarkedProperiesGetInjected() { MvxSingleton.ClearAllSingletons(); var options = new MvxIocOptions { PropertyInjectorOptions = new MvxPropertyInjectorOptions() { InjectIntoProperties = MvxPropertyInjection.MvxInjectInterfaceProperties } }; var instance = MvxSimpleIoCContainer.Initialize(options); Mvx.RegisterType <IA, A>(); Mvx.RegisterType <IB, B>(); Mvx.RegisterType <IC, C>(); IA a; var result = Mvx.TryResolve(out a); Assert.IsTrue(result); Assert.IsNotNull(a); Assert.IsInstanceOf <A>(a); var castA = (A)a; Assert.IsNotNull(castA.B); Assert.IsInstanceOf <B>(castA.B); Assert.IsNull(castA.C); Assert.IsNull(castA.BNever); Assert.IsNull(castA.CNever); }
public virtual void ChangePresentation(MvxPresentationHint hint) { if (hint is MvxClosePresentationHint) { IMvxControlFinder finder; if (Mvx.TryResolve(out finder)) { var control = finder.GetControl((hint as MvxClosePresentationHint).ViewModelToClose); if (control != null) { control.ViewModel = null; } else { _viewPresenter.ChangePresentation(hint); } } else { _viewPresenter.ChangePresentation(hint); } } else { _viewPresenter.ChangePresentation(hint); } }
private Dictionary <string, Type> CreateFragmentTypesDictionary(Bundle outState) { IMvxSavedStateConverter savedStateConverter; if (!Mvx.TryResolve(out savedStateConverter)) { return(null); } var typesForKeys = new Dictionary <string, Type>(); foreach (var item in _lookup) { var fragment = item.Value.CachedFragment as IMvxFragmentView; if (fragment == null) { continue; } var mvxBundle = fragment.CreateSaveStateBundle(); var bundle = new Bundle(); savedStateConverter.Write(bundle, mvxBundle); outState.PutBundle(item.Key, bundle); typesForKeys.Add(item.Key, item.Value.ViewModelType); } return(typesForKeys); }
protected override void HandleSaveInstanceStateCalled(object sender, MvxValueEventArgs <Bundle> bundleArgs) { // it is guarannted that SaveInstanceState call will be executed before OnStop (thus before Fragment detach) // it is safe to assume that Fragment has activity attached if (!FragmentView.GetType().IsFragmentCacheable(Fragment.Activity.GetType())) { return; } var mvxBundle = FragmentView.CreateSaveStateBundle(); if (mvxBundle != null) { IMvxSavedStateConverter converter; if (!Mvx.TryResolve(out converter)) { MvxTrace.Warning("Saved state converter not available - saving state will be hard"); } else { converter.Write(bundleArgs.Value, mvxBundle); } } var cache = Mvx.Resolve <IMvxMultipleViewModelCache>(); cache.Cache(FragmentView.ViewModel, FragmentView.UniqueImmutableCacheTag); }
protected override void OnCreate(Bundle bundle) { // Prevents crash when activity in background with history enable is reopened after // Android does some auto memory management. var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(this); setup.EnsureInitialized(); base.OnCreate(bundle); if (bundle == null) { HandleIntent(Intent); } else { IMvxJsonConverter serializer; if (!Mvx.TryResolve(out serializer)) { Mvx.Trace( "Could not resolve IMvxJsonConverter, it is going to be hard to create ViewModel cache"); return; } FragmentCacheConfiguration.RestoreCacheConfiguration(bundle, serializer); // Gabriel has blown his trumpet. Ressurect Fragments from the dead RestoreFragmentsCache(); RestoreViewModelsFromBundle(serializer, bundle); } }
public void TryResolve_WithFullInjection_AllInterfaceProperiesGetInjected() { MvxSingleton.ClearAllSingletons(); var options = new MvxIocOptions { PropertyInjectorOptions = new MvxPropertyInjectorOptions() { InjectIntoProperties = MvxPropertyInjection.AllInterfaceProperties } }; var instance = MvxIoCProvider.Initialize(options); Mvx.RegisterType <IA, A>(); Mvx.RegisterType <IB, B>(); Mvx.RegisterType <IC, C>(); IA a; var result = Mvx.TryResolve(out a); Assert.True(result); Assert.NotNull(a); Assert.IsType <A>(a); var castA = (A)a; Assert.NotNull(castA.B); Assert.IsType <B>(castA.B); Assert.NotNull(castA.C); Assert.IsType <C>(castA.C); Assert.Null(castA.BNever); Assert.Null(castA.CNever); }
protected override void HandleSaveInstanceStateCalled(object sender, MvxValueEventArgs <Bundle> bundleArgs) { if (!FragmentView.GetType().IsOwnedViewModelFragment()) { return; } var mvxBundle = FragmentView.CreateSaveStateBundle(); if (mvxBundle != null) { IMvxSavedStateConverter converter; if (!Mvx.TryResolve(out converter)) { MvxTrace.Warning("Saved state converter not available - saving state will be hard"); } else { converter.Write(bundleArgs.Value, mvxBundle); } } var cache = Mvx.Resolve <IMvxMultipleViewModelCache>(); cache.Cache(FragmentView.ViewModel); }
public MvxCommandBase() { if (!Mvx.TryResolve <IMvxCommandHelper>(out _commandHelper)) { _commandHelper = new MvxWeakCommandHelper(); } }
private Dictionary <string, Type> CreateFragmentTypesDictionary(Bundle outState) { IMvxSavedStateConverter savedStateConverter; if (!Mvx.TryResolve(out savedStateConverter)) { return(null); } var typesForKeys = new Dictionary <string, Type>(); var currentFragsInfo = GetCurrentCacheableFragmentsInfo(); foreach (var info in currentFragsInfo) { var fragment = info.CachedFragment as IMvxFragmentView; if (fragment == null) { continue; } var mvxBundle = fragment.CreateSaveStateBundle(); var bundle = new Bundle(); savedStateConverter.Write(bundle, mvxBundle); outState.PutBundle(info.Tag, bundle); if (!typesForKeys.ContainsKey(info.Tag)) { typesForKeys.Add(info.Tag, info.ViewModelType); } } return(typesForKeys); }
public MvxImageView(Context context, IAttributeSet attrs) : base(context, attrs) { if (!Mvx.TryResolve(out _imageHelper)) { MvxTrace.Error( "No IMvxImageHelper registered - you must provide an image helper before you can use a MvxImageView"); } else { _imageHelper.ImageChanged += ImageHelperOnImageChanged; } var typedArray = context.ObtainStyledAttributes(attrs, MvxAndroidBindingResource.Instance .ImageViewStylableGroupId); int numStyles = typedArray.IndexCount; for (var i = 0; i < numStyles; ++i) { int attributeId = typedArray.GetIndex(i); if (attributeId == MvxAndroidBindingResource.Instance.SourceBindId) { ImageUrl = typedArray.GetString(attributeId); } } typedArray.Recycle(); }
protected override void HandleCreateCalled(object sender, MvxValueEventArgs <Bundle> bundleArgs) { FragmentView.EnsureSetupInitialized(); // Create is called after Fragment is attached to Activity // it's safe to assume that Fragment has activity if (!FragmentView.GetType().IsFragmentCacheable(Fragment.Activity.GetType())) { return; } FragmentView.RegisterFragmentViewToCacheIfNeeded(Fragment.Activity.GetType()); Bundle bundle = null; MvxViewModelRequest request = null; if (bundleArgs?.Value != null) { // saved state bundle = bundleArgs.Value; } else { var fragment = FragmentView as Fragment; if (fragment?.Arguments != null) { bundle = fragment.Arguments; var json = bundle.GetString("__mvxViewModelRequest"); if (!string.IsNullOrEmpty(json)) { IMvxNavigationSerializer serializer; if (!Mvx.TryResolve(out serializer)) { MvxTrace.Warning( "Navigation Serializer not available, deserializing ViewModel Request will be hard"); } else { request = serializer.Serializer.DeserializeObject <MvxViewModelRequest>(json); } } } } IMvxSavedStateConverter converter; if (!Mvx.TryResolve(out converter)) { MvxTrace.Warning("Saved state converter not available - saving state will be hard"); } else { if (bundle != null) { var mvxBundle = converter.Read(bundle); FragmentView.OnCreate(mvxBundle, request); } } }
void LaunchHockeyAppFeedback() { IHockeyAppFeedbackService hockeyAppFeedbackService; if (Mvx.TryResolve <IHockeyAppFeedbackService>(out hockeyAppFeedbackService)) { hockeyAppFeedbackService.LaunchHockeyAppFeedback(); } }
protected override void HandleCreateCalled(object sender, MvxValueEventArgs <Bundle> bundleArgs) { FragmentView.EnsureSetupInitialized(); if (!FragmentView.GetType().IsOwnedViewModelFragment()) { return; } Bundle bundle = null; MvxViewModelRequest request = null; if (bundleArgs != null && bundleArgs.Value != null) { // saved state bundle = bundleArgs.Value; } else { var fragment = FragmentView as Fragment; if (fragment != null && fragment.Arguments != null) { bundle = fragment.Arguments; var json = bundle.GetString("__mvxViewModelRequest"); if (!string.IsNullOrEmpty(json)) { IMvxNavigationSerializer serializer; if (!Mvx.TryResolve(out serializer)) { MvxTrace.Warning( "Navigation Serializer not available, deserializing ViewModel Request will be hard"); } else { request = serializer.Serializer.DeserializeObject <MvxViewModelRequest>(json); } } } } IMvxSavedStateConverter converter; if (!Mvx.TryResolve(out converter)) { MvxTrace.Warning("Saved state converter not available - saving state will be hard"); } else { if (bundle != null) { var mvxBundle = converter.Read(bundle); FragmentView.OnCreate(mvxBundle, request); } } }
public static Context GetActivityContext() { IMvxAndroidCurrentTopActivity topActivity; bool canResolve = Mvx.TryResolve(out topActivity); if (canResolve) { return(topActivity.Activity); } return(null); }
private static IMvxBindingCreator ResolveBindingCreator() { IMvxBindingCreator toReturn; if (!Mvx.TryResolve <IMvxBindingCreator>(out toReturn)) { throw new MvxException("Unable to resolve the binding creator - have you initialized Xamarin Forms Binding"); } return(toReturn); }
public MvxCommandBase() { if (!Mvx.TryResolve <IMvxCommandHelper>(out _commandHelper)) { _commandHelper = new MvxWeakCommandHelper(); } var alwaysOnUIThread = MvxSingletonCache.Instance == null || MvxSingletonCache.Instance.Settings.AlwaysRaiseInpcOnUserInterfaceThread; ShouldAlwaysRaiseCECOnUserInterfaceThread = alwaysOnUIThread; }
public static IMvxFileStore SafeGetFileStore() { IMvxFileStore toReturn; if (Mvx.TryResolve(out toReturn)) { return(toReturn); } throw new MvxException("You must call EnsureLoaded on the File plugin before using the DownloadCache"); }
public static IMvxImageCache <UIImage> SafeGetImageCache() { IMvxImageCache <UIImage> imageCache; if (Mvx.TryResolve(out imageCache)) { return(imageCache); } throw new MvxException("You must call EnsureLoaded on the File plugin before using the DownloadCache"); }
public async Task Init() { IProfileService profileService; Mvx.TryResolve(out profileService); _progressLoaderService = Mvx.Resolve <IProgressLoaderService>(); Mvx.TryResolve(out _dataLoaderService); _user = await profileService.GetUser(); ListFriends = await profileService.GetFriends(); }
private static Type GetActivityViewModelType(Type activityType) { 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(activityType)); }
protected MvxBaseImageViewLoader(Action <TImage> imageSetAction) { _imageSetAction = imageSetAction; if (!Mvx.TryResolve(out _imageHelper)) { MvxBindingTrace.Error( "Unable to resolve the image helper - have you referenced and called EnsureLoaded on the DownloadCache plugin?"); return; } var eventInfo = _imageHelper.GetType().GetEvent("ImageChanged"); _subscription = eventInfo.WeakSubscribe <TImage>(_imageHelper, ImageHelperOnImageChanged); }
public MvxImageView(Context context) : base(context) { if (!Mvx.TryResolve(out _imageHelper)) { MvxTrace.Error( "No IMvxImageHelper registered - you must provide an image helper before you can use a MvxImageView"); } else { _imageHelper.ImageChanged += ImageHelperOnImageChanged; } }
public static void Resolves_unsuccessfully_when_registered_open_generic_with_one_generic_parameter_that_was_not_registered() { var instance = MvxIoCProvider.Initialize(); ((MvxIoCProvider)instance).CleanAllResolvers(); IOG <C2> toResolve = null; var isResolved = Mvx.TryResolve <IOG <C2> >(out toResolve); Assert.False(isResolved); Assert.Null(toResolve); }
private static bool ReadIsTvosVersionOrHigher(int target, bool defaultValue) { IMvxTvosSystem tvosSystem; Mvx.TryResolve <IMvxTvosSystem>(out tvosSystem); if (tvosSystem == null) { MvxLog.Instance.Warn("IMvxTvosSystem not found - so assuming we {1} on tvOS {0} or later", target, defaultValue ? "are" : "are not"); return(defaultValue); } return(tvosSystem.Version.Major >= target); }
private static void RestoreViewModelsFromBundle(IMvxJsonConverter serializer, Bundle savedInstanceState) { IMvxSavedStateConverter savedStateConverter; IMvxMultipleViewModelCache viewModelCache; IMvxViewModelLoader viewModelLoader; if (!Mvx.TryResolve(out savedStateConverter)) { Mvx.Trace("Could not resolve IMvxSavedStateConverter, won't be able to convert saved state"); return; } if (!Mvx.TryResolve(out viewModelCache)) { Mvx.Trace("Could not resolve IMvxMultipleViewModelCache, won't be able to convert saved state"); return; } if (!Mvx.TryResolve(out viewModelLoader)) { Mvx.Trace("Could not resolve IMvxViewModelLoader, won't be able to load ViewModel for caching"); return; } // Harder ressurection, just in case we were killed to death. var json = savedInstanceState.GetString(SavedFragmentTypesKey); if (string.IsNullOrEmpty(json)) { return; } var savedState = serializer.DeserializeObject <Dictionary <string, Type> >(json); foreach (var item in savedState) { var bundle = savedInstanceState.GetBundle(item.Key); if (bundle.IsEmpty) { continue; } var mvxBundle = savedStateConverter.Read(bundle); var request = MvxViewModelRequest.GetDefaultRequest(item.Value); // repopulate the ViewModel with the SavedState and cache it. var vm = viewModelLoader.LoadViewModel(request, mvxBundle); viewModelCache.Cache(vm, item.Key); } }
public static Page CreatePage(MvxViewModelRequest request) { IMvxFormsPageLoader viewPageLoader; Mvx.TryResolve(out viewPageLoader); if (viewPageLoader == null) { viewPageLoader = new MvxFormsPageLoader(); Mvx.RegisterSingleton(viewPageLoader); } var page = viewPageLoader.LoadPage(request); return(page); }
public void OnClick(GameObject go) { if (Target != null && _currentCommand != null) { IMvxSound sound; if (Mvx.TryResolve <IMvxSound>(out sound)) { sound.Play("ButtonClick"); } _currentCommand.Execute(null); } }
protected override async void OnCreate(Bundle bundle) { try { base.OnCreate(bundle); ViewModel.Title = "Расписание"; Title = ViewModel.Title; _drawerLayout = FindViewById <DrawerLayout>(Resource.Id.drawer_layout); SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu); var config = ImageLoaderConfiguration.CreateDefault(ApplicationContext); if (!ImageLoader.Instance.IsInited) { ImageLoader.Instance.Init(config); } } catch (Exception ex) { var m = ex.Message; } //// Initialize ImageLoader with configuration. var imageLoader = ImageLoader.Instance; var headerMenu = FindViewById <NavigationView>(Resource.Id.nav_view).GetHeaderView(0); var photoUser = headerMenu.FindViewById <ImageView>(Resource.Id.user_image); var nameUser = headerMenu.FindViewById <TextView>(Resource.Id.user_name); await ViewModel.Initialize() .ContinueWith( task => { RunOnUiThread( () => { imageLoader.DisplayImage(ViewModel?.CurrentUser?.photo_100, photoUser); }); }); IProfileService profileService; Mvx.TryResolve(out profileService); var user = await profileService.GetUser(); nameUser.Text = user.first_name + " " + user.last_name; imageLoader.DisplayImage(user.photo_100, photoUser); _navigationView = FindViewById <NavigationView>(Resource.Id.nav_view); _navigationView.NavigationItemSelected += NavigationViewOnNavigationItemSelected; }