【问题标题】:Display a ToolBar in a Xamarin.Forms modal page在 Xamarin.Forms 模式页面中显示工具栏
【发布时间】:2022-09-27 17:03:12
【问题描述】:

使用基于 Xamarin.Forms Shell uri 的导航显示模式页面的“新”和推荐方法是在 XAML 文件 (source) 中设置此标记:Shell.PresentationMode=\"ModalAnimated\" 并通过使用标准路线并使用函数Shell.Current.GoToAsync(\"routeToMyPage\") 调用它来导航到它。

但是,这将显示没有工具栏的模式页面。如果没有 Shell 导航,我会将此页面包装在 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

    标签: c# xaml xamarin.forms


    【解决方案1】:

    您可以先在应用类中创建 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>
    

    顺便问一下,你想只显示默认的导航工具栏吗?

    【讨论】:

    • 我正在使用 Shell.GoToAsync("myModalRoute") 函数显示我的模态页面,如何在执行此操作时将我的模态页面包装在 NavigationPage 中?
    • 我添加了一个示例项目,显示了我的问题以及我正在尝试做的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2012-04-17
    相关资源
    最近更新 更多