【问题标题】:how to integrate custom navigation title bar to all pages如何将自定义导航标题栏集成到所有页面
【发布时间】:2022-01-04 08:34:01
【问题描述】:

我有代码可以使用 shell 视图的内容控制在所有页面上集成自定义标题栏。

这是我的代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SmartEntry.DashBoard">
    
    Shell.TitleView = new Lable(Application.Current.Resources["TopBar"]);


    <ContentPage.ToolbarItems>
        <ToolbarItem Icon="logout_icon.png" Order="Primary" Priority="1" Clicked="ToolbarItem_Clicked"/>
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
       
        <StackLayout>

.....

这是我的 app.xaml 代码:

public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new SmartEntry.LoginPage1());

            var page = App.Current.MainPage.Navigation.NavigationStack.Last();

            switch(page.ToString())
            {
                case "DashBoard.xaml":
                    lbltitle.Text = "DASHBOARD";
                    break;
                case "GenerateBarcode.xaml":
                    lbltitle.Text = "WIFI SETTINGS";
                    break;
                case "ManageDevice.xaml":
                    lbltitle.Text = "ADD DEVICE";
                    break;
                case "MyProfile.xaml":
                    lbltitle.Text = "MY PROFILE";
                    break;
                case "RegistrationUser.xaml":
                    lbltitle.Text = "MEMBER REGISTRATION";
                    break;
                case "WifiSettings1.xaml":
                    lbltitle.Text = "WIFI SETTINGS";
                    break;
            }


            //MainPage = CrossConnectivity.Current.IsConnected ? (Page)new WifiSettings1() : new WifiSettings1();

        }

这里是 app.xaml 标记:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SmartEntry.App">
    <Application.Resources>
        <Label x:Name="lbltitle" TextColor="White" HorizontalOptions="Start" VerticalOptions="StartAndExpand" x:Key="TopBar"></Label>
    </Application.Resources>
</Application>

在这里动态更改 lbltitle 控件的标题。但没有成功。

有没有人能解决我的问题。

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android


    【解决方案1】:

    使用 MVVM 绑定! Xamarin Forms 是为开箱即用的 MVVM 而构建的,如果您还没有,我真的建议您阅读架构。

    基本上,每个屏幕(视图)都有一个相应的功能处理类(ViewModel)。此 ViewModel 用于通过 Binding 更新视图。 Binding 是一种特殊的双向属性,一旦在屏幕上或通过 ViewModel 中的代码更新值,您就可以在 View 上获取和设置值。因此,在您的视图模型中,您可以创建一个名为 Title 的属性并将其绑定到您的前端 XAML 使用

    <Application.Resources>
        <Label Text="{Binding Title}" />
    </Application.Resources>
    

    这应该让你开始 - https://nicksnettravels.builttoroam.com/mvvm-navigation/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多