Navigation with Shell 在 maui c# 中不工作得很好。

huangapple go评论58阅读模式
英文:

Navigation with Shell don't work well maui c#

问题

我在Maui中创建了一个导航项目,我可以在第一页和第二页之间来回导航,但是当我从第二页到第三页时,无法返回到上一页,返回箭头也不起作用。在下一页甚至会抛出异常并停在那里。我想要能够在页面之间导航,但我不理解通过Shell进行导航。

Main Page.xaml
<Button Text="转到第一页"
                Command="{Binding GoPageACommand}"/>
        <Button Text="转到第二页"
                Command="{Binding GoPageBCommand}"/>

        <Button Text="转到第三页"
                Command="{Binding GoPageCCommand}"/>
MainViewModel
[RelayCommand]
 async void GoPageA()
        {
            try
            {
                await Shell.Current.GoToAsync(nameof(Page1));
            }catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

 [RelayCommand]
 async void GoPageB()
        {
             await Shell.Current.GoToAsync(nameof(Page2));
        }
[RelayCommand]
 async void GoPageC()
        {
            await Shell.Current.GoToAsync(nameof(Page3));
        }
Page1.xaml
<Button Text="转到第二页"
                Command="{Binding GotoPage2Command}"/>
        <Button Text="转到第三页"
                Command="{Binding GotoPage3Command}"/>
        <Button Text="返回"
                Command="{Binding GoBackCommand}"/>
Page1ViewModel.cs
[RelayCommand]
        async void GotoPage2()
        {
            await Shell.Current.GoToAsync(nameof(Page2));
        }

        [RelayCommand]
        async void GotoPage3()
        {
            await Shell.Current.GoToAsync(nameof(Page3));

        }

        [RelayCommand]
        async void GoBack()
        {
            await Shell.Current.GoToAsync("..");

        }
Page2.xaml
<Button Text="返回"
                Command="{Binding GoBackCommand}"/>
Page2ViewModel.cs
[RelayCommand]
        async void GoBack()
        {
            await Shell.Current.GoToAsync("..");
        }
AppShell.xaml
<ShellContent Title="主页" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>

AppShell.xaml.cs
Routing.RegisterRoute(nameof(MainPage),typeof(MainPage));
Routing.RegisterRoute(nameof(Page1), typeof(Page1));
Routing.RegisterRoute(nameof(Page2), typeof(Page2));
Routing.RegisterRoute(nameof(Page3), typeof(Page3));

在到达第二页时,无法返回并停留在该页面,即使按下返回按钮也会抛出异常。我想要能够在页面之间返回,但我无法返回。

英文:

I created a navigation project in Maui, as I don't understand more than one page. I can navigate between the first page and the second one back and forth but when I go from the second page to the third it doesn't go back to the previous page and the back arrow doesn't work. On the next page it even gives me an exception and stops there. I would like to be able to navigate between pages but I don't understand navigation through the shell

Main Page.xaml
<Button Text="Go page 1"
                Command="{Binding GoPageACommand}"/>
        <Button Text="Go Page 2"
                Command="{Binding GoPageBCommand}"/>

        <Button Text="Go page 3"
                Command="{Binding GoPageCCommand}"/>

MainViewModel
[RelayCommand]
 async void GoPageA()
        {
            try
            {
                await Shell.Current.GoToAsync(nameof(Page1));
            }catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

 [RelayCommand]
 async void GoPageB()
        {
             await Shell.Current.GoToAsync(nameof(Page2));
        }
[RelayCommand]
 async void GoPageC()
        {
            await Shell.Current.GoToAsync(nameof(Page3));
        }



Page1.xaml
<Button Text="Go page 2"
                Command="{Binding GotoPage2Command}"/>
        <Button Text="Go page 3"
                Command="{Binding GotoPage3Command}"/>
        <Button Text="GoBack"
                Command="{Binding GoBackCommand}"/>

Page1ViewModel.cs
[RelayCommand]
        async void GotoPage2()
        {
            await Shell.Current.GoToAsync(nameof(Page2));
        }

        [RelayCommand]
        async void GotoPage3()
        {
            await Shell.Current.GoToAsync(nameof(Page3));

        }

        [RelayCommand]
        async void GoBack()
        {
            await Shell.Current.GoToAsync("..");

        }
Page2.xaml
<Button Text="Go Back"
                Command="{Binding GoBackCommand}"/>
Page2ViewModel.cs
[RelayCommand]
        async void GoBack()
        {
            await Shell.Current.GoToAsync("..");
        }
AppShell.xaml
<ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>

AppShell.xaml.cs
Routing.RegisterRoute(nameof(MainPage),typeof(MainPage));
Routing.RegisterRoute(nameof(Page1), typeof(Page1));
Routing.RegisterRoute(nameof(Page2), typeof(Page2));
Routing.RegisterRoute(nameof(Page3), typeof(Page3));

Arriving at page 2 it doesn't back up and stays put on that page, even if I press the back button it will throw an exception

What I wanted was to go back between the pages but I can't go back.

答案1

得分: 2

这个问题可以在这里复制。它是由在XAML中注册路由(在AppShell.xaml中)以及在C#代码中尝试注册路由引起的。

要解决这个问题,只需删除或注释掉AppShell.xaml.cs中的这行代码Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));

英文:

This issue can be replicated here. It's caused by registering your routes in XAML (in the AppShell.xaml) and you also try to register your routes in code behind in C#.

To fix the issue, just delete or comment out the line Routing.RegisterRoute(nameof(MainPage),typeof(MainPage)); in AppShell.xaml.cs.

huangapple
  • 本文由 发表于 2023年5月15日 04:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定