Esempio n. 1
0
        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);
        }
        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;
            }));
        }
Esempio n. 3
0
        public static IMauiContext MakeScopededArgs <TArgs>(this IMauiContext mauiContext, TArgs args)
            where TArgs : class
        {
            var scopedContext = new MauiContext(mauiContext.Services);

            scopedContext.AddWeakSpecific(args);
            return(scopedContext);
        }
Esempio n. 4
0
        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);
        }
        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);
                    }
                }
            }));
        }
Esempio n. 6
0
        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();

                    MauiContext
                    .Services
                    .GetRequiredService <WWindow>()
                    .SetWindowHandler(window, mauiContext);

                    newWindowHandler = window.Handler;
                    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);
                    }
                    else if (window.Content is ContentPage cp && typeof(THandler).IsAssignableFrom(cp.Content.Handler.GetType()))
                    {
                        await action((THandler)cp.Content.Handler);
                    }
                    else
                    {
                        throw new Exception($"I can't work with {typeof(THandler)}");
                    }
                }
                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);
                    }

                    // reset the appbar title to our test runner
                    MauiProgram
                    .CurrentWindow
                    .GetWindow()
                    .Handler
                    .MauiContext
                    .GetNavigationRootManager()
                    .UpdateAppTitleBar(true);
                }
            }));