XAML 在Xamarin.Forms模式页中显示工具栏

icomxhvb  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(189)

使用基于Xamarin.Forms Shell uri的导航显示模态页面的“新”和推荐方法是在XAML文件(source)中设置以下标记:Shell.PresentationMode="ModalAnimated",并通过使用标准路线并利用函数Shell.Current.GoToAsync("routeToMyPage")调用它来导航到它。
如果没有Shell导航,我会把这个页面 Package 在一个NavigationPage中,但是由于页面是通过反射初始化的(至少看起来是这样的--不要引用我的话),我不知道如何做到这一点。
在页面的XAML代码中添加ToolbarItem并不能解决此问题,Shell.NavBarIsVisible="True"属性也不能解决此问题,在Shell.TitleView标记中添加Button也不能显示工具栏。
有没有一种方法可以显示默认的导航工具栏,而无需自己渲染自定义工具栏?
下面是我用来尝试显示工具栏的XAML代码:

<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>

编辑:我创建了一个小的示例项目来展示我的问题:https://github.com/Kuurse/StackOverflowExample

fv2wmkja

fv2wmkja1#

您可以先在应用程序类中创建NavigationPage根页面:

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

然后在您的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>

顺便问一下,是否只显示默认导航工具栏?

相关问题