![](/img/trans.png)
[英]Xamarin.Forms: how to set background image of NavigationPage bar
[英]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="◃" 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.