0
votes

I am developing a cross platform app using xaml forms and have yet to figured out how to call a ContentView ontapGesture. I am able to call ContentView on xaml forms using <local:MailRoomList/> but I cant show a view ontap or on click event.

I have tried to show view on stacklayout using StackLayoutID.Children.Add(new MailRoomList()); but thats not what I want. I want to show full contentview on content page with back button enabled.

Please advise. Thank you

MailRoomList

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="xx.xxxx.xx.Views.MailRoomList">
<ContentView.Content>
  <StackLayout>
      <Label Text="This is my MailRoom List" />
  </StackLayout>
</ContentView.Content>
</ContentView>

OnTapGesture

<Frame.GestureRecognizers>
    <TapGestureRecognizer Tapped="OnRegisterMail" NumberOfTapsRequired="1"/>
</Frame.GestureRecognizers>

CodeBehind

async void OnRegisterMail(object sender, EventArgs e)
{
    await Navigation.PushAsync(new MailRoomList());
}

Edit: Home Page

<?xml version="1.0" encoding="utf-8" ?>
<xf1:BottomBarPage
         xmlns:xf1="clr-namespace:xx.xxxx.xx.BottomBar" 
         xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:xx.xxxx.xx.Views"
        x:Class="xx.xxxx.xx.Views.Home">
<ContentPage x:Name="MailRoomPage" Title="MailRoom" Icon="mailroom.png" xf1:BottomBarPageExtensions.TabColor="Orange" xf1:BottomBarPageExtensions.BadgeCount="5">
    <local:MailRoom/>
</ContentPage>

<ContentPage Title="Transport" Icon="transport.png" xf1:BottomBarPageExtensions.TabColor="Green" xf1:BottomBarPageExtensions.BadgeCount="10">
    <Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="Transport" FontSize="Large"/>
</ContentPage>
<ContentPage Title="Communication" Icon="communication.png" xf1:BottomBarPageExtensions.TabColor="Blue" xf1:BottomBarPageExtensions.BadgeCount="3">
    <Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="Communication" FontSize="Large"/>
</ContentPage>
<ContentPage Title="HSE" Icon="hse.png" xf1:BottomBarPageExtensions.TabColor="Red" xf1:BottomBarPageExtensions.BadgeCount="2">
    <Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="HSE" FontSize="Large"/>
</ContentPage>
<ContentPage Title="Meeting" Icon="meeting.png" xf1:BottomBarPageExtensions.TabColor="DarkCyan" xf1:BottomBarPageExtensions.BadgeCount="5">
    <Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="Meeting" FontSize="Large"/>
</ContentPage>

From homepage above I am calling my view <local:MailRoom/> which is calling another view MailRoomList. This is where I need your advice on how to call a content view from another content view.

1
Is your app's MainPage a NavigationPage? - Markus Michel
Yes, Home is my NavigationPage after login screen. Application.Current.MainPage = new NavigationPage(new Home()); - Mohsin
Did you check if the OnRegisterMail function is actually being called after Tapping? Also try Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { await Navigation.PushAsync(new MailRoomList()); }); - Markus Michel
If that doesn't work, please also post xaml and codebehind of your home page. - Markus Michel
That doesnt work, throws error cannot convert from view to xamarin.forms.page. I am calling a from another view. - Mohsin

1 Answers

0
votes

ContentView is a View not a Page. Change your MainRoomList Xaml to this

 <?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="xx.xxxx.xx.Views.MailRoomList">
     <StackLayout>
        <Label Text="This is my MailRoom List" />
     </StackLayout>
 </ContentPage>

View's dont support navigation, they can only be navigated to if they belong to a Page which are the objects that contain the navigation infrastructure.

IF you want to mantain your MailRoomList as a ContentView, then you need to include her in some other page, and perform the navigation ( inside the tap method ) to that other page