等待 Shell.Current.GoToAsync($"//nameof(AboutPage)"); Xamarin 中的错误

Posted

技术标签:

【中文标题】等待 Shell.Current.GoToAsync($"//nameof(AboutPage)"); Xamarin 中的错误【英文标题】:await Shell.Current.GoToAsync($"//nameof(AboutPage)"); Error in Xamarin等待 Shell.Current.GoToAsync($"//nameof(AboutPage)"); Xamarin 中的错误 【发布时间】:2022-01-14 18:03:37 【问题描述】:

我是 Xamarin 编程的新手,之前使用过很多 ASP.NET Core MVC。我正在尝试为 android/ios 制作一个简单有趣的应用程序,但我遇到了一个小按钮的问题 :)。我有一个登录页面,它会重定向到 aboutpage、主页,但是当我按下按钮时出现此错误:System.NullReferenceException: 'Object reference not set to an instance of an object.'

这是我的代码: AppShell.xaml

        <?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:WorkoutApp.Views"
       Title="WorkoutApp"
       x:Class="WorkoutApp.AppShell">

    <Shell.Resources>
        <ResourceDictionary>
                <Style x:Key="BaseStyle" TargetType="Element">
                    <Setter Property="Shell.BackgroundColor" Value="StaticResource Primary" />
                    <Setter Property="Shell.ForegroundColor" Value="White" />
                    <Setter Property="Shell.TitleColor" Value="White" />
                    <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
                    <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
                    <Setter Property="Shell.TabBarBackgroundColor" Value="White" />
                    <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                    <Setter Property="Shell.TabBarUnselectedColor" Value="black"/>
                    <Setter Property="Shell.TabBarTitleColor" Value="black"/>
                </Style>
                <Style TargetType="TabBar" BasedOn="StaticResource BaseStyle" />
                <Style TargetType="FlyoutItem" BasedOn="StaticResource BaseStyle" />
            </ResourceDictionary>
        
    </Shell.Resources>

    <TabBar>
        <ShellContent Title="Home" Icon="home.png" Route="AboutPage" ContentTemplate="DataTemplate local:HomePage" />
        <ShellContent Title="Browse" Icon="cart.png" ContentTemplate="DataTemplate local:ShopPage" />
        <ShellContent Title="Browse" Icon="account.png" ContentTemplate="DataTemplate local:AboutPage" />
    </TabBar>

</Shell>

App.xaml.cs:

       using System;
using WorkoutApp.Views;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace WorkoutApp

    public partial class App : Application
    

        public App()
        
            InitializeComponent();

            MainPage = new LoginPage();
        

        protected override void OnStart()
        
        

        protected override void OnSleep()
        
        

        protected override void OnResume()
        
        
    

LoginPage.xaml + .cs:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="WorkoutApp.Views.LoginPage"
             Shell.NavBarIsVisible="False">
    <ContentPage.Content>
        <StackLayout Padding="10,0,10,0" VerticalOptions="Center">
            <Button VerticalOptions="Center" Text="Login" Command="Binding LoginCommand"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkoutApp.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace WorkoutApp.Views

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    
        public LoginPage()
        
            InitializeComponent();
            this.BindingContext = new LoginViewModel();
        
    

LoginViewModel.cs:

   using System;
using System.Collections.Generic;
using System.Text;
using WorkoutApp.Views;
using Xamarin.Forms;

namespace WorkoutApp.ViewModels

    public class LoginViewModel
    
        public Command LoginCommand  get; 
        public Command RegisterCommand  get; 

        public LoginViewModel()
        
            LoginCommand = new Command(OnLoginClicked);
            RegisterCommand = new Command(OnRegisterClicked);
        

        private async void OnLoginClicked(object obj)
        
            // Prefixing with `//` switches to a different navigation stack instead of pushing to the active one
            await Shell.Current.GoToAsync($"//nameof(AboutPage)");

        

        private async void OnRegisterClicked(object obj)
        
            // Prefixing with `//` switches to a different navigation stack instead of pushing to the active one
            await Shell.Current.GoToAsync($"//nameof(RegisterPage)");
        
    

先谢谢了! 最好的问候马克斯

【问题讨论】:

首先,空引用是最常见的 C# 异常之一 - 它与 Xamarin 没有任何关系。其次,如果在您按下按钮时发生错误,那么发布 该代码 会有所帮助。您发布了许多其他不相关的代码,但没有发布包含 LoginCommand 的 VM 代码 我一定错过了!抱歉,我现在在主帖中添加了它! 您是否在调试器中单步执行了代码以确定哪个元素为空? 为什么你的虚拟机继承自ContentPage @Jason 我检查了调试器,我相信它是空的 MainPage 【参考方案1】:

使用TabBar 可以修复错误。

变化:

  <TabBar>
    <ShellContent Title="Home" Icon="home.png" Route="AboutPage" ContentTemplate="DataTemplate local:HomePage" />
    <ShellContent Title="Browse" Icon="cart.png" ContentTemplate="DataTemplate local:ShopPage" />
    <ShellContent Title="Browse" Icon="account.png" ContentTemplate="DataTemplate local:AboutPage" />
</TabBar>

<!--
    If you would like to navigate to this content you can do so by calling
    await Shell.Current.GoToAsync("//LoginPage");
-->
<TabBar>
    <ShellContent Route="LoginPage" ContentTemplate="DataTemplate local:LoginPage" />
    <ShellContent Route="AboutPage" ContentTemplate="DataTemplate local:AboutPage" />
</TabBar>

收件人:

  <TabBar>
    <ShellContent Route="LoginPage" ContentTemplate="DataTemplate local:LoginPage" />
    <ShellContent Route="AboutPage" ContentTemplate="DataTemplate local:AboutPage" />
</TabBar>

【讨论】:

嗨!我删除了另一个标签栏,它仍然给我同样的错误。我会更新上面的代码!【参考方案2】:

已修复!我的 MainPage 错误,应该是 AppShell()!!感谢大家的帮助!

【讨论】:

以上是关于等待 Shell.Current.GoToAsync($"//nameof(AboutPage)"); Xamarin 中的错误的主要内容,如果未能解决你的问题,请参考以下文章

selenium强制等待,隐式等待,显式等待

selenium中的显示等待,隐示等待,强制等待

sql server等待类型

selenium中的三种等待方式(显示等待WebDriverWait()隐式等待implicitly()强制等待sleep())---基于python

selenium显示等待和隐式等待的区别

selenium 显示等待和隐式等待哪个更好