我是 Xamarin 編程的新手,之前使用過很多 ASP.NET Core MVC。我正在嘗試為 Android/iOS 制作一個簡單有趣的應用程式,但我在使用一個小按鈕時遇到了問題:)。我有一個登錄頁面,它將重定向到一個關于頁面,主頁,但是當我按下按鈕時出現此錯誤: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>
應用程式.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()
{
}
}
}
登錄頁面.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();
}
}
}
登錄ViewModel.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)}");
}
}
}
先謝謝了!最好的問候馬克斯
uj5u.com熱心網友回復:
使用一個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>
uj5u.com熱心網友回復:
解決它!我弄錯了 MainPage,應該是 AppShell()!!感謝所有的幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/382420.html
