|
| 1 | +// Copyright (c) 2009-2026 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using ReactiveUI.Maui; |
| 7 | +using Splat; |
| 8 | + |
| 9 | +namespace ReactiveUI.Documentation.PlatformMaui; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Shows the two ways to host a view registered only with the service locator: the Unsafe twin of each host, or a |
| 13 | +/// <c>MapFromServiceLocator</c> entry that the default hosts read. |
| 14 | +/// </summary> |
| 15 | +public static class ServiceLocatorViewExamples |
| 16 | +{ |
| 17 | + /// <summary>The default <see cref="ViewModelViewHost"/> throws for this view; <see cref="ViewModelViewHostUnsafe"/> finds it.</summary> |
| 18 | + public static void ViewModelViewHostUnsafeFindsTheView() |
| 19 | + { |
| 20 | + AppLocator.CurrentMutable.Register<IViewFor<ShoppingListViewModel>>(static () => new ShoppingListView()); |
| 21 | + ShoppingListViewModel shopping = new(new RecipeBookScreen()); |
| 22 | + |
| 23 | + ViewModelViewHost host = new(); |
| 24 | + try |
| 25 | + { |
| 26 | + host.ViewModel = shopping; |
| 27 | + } |
| 28 | + catch (InvalidOperationException exception) |
| 29 | + { |
| 30 | + Console.WriteLine(exception.GetType().Name); |
| 31 | + } |
| 32 | + |
| 33 | + ViewModelViewHostUnsafe unsafeHost = new() { ViewModel = shopping }; |
| 34 | + Console.WriteLine(unsafeHost.Content?.GetType().Name); |
| 35 | + |
| 36 | + // Output: |
| 37 | + // InvalidOperationException |
| 38 | + // ShoppingListView |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary><see cref="RoutedViewHostUnsafe"/> pushes a page registered only with the service locator.</summary> |
| 42 | + /// <returns>A task that completes once the navigation has run.</returns> |
| 43 | + public static async Task RoutedViewHostUnsafePushesThePage() |
| 44 | + { |
| 45 | + RecipeBookScreen screen = new(); |
| 46 | + AppLocator.CurrentMutable.RegisterConstant<IScreen>(screen); |
| 47 | + AppLocator.CurrentMutable.Register<IViewFor<ShoppingListViewModel>>(static () => new ShoppingListPage()); |
| 48 | + |
| 49 | + RoutedViewHostUnsafe host = new(); |
| 50 | + _ = await screen.Router.Navigate.Execute(new ShoppingListViewModel(screen)); |
| 51 | + |
| 52 | + Console.WriteLine(host.Navigation.NavigationStack[0].GetType().Name); |
| 53 | + |
| 54 | + // Output: |
| 55 | + // ShoppingListPage |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// <c>MapFromServiceLocator</c> adds the service locator's view to the view locator's <c>Map</c> entries, so the |
| 60 | + /// default <see cref="ViewModelViewHost"/> finds it and the app stays safe to compile ahead of time. |
| 61 | + /// </summary> |
| 62 | + public static void MapFromServiceLocatorLetsTheDefaultHostFindTheView() |
| 63 | + { |
| 64 | + AppLocator.CurrentMutable.Register<IViewFor<ShoppingListViewModel>>(static () => new ShoppingListView()); |
| 65 | + DefaultViewLocator locator = new(); |
| 66 | + _ = locator.CreateMappingBuilder().MapFromServiceLocator<ShoppingListViewModel, IViewFor<ShoppingListViewModel>>(); |
| 67 | + |
| 68 | + ViewModelViewHost host = new() { ViewLocator = locator, ViewModel = new ShoppingListViewModel(new RecipeBookScreen()) }; |
| 69 | + |
| 70 | + Console.WriteLine(host.Content?.GetType().Name); |
| 71 | + |
| 72 | + // Output: |
| 73 | + // ShoppingListView |
| 74 | + } |
| 75 | +} |
0 commit comments