Share via

Windows navigation

Eduardo Gomez Romero 1,375 Reputation points
2024-11-18T15:38:20.7133333+00:00

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);
    }

Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

Answer accepted by question author

Anonymous
2024-11-19T02:24:39.03+00:00

Hello,

If you want to open the window, you cannot use Shell.Current.GoToAsync to navigate to the window.

You can use Application.Current?.OpenWindow(new Window1()); to open it.

You can refer to this document: Multi-window support

You can set the Page1 to page property in the NewWindow1. You do not need to Routing.RegisterRoute(nameof(Window1), typeof(Window1)); in the AppShell.

public partial class NewWindow1 : Window
{
	public NewWindow1()
	{
		InitializeComponent();
		Page = new Page1();
	}
}

By the way, if you just want to open the window for windows platform.

You can use conditional compilation to do it.

#if WINDOWS

            Application.Current?.OpenWindow(new Window1());
 
#endif

Best Regards,

Leon Lu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.