iam*_*hia 5 c# xamarin 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在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>
Run Code Online (Sandbox Code Playgroud)
我想做的是在一个单独的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="◃" FontSize="25"/>
</StackLayout>
<Label x:Name="pageTitle" FontSize="14"/>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的就是能够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() };
Run Code Online (Sandbox Code Playgroud)
这有效,但不是100%。我可以显示XAML,但不能从中获取PageTitle值TitleViewTemplate。有人可以在这里指出我正确的方向吗?也许我缺少一些绑定之类的东西?
| 归档时间: |
|
| 查看次数: |
495 次 |
| 最近记录: |