Search code examples
xamarin.formsxamarin.androidxamarin.ios

Xamarin page model


I am trying to make a simple application with xamarin.

<?xml version="1.0" encoding="UTF-8" ?>
<Content Page
      xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:redcorner="clr-namespace:RedCorners.Forms;assembly=RedCorners.Forms"
      xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
      ios:Page.UseSafeArea="True"
      x:Class="CargoTracking.view.Login"
      ControlTemplate="{StaticResource LoginTemplate}"
      NavigationPage.HasNavigationBar="False">
      <ContentPage.Content>

It is very difficult to write these codes and it can be very challenging when I want to make changes. I'm a professional web developer and in my operations I model the header and footer and pass it to the required pages through each router. Is it possible to do this kind of operation in xamarin or is there an alternative solution?


Solution

  • The correct way of doing this in my Knowledge is extending the content page:

     public class BasePage : ContentPage
     {
       
        public ICommand CartItemCommand { get; set; }
        public ICommand NotificationPageCommand { get; set; }
        public BasePage() : base()
        {
            CartItemCommand = new Command(async () => await GoCartItemCommand());
            NotificationPageCommand = new Command(GoNotificationPage);
        }
    
    }
    

    And once you are done with that just use this BasePage everywhere in Place of ContentPage

    In your XAML

    <customControls:Basepage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="NameSpace.MainPage"    
            xmlns:customControls="Your base control NameSpace"
            x:Name="Main"
            NavigationPage.HasNavigationBar="True">
    
    
    </customControls:Basepage> 
    

    And in your Xaml.cs file

    public partial class MainPage: BasePage
    

    See to it that both partial classes inherit from one base class i.e BasePage or ContentPage as per your need.

    Design your headers and footers in your basepage and then use them as you please

    Good luck revert in case of queries!