首页 > 解决方案 > TitleView 不显示

问题描述

我有一个支持 UWP 的 Xamarin.Forms 应用程序。在所有页面上,我都添加了一个带有标签的 TitleView:

        var page = (Page)Activator.CreateInstance(item.TargetType);

        Label titleViewLabel = new Label
        {
            Text = item.Title,
            FontAttributes = FontAttributes.Bold,
            FontSize = 25,
            TextColor = Color.White,
            BackgroundColor = backgroundColor,
            Margin = 10
        };

        NavigationPage.SetTitleView(page, titleViewLabel);

现在我的一个页面有一个很好的标题“产品”。

这个页面有一个产品的 ListView,点击其中的任何项目,我们导航到 ProductDetailPage:

    private async void ProductView_OnItemSelected(object sender, SelectedItemChangedEventArgs args)
    {
        Product product = (Product)args.SelectedItem;
        ProductDetailPage productDetailPage = new ProductDetailPage
        {
            BindingContext = product,
            Title = product.Name
        };

        await Navigation.PushAsync(productDetailPage);
    }

当我们将标题设置为产品名称时,之前的标题“产品”被替换为特定的产品名称。没关系,但是当我按下按钮返回再次导航到产品页面时。但标题保持为空。我调试了它,并且可以看到 TitleView 仍然包含文本“Products”:

    protected override void OnAppearing()
    {
        base.OnAppearing();

        Label titleView = ((Label)NavigationPage.GetTitleView(this));
        // titleView.Text has "Products!!
    }

如何让标题显示?

标签: xamarin.formsxamarin.uwp

解决方案


推荐阅读