【问题标题】:How to call a XAML code in NavigationPage.SetTitleView in Xamarin.Forms?如何在 Xamarin.Forms 的 NavigationPage.SetTitleView 中调用 XAML 代码?
【发布时间】:2019-03-14 19:25:39
【问题描述】:

我有一个 Xamarin.Forms 应用程序,它使用仅适用于 Android 的自定义导航标题视图。现在我的标题视图是在我的模板构造函数中定义的,如下所示:

[ContentProperty(nameof(InnerContent))]
public partial class ContentCustomTitleView : ContentPage
{

    public static readonly BindableProperty InnerContentProperty = BindableProperty.Create(nameof(InnerContent), typeof(View), typeof(ContentCustomTitleView));
    public static readonly BindableProperty PageTitleProperty = BindableProperty.Create(nameof(PageTitle), typeof(string), typeof(Label), default(string), BindingMode.OneWay);

    public View InnerContent
    {
        get => (View)this.GetValue(InnerContentProperty);
        set => this.SetValue(InnerContentProperty, value);
    }

    public string PageTitle
    {
        get
        {
            var value = (string)GetValue(PageTitleProperty);
            return value;
        }
        set => SetValue(PageTitleProperty, value);
    }

    public ContentCustomTitleView()
    {
        InitializeComponent();
        BindingContext = this;

        if (Device.RuntimePlatform == "Android")
        {
            NavigationPage.SetHasBackButton(this, false);
            NavigationPage.SetTitleView(this, SetBackView());
        }
    }

    StackLayout SetBackView()
    {
        StackLayout backButton = new StackLayout
        {
            Children =
            {
                new Label {
                    Text = "\u25C3",
                    FontSize = 25,
                }
            },
            Padding = new Thickness(5, 0, 20, 0),
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.Start,
            BackgroundColor = Color.Orange
        };

        var tabEvent = new TapGestureRecognizer();
        tabEvent.Tapped += (object sender, EventArgs e) => { Navigation.PopAsync(); };
        backButton.GestureRecognizers.Add(tabEvent);

        Label pageTitle = new Label()
        {
            FontSize = 14,
            HorizontalTextAlignment = TextAlignment.Center,
            VerticalTextAlignment = TextAlignment.Center,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.Center,
        };
        pageTitle.SetBinding(Label.TextProperty, new Binding(path: "PageTitle", source: this));

        StackLayout backView = new StackLayout
        { 
            Children = 
            {
                backButton,
                pageTitle
            },
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            Orientation = StackOrientation.Horizontal,
        };

        return backView;
    }

}

然后我在 XAML 中使用这段代码:

<t:ContentCustomTitleView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
             x:Class="MyProject.Details"
             Title="{Binding Title}"
             PageTitle="Application Details">
            <!-- more code here -->
</t:ContentCustomTitleView>

我想做的是在单独的 xaml 文件中创建我的标题视图模板,然后在 NavigationPage.SetTitleView(this, ); 中调用该模板并将 PageTitle 属性传递给该标题视图模板。这可能吗?几天来,我一直被困在这个困境中。

编辑:

这是我目前所拥有的。在 XAML 中:

<?xml version="1.0" encoding="UTF-8"?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="MyProject.TitleViewTemplate"
             VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
             Orientation="Horizontal">
    <StackLayout x:Name="backButton" Padding="5,0,20,0" VerticalOptions="Start" HorizontalOptions="Start">
        <Label Text="&#x25c3;" FontSize="25"/>
    </StackLayout>
    <Label x:Name="pageTitle" FontSize="14"/>
</StackLayout>

在 C# 中:

public partial class TitleViewTemplate : StackLayout
{
   public TitleViewTemplate()
   {
      InitializeComponent();
   }

   public StackLayout SetBackView(EventHandler backButtonClicked)
   {
      var tabEvent = new TapGestureRecognizer();
      tabEvent.Tapped += backButtonClicked;
      backButton.GestureRecognizers.Add(tabEvent);

      pageTitle.SetBinding(Label.TextProperty, new Binding(path: "PageTitle", source: this));

      return this;
   }
}

我想做的是能够像这样在我的ContentCustomTitleView 中调用它:

// more code here
public ContentCustomTitleView()
{
   InitializeComponent();
   BindingContext = this;
   tv = new TitleViewTemplate();
   if (Device.RuntimePlatform == "Android")
   {
      NavigationPage.SetHasBackButton(this, false);
      NavigationPage.SetTitleView(this, tv.SetBackView(GoBack));
    }
 }
 /// more code here
 void GoBack(object o, EventArgs e) { Navigation.PopAsync() };

这可行,但不是 100%。我可以正常显示 XAML,但我无法从 TitleViewTemplate 获取 PageTitle 值。谁能在这里指出我正确的方向?也许我错过了一些绑定或什么?

【问题讨论】:

  • 您想要的基本上是一种可以调用来更改导航栏标题的方法?

标签: c# xamarin xamarin.forms


【解决方案1】:

你试过了吗:

  StackLayout titleView = tv.SetBackView(GoBack);
  Label label = titleView.FindByName<Label>("pageTitle");
  string titleText = label.Text;

【讨论】:

    【解决方案2】:
      pageTitle.SetBinding(Label.TextProperty, new Binding(path: "PageTitle", source: this));
    

    这对我来说看起来不错,您是否尝试过在调用自定义视图的构造函数时重新排序代码并设置页面标题。? 即为自定义视图初始化组件。

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 1970-01-01
      • 2017-12-12
      • 2021-07-29
      • 1970-01-01
      • 2018-09-12
      • 2019-06-07
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多