简体   繁体   中英

Display a ToolBar in a Xamarin.Forms modal page

The "new" and recommended way to display a modal page with the Xamarin.Forms Shell uri-based navigation is to set this tag in the XAML file ( source ): Shell.PresentationMode="ModalAnimated" and to navigate to it by using a standard route and invoking it with the function Shell.Current.GoToAsync("routeToMyPage") .

However, this displays the modal page without a toolbar. Without Shell navigation, I would have wrapped this page in a NavigationPage , but since the pages are initialized through reflection (at least that's what it looks like - don't quote me on this), I don't know how to do that.

Adding a ToolbarItem in the page's XAML code doesn't solve this, neither does the Shell.NavBarIsVisible="True" property, and adding a Button in the Shell.TitleView tag doesn't display a toolbar either.

Is there a way to display the default navigation toolbar without rendering a custom one myself?

Here is the XAML code I used to try to have the Toolbar displayed:

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Shell.PresentationMode="ModalAnimated"
    Shell.NavBarIsVisible="True"
    x:Class="StackOverflow.Views.MyModalPage">
    <ContentPage.ToolbarItems >
        <ToolbarItem Text="Hi"/>
    </ContentPage.ToolbarItems>
    <Shell.TitleView>
        <Button Text="Toolbar Button"/>
    </Shell.TitleView>
    <ContentPage.Content>

    </ContentPage.Content>
</ContentPage>

Edit: I have created a small sample project to showcase my issue: https://github.com/Kuurse/StackOverflowExample

You can first create the NavigationPage root page in the app class:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
}

Then set like this in your mainpage.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="StackOverflow.Views.MyModalPage"
         NavigationPage.HasNavigationBar="True">
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Hi"/>
</ContentPage.ToolbarItems>
</ContentPage>

By the way, do you want to display only the default navigation toolbar?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM