0
votes

I've just started working on my very first Xamarin Forms app for Android, iOS and UWP and have looked through a lot of examples and code in order to get started.

The app is built using MVVM structure, as i guess most Xamarin.Forms apps are and I am currently facing two issues that I can't find any answer to. These problems might be Xamarin.Forms 101 so I apologize if these are "stupid" questions.

App.xaml.cs -> App.MainPage = new NavigationPage(new MainPage())

In the App.xaml.cs file I have defined which page that should be set as the startup-page based on Device idiom.

    Page startupPage = null;
    if (Device.Idiom == TargetIdiom.Phone)
        startupPage = new NavigationPage(new MainPage());
    else
        startupPage = new NavigationPage(new MainPageTablet());
    MainPage = startupPage;

This works fine, but i face one issue with this.

OnAppearing() and OnDisappearing() are both called on startup

When the app starts, both OnAppearing() and OnDisappearing() are called inside MainPage.xaml.cs/MainPageTablet.xaml.cs file(s). Is this a bug, or should this happen?

When removing "NavigationPage" from the declaration of startupPage only OnAppearing() is called on startup

    Page startupPage = null;
        if (Device.Idiom == TargetIdiom.Phone)
            startupPage = new MainPage();
        else
            startupPage = new MainPageTablet();
        MainPage = startupPage;

Now the startup works, like I feel it should with only calling OnAppearing() on startup. This however now introduces some new "problem(?)". The ToolbarItem that i have inside MainPage.xaml/MainPageTablet.xaml disappears.

<?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:local="clr-namespace:TestApp.Classes;assembly=TestApp"
             x:Class="TestApp.Views.MainPageTablet">
    <ContentPage.ToolbarItems>
        <ToolbarItem Name="MenuItem1" Order="Secondary" Text="Log out" Command="{Binding LogOutCommand}" Icon="settingsImage.png" />
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        ....
    </ContentPage.Content>
</ContentPage>

Can a ToolbarItem only be used in a NavigationPage, or do I have to do more than just adding it to the in order to get it to work?

Any help or comments regarding this would be really helpful.

Thanks

1
It could possibly be this bug.martinstoeckli
Thanks, exactly this bug I am facing. This should have been solved in 2.6.0-pre1. Updating forms seems to have solved the problem.J.Martin

1 Answers

0
votes

If you are running your app on mobile, then on startup OnAppearing() of MainPage() should only be called.

If the page is setup as Navigation page, then only default navigation bar appears. So when you add toolbar item it will be visible. But as you have not set main page as navigation page, navigation bar will not appear and so toolbar item is not visible.