Question Page not loading in VB 2019/C#/XAML

Riffy100

New member
Joined
Jul 24, 2021
Messages
2
Programming Experience
Beginner
Hi

I have recently started using VB 2019 with Xamarin so am a newbie when it comes to developing an app.

I have stated to build a simple app, but am struggling with getting pages to load with after the Main page. Am really keen to go past this stage.

I have a button declared in my XAML file Called TEST as follows:
C#:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="Xamarin | Open-source mobile app platform for .NET"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.MainPage">

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <Button Text="TEST" Clicked="Button_Clicked"/>
        <Label Text="Start developing now" FontSize="Title" Padding="30,10,30,10"/>
        <Label Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" FontSize="16" Padding="30,0,30,0"/>
        <Label FontSize="16" Padding="30,24,30,0">
            <Label.FormattedText>
                <FormattedString>
                    <FormattedString.Spans>
                        <Span Text="Learn more at "/>
                        <Span Text="Xamarin.Forms quickstarts - Xamarin" FontAttributes="Bold"/>
                    </FormattedString.Spans>
                </FormattedString>
            </Label.FormattedText>
        </Label>

        </StackLayout>

</ContentPage>
Clicking TEST button invokes Button_Clicked in MainPage.XAML.CS as follows

MainPage.XAML.CS:
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
          [B]  Page1 Pg1 = new Page1();[/B]
      

        }
    }
}

The Page1.xaml is :
C#:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="Xamarin | Open-source mobile app platform for .NET"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.Page1">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The Page1.xaml.cs is:

Page1.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
        }

      
    }
}

Clicking Button does nothing.

Can anyone help me resolve this and if possible explain how pages get loaded, as I would like to write correct and efficient code for my App, and can run on Android/Windows and IOS.

Thanks
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
Take the following with a huge grain of salt because I'm on the good pain medication right now, but I believe you don't just new-up another content page in Xamarin Forms and expect that content page to be displayed. You have to actually hook into Xamarin's navigation framework and tell it about your other pages, and then force a navigation to that page.
 

Riffy100

New member
Joined
Jul 24, 2021
Messages
2
Programming Experience
Beginner
Take the following with a huge grain of salt because I'm on the good pain medication right now, but I believe you don't just new-up another content page in Xamarin Forms and expect that content page to be displayed. You have to actually hook into Xamarin's navigation framework and tell it about your other pages, and then force a navigation to that page.
Hi Skydiver

Thank you for the response.

I have tried to google tutorials and examples of handling content pages in Xamarin, without much success. Would you be able to demonstrate, using my example on how to achieve this.

Nice to know you are on good medication.;)

Thanks
 
Top Bottom