void OnSetImageSource(UIImage?image) { if (image == null) { PlatformView.SetImage(null, UIControlState.Normal); } else { var maxWidth = PlatformView.Frame.Width * 0.5f; var maxHeight = PlatformView.Frame.Height * 0.5f; var resizedImage = MaxResizeSwipeItemIconImage(image, maxWidth, maxHeight); try { PlatformView.SetImage(resizedImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIControlState.Normal); var tintColor = VirtualView.GetTextColor(); if (tintColor != null) { PlatformView.TintColor = tintColor.ToPlatform(); } } catch (Exception) { // UIImage ctor throws on file not found if MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure is true; MauiContext?.CreateLogger <SwipeItemMenuItemHandler>()?.LogWarning("Can not load SwipeItem Icon"); } } }
static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows) { windows.OnLaunching((app, args) => { // This is the initial Init to set up any system services registered by // Forms.Init(). This happens before any UI has appeared. // This creates a dummy MauiContext. // We need to call this so the Window and Root Page can new up successfully // The dispatcher that's inside of Forms.Init needs to be setup before the initial // window and root page start creating. // Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct // MauiContext inside Forms var services = MauiWinUIApplication.Current.Services; var mauiContext = new MauiContext(services); var state = new ActivationState(mauiContext, args); Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers }); }) .OnMauiContextCreated((mauiContext) => { // This is the final Init that sets up the real context from the application. var state = new ActivationState(mauiContext); Forms.Init(state); }); }
protected override FrameworkElement CreatePlatformView() { _navigationFrame = new WFrame(); if (VirtualView.FindParentOfType <FlyoutPage>() != null) { _navigationView = new MauiNavigationView() { Content = _navigationFrame, PaneDisplayMode = NavigationViewPaneDisplayMode.LeftMinimal, IsBackButtonVisible = NavigationViewBackButtonVisible.Collapsed, IsSettingsVisible = false, IsPaneToggleButtonVisible = false }; // Unset styles set by parent NavigationView _navigationView.SetApplicationResource("NavigationViewContentMargin", null); _navigationView.SetApplicationResource("NavigationViewMinimalHeaderMargin", null); _navigationView.SetApplicationResource("NavigationViewHeaderMargin", null); _navigationView.SetApplicationResource("NavigationViewMinimalContentGridBorderThickness", null); return(_navigationView); } _navigationRootManager = MauiContext?.GetNavigationRootManager(); return(_navigationFrame); }
protected override void DisconnectHandler(AActivity platformView) { base.DisconnectHandler(platformView); var windowManager = MauiContext.GetNavigationRootManager(); windowManager.Disconnect(); }
void UpdateDetailsFragmentView() { _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class."); var newDetailsView = VirtualView.Detail?.ToPlatform(MauiContext); if (_detailView == newDetailsView) { return; } if (newDetailsView != null) { newDetailsView.RemoveFromParent(); } _detailView = newDetailsView; if (_detailView != null) { MauiContext .GetFragmentManager() .BeginTransaction() .Replace(Resource.Id.navigationlayout_content, new ViewFragment(_detailView)) .SetReorderingAllowed(true) .Commit(); } }
private protected override void OnConnectHandler(FrameworkElement platformView) { base.OnConnectHandler(platformView); NavigationFrame.Navigated += OnNavigated; // If CreatePlatformView didn't set the NavigationView then that means we are using the // WindowRootView for our tabs if (_navigationView == null) { SetupNavigationLocals(); } else { SetupNavigationView(); } if (_navigationView == null && _navigationRootManager?.RootView is WindowRootView wrv) { wrv.ContentChanged += OnContentChanged; void OnContentChanged(object?sender, EventArgs e) { wrv.ContentChanged -= OnContentChanged; SetupNavigationLocals(); } } void SetupNavigationLocals() { _navigationRootManager = MauiContext?.GetNavigationRootManager(); _navigationView = (_navigationRootManager?.RootView as WindowRootView)?.NavigationViewControl; SetupNavigationView(); } }
static void SetupToolBar(ToolbarSettings settings, MauiContext context) { foreach (var item in settings.ToolbarItems) { if (String.IsNullOrWhiteSpace(item.AutomationId) && !String.IsNullOrWhiteSpace(item.Text)) { item.AutomationId = item.Text; } } settings.ToolBar = new AToolBar(context.Context); ToolbarExtensions.UpdateMenuItems( settings.ToolBar, settings.ToolbarItems, context, settings.TintColor, OnToolbarItemPropertyChanged, settings.MenuItemsCreated, settings.ToolbarItemsCreated ); void OnToolbarItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { settings.ToolBar.OnToolbarItemPropertyChanged(e, (ToolbarItem)sender, settings.ToolbarItems, context, settings.TintColor, OnToolbarItemPropertyChanged, settings.MenuItemsCreated, settings.ToolbarItemsCreated); } }
Task RunWindowTest(IWindow window, Func <NavigationRootManager, Task> action) { return(InvokeOnMainThreadAsync(async() => { FrameworkElement frameworkElement = null; var content = (Panel)DefaultWindow.Content; try { var scopedContext = new MauiContext(MauiContext.Services); scopedContext.AddWeakSpecific(DefaultWindow); var mauiContext = scopedContext.MakeScoped(true); var windowManager = mauiContext.GetNavigationRootManager(); windowManager.Connect(window.Content); frameworkElement = windowManager.RootView; TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>(); frameworkElement.Loaded += (_, __) => taskCompletionSource.SetResult(true); content.Children.Add(frameworkElement); await taskCompletionSource.Task; await action(windowManager); } finally { if (frameworkElement != null) { content.Children.Remove(frameworkElement); } } return Task.CompletedTask; })); }
Task CreateNavigationViewHandlerAsync(IStackNavigationView navigationView, Func <NavigationViewHandler, Task> action) { return(InvokeOnMainThreadAsync(async() => { FrameworkElement frameworkElement = null; var content = (Panel)DefaultWindow.Content; try { var mauiContext = MauiContext.MakeScoped(true); var handler = CreateHandler(navigationView, mauiContext); frameworkElement = handler.PlatformView; content.Children.Add(frameworkElement); if (navigationView is NavigationViewStub nvs && nvs.NavigationStack?.Count > 0) { navigationView.RequestNavigation(new NavigationRequest(nvs.NavigationStack, false)); await nvs.OnNavigationFinished; } await action(handler); } finally { if (frameworkElement != null) { content.Children.Remove(frameworkElement); } } })); }
public static IMauiContext MakeScoped(this IMauiContext mauiContext, LayoutInflater?layoutInflater = null, FragmentManager?fragmentManager = null, Android.Content.Context?context = null, bool registerNewNavigationRoot = false) { var scopedContext = new MauiContext(mauiContext.Services); if (layoutInflater != null) { scopedContext.AddWeakSpecific(layoutInflater); } if (fragmentManager != null) { scopedContext.AddWeakSpecific(fragmentManager); } if (context != null) { scopedContext.AddWeakSpecific(context); } if (registerNewNavigationRoot) { if (fragmentManager == null) { throw new InvalidOperationException("If you're creating a new Navigation Root you need to use a new Fragment Manager"); } scopedContext.AddWeakSpecific(new NavigationRootManager(scopedContext)); } return(scopedContext); }
AView GetCurrentRootView() { return(MauiContext ?.GetNavigationRootManager() ?.RootView ?? throw new InvalidOperationException("Current Root View cannot be null")); }
static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS) { iOS.WillFinishLaunching((app, options) => { // This is the initial Init to set up any system services registered by // Forms.Init(). This happens before any UI has appeared. // This creates a dummy MauiContext. var services = MauiUIApplicationDelegate.Current.Services; var mauiContext = new MauiContext(services); var state = new ActivationState(mauiContext); #pragma warning disable CS0612 // Type or member is obsolete Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers }); #pragma warning restore CS0612 // Type or member is obsolete return(true); }) .OnMauiContextCreated((mauiContext) => { // This is the final Init that sets up the real context from the application. var state = new ActivationState(mauiContext); #pragma warning disable CS0612 // Type or member is obsolete Forms.Init(state); #pragma warning restore CS0612 // Type or member is obsolete }); }
static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android) { android .OnApplicationCreating((app) => { // This is the initial Init to set up any system services registered by // Forms.Init(). This happens in the Application's OnCreate - before // any UI has appeared. // This creates a dummy MauiContext that wraps the Application. var services = MauiApplication.Current.Services; var mauiContext = new MauiContext(services, app); var state = new ActivationState(mauiContext); Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers }); }) .OnMauiContextCreated((mauiContext) => { // This is the final Init that sets up the real context from the activity. var state = new ActivationState(mauiContext); Forms.Init(state); }); }
internal async Task FirstLoadUrlAsync(string url) { try { var uri = new Uri(url); var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute); var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative); NSUrlRequest request = new NSUrlRequest(new Uri(safeHostUri, safeRelativeUri)); if (HasCookiesToLoad(url) && !PlatformVersion.IsAtLeast(11)) { return; } await SyncPlatformCookiesAsync(url); PlatformView?.LoadRequest(request); } catch (UriFormatException) { // If we got a format exception trying to parse the URI, it might be because // someone is passing in a local bundled file page. If we can find a better way // to detect that scenario, we should use it; until then, we'll fall back to // local file loading here and see if that works: if (!LoadFile(url)) { MauiContext?.CreateLogger <WebViewHandler>()?.LogWarning($"Unable to Load Url {url}"); } } catch (Exception) { MauiContext?.CreateLogger <WebViewHandler>()?.LogWarning($"Unable to Load Url {url}"); } }
public MauiContextTests() { _mauiApp = MauiApp .CreateBuilder() .Build(); _mauiContext = new MauiContext(_mauiApp.Services, Platform.DefaultContext); }
void UpdateContent() { _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class."); var rootManager = MauiContext.GetNavigationRootManager(); rootManager.Connect(VirtualView.Content); }
public static IMauiContext MakeScopededArgs <TArgs>(this IMauiContext mauiContext, TArgs args) where TArgs : class { var scopedContext = new MauiContext(mauiContext.Services); scopedContext.AddWeakSpecific(args); return(scopedContext); }
protected override void DisconnectHandler(UI.Xaml.Window platformView) { var windowManager = MauiContext.GetNavigationRootManager(); var rootPanel = platformView.Content as WPanel; rootPanel.Children.Remove(windowManager.RootView); windowManager.Disconnect(); base.DisconnectHandler(platformView); }
public static IMauiContext MakeScoped(this IMauiContext mauiContext, bool registerNewNavigationRoot) { var scopedContext = new MauiContext(mauiContext.Services); if (registerNewNavigationRoot) { scopedContext.AddWeakSpecific(new NavigationRootManager(scopedContext.GetPlatformWindow())); } return(scopedContext); }
protected override void DisconnectHandler(UI.Xaml.Window platformView) { MauiContext ?.GetNavigationRootManager() ?.Disconnect(); _rootPanel?.Children?.Clear(); platformView.Content = null; base.DisconnectHandler(platformView); }
protected override void DisconnectHandler(UI.Xaml.Window nativeView) { var windowManager = MauiContext?.GetNavigationRootManager(); windowManager?.Disconnect(VirtualView.Content); _rootPanel?.Children?.Clear(); nativeView.Content = null; base.DisconnectHandler(nativeView); }
void BackClick() { if (VirtualView.BackButtonVisible && VirtualView.IsVisible) { _ = MauiContext?.GetActivity().GetWindow()?.BackButtonClicked(); } else { _drawerLayout?.Open(); } }
public void HandlerNotFoundExceptionWhenHandlerNotRegistered() { var mauiApp = MauiApp.CreateBuilder() .Build(); var mauiContext = new MauiContext(mauiApp.Services); Assert.Throws <HandlerNotFoundException>(() => new ViewStub().ToPlatform(mauiContext) ); }
protected override View CreateNativeView() { LayoutInflater?li = MauiContext?.GetLayoutInflater(); _ = li ?? throw new InvalidOperationException($"LayoutInflater cannot be null"); var view = li.Inflate(Resource.Layout.fragment_backstack, null).JavaCast <FragmentContainerView>(); _ = view ?? throw new InvalidOperationException($"Resource.Layout.navigationlayout view not found"); return(view); }
protected override DrawerLayout CreateNativeView() { var li = MauiContext?.GetLayoutInflater(); _ = li ?? throw new InvalidOperationException($"LayoutInflater cannot be null"); var dl = li.Inflate(Resource.Layout.drawer_layout, null) .JavaCast <DrawerLayout>() ?? throw new InvalidOperationException($"Resource.Layout.drawer_layout missing"); return(dl); }
private protected override void OnDisconnectHandler(object platformView) { base.OnDisconnectHandler(platformView); if (platformView is MauiToolbar mauiToolbar) { var navRootManager = MauiContext?.GetNavigationRootManager(); if (navRootManager?.ToolBar == mauiToolbar) { navRootManager.SetToolbar(null); } } }
Task RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action) where THandler : class, IElementHandler { return(InvokeOnMainThreadAsync(async() => { AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup; var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context); var fragmentManager = MauiContext.GetFragmentManager(); var viewFragment = new WindowTestFragment(MauiContext, window); try { linearLayoutCompat.Id = AView.GenerateViewId(); fragmentManager .BeginTransaction() .Add(linearLayoutCompat.Id, viewFragment) .Commit(); rootView.AddView(linearLayoutCompat); await viewFragment.FinishedLoading; if (typeof(THandler).IsAssignableFrom(window.Handler.GetType())) { await action((THandler)window.Handler); } else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType())) { await action((THandler)window.Content.Handler); } } finally { if (window.Handler != null) { window.Handler.DisconnectHandler(); } rootView.RemoveView(linearLayoutCompat); fragmentManager .BeginTransaction() .Remove(viewFragment) .Commit(); await linearLayoutCompat.OnUnloadedAsync(); if (viewFragment.View != null) { await viewFragment.View.OnUnloadedAsync(); } } })); }
public void AddSpecificIsNotWeak() { var collection = new MauiServiceCollection(); var services = new MauiFactory(collection); var context = new MauiContext(services); DoAdd(context); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.NotNull(context.Services.GetService <TestThing>());
Task RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action) where THandler : class, IElementHandler { return(InvokeOnMainThreadAsync(async() => { var testingRootPanel = (WPanel)MauiProgram.CurrentWindow.Content; IElementHandler newWindowHandler = null; NavigationRootManager navigationRootManager = null; try { var scopedContext = new MauiContext(MauiContext.Services); scopedContext.AddWeakSpecific(MauiProgram.CurrentWindow); var mauiContext = scopedContext.MakeScoped(true); navigationRootManager = mauiContext.GetNavigationRootManager(); navigationRootManager.UseCustomAppTitleBar = false; newWindowHandler = window.ToHandler(mauiContext); var content = window.Content.Handler.ToPlatform(); await content.OnLoadedAsync(); await Task.Delay(10); if (typeof(THandler).IsAssignableFrom(newWindowHandler.GetType())) { await action((THandler)newWindowHandler); } else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType())) { await action((THandler)window.Content.Handler); } } finally { if (navigationRootManager != null) { navigationRootManager.Disconnect(); } if (newWindowHandler != null) { newWindowHandler.DisconnectHandler(); } // Set the root window panel back to the testing panel if (testingRootPanel != null && MauiProgram.CurrentWindow.Content != testingRootPanel) { MauiProgram.CurrentWindow.Content = testingRootPanel; await testingRootPanel.OnLoadedAsync(); await Task.Delay(10); } } })); }
public void ExceptionPropagatesFromHandler() { var mauiApp = MauiApp.CreateBuilder() .ConfigureMauiHandlers(handlers => handlers.AddHandler <IViewStub, ViewHandlerWithChildException>()) .ConfigureMauiHandlers(handlers => handlers.AddHandler <ExceptionThrowingStub, ExceptionThrowingViewHandler>()) .Build(); var mauiContext = new MauiContext(mauiApp.Services); Assert.Throws <PlatformViewStubCreatePlatformViewException>(() => new ViewStub().ToPlatform(mauiContext) ); }