Example #1
0
        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);
                    }
                }
            }));
        }
        Task CreateNavigationViewHandlerAsync(IStackNavigationView navigationView, Func <NavigationViewHandler, Task> action)
        {
            return(InvokeOnMainThreadAsync(async() =>
            {
                var context = MauiProgram.DefaultContext;

                var rootView = (context as AppCompatActivity).Window.DecorView as ViewGroup;
                var linearLayoutCompat = new LinearLayoutCompat(context);
                var fragmentManager = MauiContext.GetFragmentManager();
                var viewFragment = new NavViewFragment(MauiContext);

                try
                {
                    linearLayoutCompat.Id = View.GenerateViewId();

                    fragmentManager
                    .BeginTransaction()
                    .Add(linearLayoutCompat.Id, viewFragment)
                    .Commit();

                    rootView.AddView(linearLayoutCompat);
                    await viewFragment.FinishedLoading;
                    var handler = CreateHandler(navigationView, viewFragment.ScopedMauiContext);

                    if (navigationView is NavigationViewStub nvs && nvs.NavigationStack?.Count > 0)
                    {
                        navigationView.RequestNavigation(new NavigationRequest(nvs.NavigationStack, false));
                        await nvs.OnNavigationFinished;
                    }

                    await action(handler);
                }
                finally
                {
                    rootView.RemoveView(linearLayoutCompat);

                    fragmentManager
                    .BeginTransaction()
                    .Remove(viewFragment)
                    .Commit();
                }
            }));
        }