Xamarin.Forms Navigation.PushAsync 不工作

Xamarin.Forms Navigation.PushAsync Not Working

我有一个 Xamarin.Forms 项目,我有一个自定义控件,点击时应该会打开一个新页面。不幸的是,当我调用 Navigation.PushAsync(...); 时没有任何反应。我阅读了无数 Whosebug 问题 (such as this one) 和 Xamarin 论坛主题并进行了几次 Google 搜索,但 none 的解决方案似乎解决了我的问题。

我的控件的构造函数:

public EventControl() {
    InitializeComponent();
    GestureRecognizers.Add(new TapGestureRecognizer() {
        Command = new Command(() => Navigation.PushAsync(new EventDetailsPage(src)))
    });
}

通常,在我尝试过的其他方法都无法解决我的问题时,在 Whosebug 上提出新问题是我最后的选择。感谢您的帮助。

我发现了问题。 翻看EventDetailsPage 注释掉一行XAML,问题就解决了。该行向页面添加自定义控件(与 EventControl 无关)。我不知道控件的定义或其自定义渲染器的定义是否导致了奇怪的行为,但删除该行解决了问题。

像这样改变你的App.cs

  public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new MainPage());//Very important add this..
        //MainPage = new MainPage(); //not like this        
    }

同样的问题,不同的原因...

我在 Visual Studio 中安装更新后遇到了这个问题,包括 Xamarin。

更新我所有的 nuget 包后,通过以下(第一个命令显示哪些有更新),我能够解决问题:

Get-Package -updates
Update-Package <package>

我怀疑,删除 AppData\local\Xamarin 文件夹(它似乎充当缓存)并让它重新填充也可以解决问题。

我实现了 AppShell class,它有一个输入字符串,以便设置正确的页面:

    public partial class AppShell : Shell
    {
        public AppShell(string startUpPageString = null)
        {
            InitializeComponent();

            Page startUpPage;
            if (startUpPageString == nameof(SignUpPage))
            {
                startUpPage = new SignUpPage();
            }
            else if (startUpPageString == nameof(LoginPinPage))
            {
                startUpPage = new LoginPinPage();
            }
            else
            {
                startUpPage = new SignUpPage();
            }

            ShellSection shellSection = new ShellSection();
            shellSection.Items.Add(new ShellContent() { Content = startUpPage });

            CurrentItem = shellSection;

            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            ...removed for clarity
        }
    }

其中,在我的 XAML 中,我添加了一些通过 GoToAsync(postlogin) 方法调用的选项卡(登录后部分):

<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:login="clr-namespace:XXX.Views.Login"
       xmlns:postlogin ="clr-namespace:XXX.XXX.Views.PostLogin"
       x:Class="XXX.AppShell"
       FlyoutBehavior="Disabled">


    <ShellItem Route="postlogin">
        <ShellSection Route="activity_section" Title="Activity" Icon="Activity_Menu_Icon.png">
            <ShellContent  Route="page1"
                      Title="Page1"
                      Icon="Pag1.png"
                      ContentTemplate="{DataTemplate postlogin:Page1}" />
        </ShellSection>
 <ShellContent  Route="page2"
                      Title="Page2"
                      Icon="Pag2.png"
                      ContentTemplate="{DataTemplate postlogin:Page2}" />
        </ShellSection>
 <ShellContent  Route="page3"
                      Title="Page3"
                      Icon="Page3.png"
                      ContentTemplate="{DataTemplate postlogin:Page3}" />
        </ShellSection>
 <ShellContent  Route="page4"
                      Title="Page4"
                      Icon="Pag4.png"
                      ContentTemplate="{DataTemplate postlogin:Page4}" />
        </ShellSection>
    </ShellItem>

    <FlyoutItem Route="others"
                Title="OTHERS"
                FlyoutDisplayOptions="AsMultipleItems">
        <Tab>
            <ShellContent Route="cats"

                          Title="Cats"
                          Icon="next_7.png"
                       />
            <ShellContent Route="dogs"

                          Title="Dogs"
                          Icon="next_7.png"
                          />
        </Tab>
    </FlyoutItem>
</Shell>

因此,我的问题如下:例如,当我到达 SignUpPage 并单击一个按钮时,该按钮将执行: await Shell.Current.Navigation.PushAsync(new SentEmailPage()); 赶上消息的一部分:Object reference not set to an instance of an object.at Xamarin.Forms.ShellSection.OnPushAsync