繁体   English   中英

如何在Xamarin.Forms中的NavigationPage.SetTitleView中调用XAML代码?

[英]How to call a XAML code in NavigationPage.SetTitleView in Xamarin.Forms?

我有一个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, );调用该模板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值。 任何人都可以指出我正确的方向吗? 也许我错过了一些绑定或什么?

你试过这个:

  StackLayout titleView = tv.SetBackView(GoBack);
  Label label = titleView.FindByName<Label>("pageTitle");
  string titleText = label.Text;
  pageTitle.SetBinding(Label.TextProperty, new Binding(path: "PageTitle", source: this));

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM