我在App.Xaml中定义了一个模板。
我在我的首页中使用它。
我在代码中设置了Homepage的BindingContext。那么控件模板(ControlTemplate)不应该继承HomePage的BindingContext吗?因为我认为是这样的,但我的标签(Label)没有保留MyLabelText的文本。在这些模板中使用绑定(Bindings)有什么解决方法吗?
编辑:使用以下选项:
<ResourceDictionary>
<ControlTemplate x:Key="HomePageTemplate">
<Label Text="{Binding MyLabelText}"/>
</ControlTemplate>
</ResourceDictionary>
我在我的首页中使用它。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
xmlns:local="clr-namespace:App.Converters"
x:Class="App.Views.HomePage"
ControlTemplate="{StaticResource HomePageTemplate}">
</ContentPage>
我在代码中设置了Homepage的BindingContext。那么控件模板(ControlTemplate)不应该继承HomePage的BindingContext吗?因为我认为是这样的,但我的标签(Label)没有保留MyLabelText的文本。在这些模板中使用绑定(Bindings)有什么解决方法吗?
编辑:使用以下选项:
<ResourceDictionary>
<ControlTemplate x:Key="HomePageTemplate">
<Label Text="{TemplateBinding Parent.BindingContext.MyLabelText}"/>
</ControlTemplate>
</ResourceDictionary>
这对我也不起作用,因为我在HomePage
的标题中使用了ControlTemplate
而不是在其主体中。
这种方法可行,但不是我要找的:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
xmlns:local="clr-namespace:App.Converters"
x:Class="App.Views.HomePage"
>
<ContentView ControlTemplate="{StaticResource HomePageTemplate}" />
</ContentPage>