in .net 9, we have a new control named Window.TitleBar, so now we can play with some pages on Mobile and windows on Desktop
I created
Page1
Window1
I register them both
public partial class AppShell : Shell {
public AppShell() {
InitializeComponent();
Routing.RegisterRoute(nameof(Window1), typeof(Window1));
Routing.RegisterRoute(nameof(Page1), typeof(Page1));
}
and created an implementation of an interface, that will help me navigate
public async Task NavigateTo(string baseRoute, bool animated = true) {
// Generate the dynamic route based on the platform
string route = baseRoute;
// If the device is Desktop (Windows), append "Windows" to the base route (e.g., "Page1" becomes "Windows1")
if (DeviceInfo.Idiom == DeviceIdiom.Desktop) {
route = $"Window{baseRoute.Substring(4)}"; // Assuming the base route starts with "Page" (e.g., "Page1" => "Windows1")
}
/
// Now navigate to the dynamically generated route
await Shell.Current.GoToAsync(route, animated);
}
For mobile is navigation fne
but for wndows I get
- $exception {"Object reference not set to an instance of an object."} System.NullReferenceException
+ Data Count = 0 System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
HResult -2147467261 int
HasBeenThrown true bool
HelpLink null string
+ InnerException null System.Exception
Message "Object reference not set to an instance of an object." string
SerializationStackTraceString " at Microsoft.Maui.Controls.ShellNavigationManager.ApplyQueryAttributes(Element element, ShellRouteParameters query, Boolean isLastItem, Boolean isPopping)" string
SerializationWatsonBuckets null object
Source "Microsoft.Maui.Controls" string
StackTrace " at Microsoft.Maui.Controls.ShellNavigationManager.ApplyQueryAttributes(Element element, ShellRouteParameters query, Boolean isLastItem, Boolean isPopping)" string
+ TargetSite {Void ApplyQueryAttributes(Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls.ShellRouteParameters, Boolean, Boolean)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
_HResult -2147467261 int
+ _data Count = 0 System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
_exceptionMethod null System.Reflection.MethodBase
_helpURL null string
+ _innerException null System.Exception
_ipForWatsonBuckets 0x00007fff0730abbf System.UIntPtr
_message "Object reference not set to an instance of an object." string
_remoteStackTraceString null string
_source null string
+ _stackTrace {sbyte[48]} object {sbyte[]}
_stackTraceString null string
_watsonBuckets null byte[]
_xcode -532462766 int
_xptrs 0x0000000000000000 System.IntPtr
+ Static members
here is the viewModel
public partial class MainPageViewModel(IAppService appService) : ObservableObject {
[RelayCommand]
async Task nav() {
string route = "Page1"; // This can be dynamically determined based on some conditions if needed
// Call the AppService to perform the navigation (platform-specific logic happens inside AppServices)
await appService.NavigateTo(route);
}