Home > OS >  Xamarin App opens different Page from Navigation.PushAsync() Page
Xamarin App opens different Page from Navigation.PushAsync() Page

Time:03-21

Description

Xamarin application opens different Page upon NavigationPage.Pushaync().
NavigationPage.Asyncpush() is called within a ButtonClicked Method. That different page used to be the one being pushed but it no longer is.

ViewModel.cs

private async void OnAboutUsClicked()
{
    var modifiedContactUs = new ModifiedContactUs() { BackgroundColor = ResourceColourModel.BackgroundColor };
    await Application.Current.MainPage.Navigation.PushAsync(modifiedContactUs);
}

ModifiedContactUs.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Project.Views.Pages.ModifiedContact"
             xmlns:mapObject="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps">
    <ContentPage.Content>
        <StackLayout>
            <Grid BackgroundColor="white">
                <mapObject:Map x:Name="mapId" HasZoomEnabled="True" MapType="Street">

                </mapObject:Map>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

ModifiedContactUs.xaml.cs

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

namespace Project.Views.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ModifiedContactUs : ContentPage
    {
        public ModifiedContactUs()
        {
            InitializeComponent();
        }
    }

ContactUs.xaml(The different Page)

<?xml version="1.0" encoding="utf-8" ?>
<customControl:CustomContentPage
    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"
    xmlns:map ="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
    xmlns:ffimageloading="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
    xmlns:customControl="clr-namespace:Project.CustomControls.Renderers"
    xmlns:templates_navbar="clr-namespace:Project.Views.Templates.Menus"
    xmlns:fontAwesome="clr-namespace:Project._Utilities"
    mc:Ignorable="d"
    x:Class="Project.Views.Pages.ContactUs"
    >

    <ContentPage.Content>
        <StackLayout BackgroundColor="Transparent" Spacing="0">
            
            <templates_navbar:NavBar DotMenu_IsVisible="False" BackButton_IsVisible="True"/>

            <Grid BackgroundColor="white">
                <Grid.RowDefinitions>
                    <RowDefinition Height=".33*"/>
                    <RowDefinition Height=".07*"/>
                    <RowDefinition Height=".6*"/>
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width=".7*"/>
                        <ColumnDefinition Width=".3*"/>
                    </Grid.ColumnDefinitions>

                    <StackLayout Grid.Column="0" Padding="10">
                        <StackLayout>
                            <Label 
                                Text="Company Name" 
                                FontAttributes="Bold" 
                                FontSize="Medium"
                                FontFamily="Lato-Bold"
                                VerticalOptions="CenterAndExpand" 
                                HorizontalOptions="StartAndExpand" 
                                />
                            <Label
                                Text="{Binding Address}" 
                                FontFamily="Lato-Bold"
                                VerticalOptions="CenterAndExpand" 
                                HorizontalOptions="StartAndExpand"
                                />
                        </StackLayout>
                        <StackLayout>
                            <Label 
                                Text="Opening Times"
                                FontAttributes="Bold" 
                                FontSize="Medium" 
                                FontFamily="Lato-Bold"
                                VerticalOptions="CenterAndExpand" 
                                HorizontalOptions="StartAndExpand" 
                                />
                            <Label
                                x:Name="weekdays_lbl"
                                Text="{Binding Weekday}"
                                FontFamily="Lato-Bold"
                                VerticalOptions="CenterAndExpand" 
                                HorizontalOptions="StartAndExpand" 
                                />
                            <Label 
                                x:Name="weekend_lbl" 
                                Text="{Binding Weekend}" 
                                FontFamily="Lato-Bold"
                                VerticalOptions="CenterAndExpand" 
                                HorizontalOptions="StartAndExpand" 
                                />
                        </StackLayout>
                    </StackLayout>

                    <StackLayout Grid.Column="1">
                        <StackLayout VerticalOptions="CenterAndExpand">

                            <Button 
                                x:Name="CallUsBtn" 
                                Text="Call Us"
                                TextColor="White"
                                BackgroundColor="{StaticResource PrimaryColour}"
                                FontFamily="Lato-Bold" 
                                FontSize="12"
                                BorderWidth="2" 
                                BorderColor="White" 
                                Margin="1" 
                                CornerRadius="22" 
                                Command="{Binding PhoneCommand}"
                                />

                            <Button 
                                x:Name="MessageUsBtn" 
                                Text="Message" 
                                TextColor="White"
                                BackgroundColor="{StaticResource PrimaryColour}"
                                FontFamily="Lato-Bold" 
                                FontSize="12" 
                                BorderWidth="2" 
                                BorderColor="White" 
                                Margin="1" 
                                CornerRadius="22" 
                                Command="{Binding EmailCommand}"
                                />

                            <Button 
                                x:Name="RequestCallBack" 
                                Text="Request CallBack"
                                TextColor="White"
                                BackgroundColor="{StaticResource AccentColour}"
                                FontFamily="Lato-Bold" 
                                FontSize="11" 
                                BorderWidth="2" 
                                BorderColor="White" 
                                Margin="1" 
                                CornerRadius="22"
                                Command="{Binding CallbackCommand}"
                                />

                        </StackLayout>
                    </StackLayout>

                </Grid>

                <Grid Grid.Row="1" Padding="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width=".25*"/>
                        <ColumnDefinition Width=".25*"/>
                        <ColumnDefinition Width=".25*"/>
                        <ColumnDefinition Width=".25*"/>
                    </Grid.ColumnDefinitions>

                    <StackLayout Grid.Column="0">
                        <Grid VerticalOptions="CenterAndExpand">
                            <Image
                                Source="{FontImage FontFamily={StaticResource MaterialFontFamily},
                                Glyph={x:Static fontAwesome:IconFonts.Facebook}, Color=#1877F2,Size=60}"
                                HeightRequest="80"
                                />
                            <Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="facebook"/>
                        </Grid>
                    </StackLayout>
                    <StackLayout Grid.Column="1">
                        <Grid VerticalOptions="CenterAndExpand">
                            <Image 
                                Source="{FontImage FontFamily={StaticResource MaterialFontFamily},
                                Glyph={x:Static fontAwesome:IconFonts.Twitter}, Color=#00ACED, Size=60}"/>
                            <Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="twitter"/>
                        </Grid>
                    </StackLayout>
                    <StackLayout Grid.Column="2">
                        <Grid VerticalOptions="CenterAndExpand">
                            <Image
                                Source="{FontImage FontFamily={StaticResource MaterialFontFamily},
                                Glyph={x:Static fontAwesome:IconFonts.Linkedin}, Color=#0E76A8, Size=60}"/>
                            <Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="linkedIn"/>
                        </Grid>
                    </StackLayout>
                    <StackLayout Grid.Column="3">
                        <Grid VerticalOptions="CenterAndExpand" Padding="2">
                            <ffimageloading:SvgCachedImage
                                Source="social_logo_instagram.png"
                                HeightRequest="40"/>
                            <Button Clicked="SocialMediaBtnHandler" BackgroundColor="Transparent" Margin="5, 0" CommandParameter="instagram"/>
                        </Grid>
                    </StackLayout>

                </Grid>

                <Grid Grid.Row="2">
                    <Grid>
                        <StackLayout Grid.Row="0" Padding="5">
                            <map:Map x:Name="mapObject" HasZoomEnabled="True" MapType="Street">
                                <map:Map.ItemTemplate>
                                    <DataTemplate>
                                        <map:Pin
                                            Position="{Binding Position}"
                                            Address="{Binding Address}"
                                            Label="{Binding PlaceName}"
                                            />
                                    </DataTemplate>
                                </map:Map.ItemTemplate>
                            </map:Map>
                        </StackLayout>
                    </Grid>
                </Grid>


            </Grid>

            <AbsoluteLayout  BackgroundColor="Red" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="SizeProportional">


            </AbsoluteLayout>

        </StackLayout>
    </ContentPage.Content>
</customControl:CustomContentPage>

ContactUs.xaml.cs (The different page)

using Project._SharedData;
using Project.CustomControls.Renderers;
using Project.Models;
using Project.ViewModels;
using Rg.Plugins.Popup.Services;
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Xaml;

namespace Project.Views.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class ContactUsPage : CustomContentPage
    {
        ContactUsVM _ContactUsVM;
        public ContactUsPage()
        {
            InitializeComponent();
            BindingContext = _ContactUsVM = new ContactUsVM();
            initMap();

            configureDisplay();

            setFontOpeningTime();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            _ContactUsVM.OnAppearing();
            configureDisplay();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            _ContactUsVM.OnDisappearing();

            try
            {
                if (PopupNavigation.Instance != null)
                    PopupNavigation.Instance.PopAsync(true);
            }
            catch (Exception) { }
        }

        private void setFontOpeningTime()
        {
            DayOfWeek day = DateTime.Now.DayOfWeek;

            // Set the font based on whether current day is weekday or weekend
            if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
            {
                weekend_lbl.FontAttributes = FontAttributes.Bold;
            }
            else
            {
                weekdays_lbl.FontAttributes = FontAttributes.Bold;
            }
        }

        private void configureDisplay()
        {
            if (!string.IsNullOrEmpty(SharedPrefs.UserSessionEmail))
            {
                RequestCallBack.IsVisible = true;
                MessageUsBtn.Text = "Email";
            }
            else
            {
                RequestCallBack.IsVisible = false;
                MessageUsBtn.Text = "Message";
            }
        }

        private async void SocialMediaBtnHandler(object sender, EventArgs e)
        {
            string socialMediaName = (sender as Button).CommandParameter.ToString().ToLower();
            await Launcher.OpenAsync(SocialMediaModel.WebLink(socialMediaName));
        }



        private void initMap()
        {
            try
            {
                double latitude = Convert.ToDouble(SharedData.CompanyDetailsList[0].Latitude);
                double longitude = Convert.ToDouble(SharedData.CompanyDetailsList[0].Longitude);

                mapObject.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(latitude, longitude), Distance.FromMeters(100)));

                mapObject.ItemsSource = SharedData.CompanyDetailsList;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Expected behavior

To navigate to a new Page named ModifiedContactUs

Actual behavior:

Navigates to page called named ContactUs

Basic Information:

Android Build:
-minimum level 21
-Target level 30
Devices:
-Huawei Y6 2019
-API level 29

-Pixel 5
-API level 30

Notes

This question is different to from the first question I asked recently (Where the device crashes). This is behavior happens on all devices I test on except the one from that first question

CodePudding user response:

Expected behavior To navigate to a new Page named ModifiedContactPage

Actual behavior: Navigates to page called named ContactUsPage

From your code I saw you just push to ModifiedContactUs not ModifiedContactPage .

var modifiedContactUs = new ModifiedContactUs() { BackgroundColor = ResourceColourModel.BackgroundColor };
await Application.Current.MainPage.Navigation.PushAsync(modifiedContactUs);

Suggestion

  1. Try to add breakpoint to see if it triggers button click event es expected .

  2. Delete the old class and try again .

  3. Delete the app from the device and try again .

CodePudding user response:

This worked: Clone the repo into a new folder as a new project and copy the files and changes to this new repo.

Attempted configuration solutions that did not work: Restarting visual studio. Untick and Tick Debug Mode. Untick and tick Deploy in Configuration Manager under Build. In Android Properties> android Options making sure debugging is checked and debugger is Xamarin.

  • Related