Home > Mobile >  await Shell.Current.GoToAsync($"//{nameof(AboutPage)}"); Error in Xamarin
await Shell.Current.GoToAsync($"//{nameof(AboutPage)}"); Error in Xamarin

Time:12-10

I am new to Xamarin programming and have been using a lot of ASP.NET Core MVC before. I am trying to make a simple fun app for Android/iOS but I am having problem with a little button :). I have a loginpage that will redirect to an aboutpage, homepage, but I get this error hen I press the button: System.NullReferenceException: 'Object reference not set to an instance of an object.'

Here is my code: 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)}");
        }
    }
}

Thanks beforehand! Best regards Max

CodePudding user response:

Use one TabBar would fix the error.

Change:

  <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>

To:

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

CodePudding user response:

FIXED IT! I had the wrong MainPage, it should be AppShell()!! Thanks for all the help!

  • Related