Xamarin.forms OnApplyTemplate没有';无法捕获父级的BindingContext?

Xamarin.forms OnApplyTemplate没有';无法捕获父级的BindingContext?,xamarin.forms,data-binding,Xamarin.forms,Data Binding,在一个测试项目中,我试图通过模板绑定获取父控件的BindingContext 这里,在主页中,我有两个模板temp1和temp2 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

在一个测试项目中,我试图通过模板绑定获取父控件的
BindingContext

这里,在
主页
中,我有两个模板
temp1
temp2

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:MyXam.ViewModels"
             xmlns:views="clr-namespace:MyXam.Views"
             x:Class="MyXam.Views.MainPage"
             x:DataType="vm:MainViewModel">
    <ContentPage.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="temp1">
                <views:View1/>
            </ControlTemplate>
            <ControlTemplate x:Key="temp2">
                <views:View2/>
            </ControlTemplate>

        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout x:Name="stk">
        <Button Text="Switch view" Command="{Binding SwitchViewCommand}"/>
        <ContentView x:Name="cv" ControlTemplate="{StaticResource temp2}" VerticalOptions="Start" HorizontalOptions="Center">
            <ContentView.Triggers>
                <DataTrigger TargetType="ContentView" Binding="{Binding IsView1}" Value="False">
                    <Setter Property="ControlTemplate" Value="{StaticResource temp2}"/>
                </DataTrigger>
                <DataTrigger TargetType="ContentView" Binding="{Binding IsView1, Mode=TwoWay}" Value="True">
                    <Setter Property="ControlTemplate" Value="{StaticResource temp1}"/>
                </DataTrigger>
            </ContentView.Triggers>
        </ContentView>
    </StackLayout>
</ContentPage>
但是当我试图在应用程序模板中获取它的vzlue时,它是空的:

 protected override void OnApplyTemplate()
 {
     base.OnApplyTemplate();
     vm = this.GetValue(BindingContextProperty);
 }
但是,绑定在xaml中解决:

<Label Text="{Binding Name, Source={Reference this}}"/>

我看不到其他代码的详细信息,但您可以参考以下代码

xaml.cs
中为此标签定义一个
BindableProperty
,并使用Name属性,例如
x:Name=“TestControlView”
和如下绑定

 <Label Text="{Binding Source={x:Reference TestControlView}, Path=TestText}" />
TestControl.xaml

MainPage.xaml

 <ListView  x:Name="lstView" HorizontalOptions="Fill" HasUnevenRows="True"  RefreshAllowed="true" IsPullToRefreshEnabled="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame CornerRadius="10" BackgroundColor="White" Margin="0,5,0,5">
                              <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
                                            <controls:TestControl TestText="{Binding Title}"  VerticalOptions="Center"/>
                                            <Label Text="{Binding Type}" FontSize="Medium" TextColor="#F0BB7F" FontAttributes="Bold" VerticalOptions="Center"/>
                              </StackLayout>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

MainPage.xaml.cs

 public partial class MainPage : ContentPage
 {
    public ObservableCollection<TestModel> veggies { get; set; }

    public MainPage()
    {
        InitializeComponent();

        veggies = new ObservableCollection<TestModel>();

        veggies.Add(new TestModel { Title = "Tomato", Type = "Fruit" });
        veggies.Add(new TestModel { Title = "Zucchini", Type = "Vegetable" });
        veggies.Add(new TestModel { Title = "Tomato" , Type = "Vegetable" });
        veggies.Add(new TestModel { Title = "Romaine", Type = "Fruit" });
        veggies.Add(new TestModel { Title = "Zucchini", Type = "Vegetable" });

        lstView.ItemsSource = veggies;

        BindingContext = this;
    }
}
public分部类主页面:ContentPage
{
公共可观测采集蔬菜{get;set;}
公共主页()
{
初始化组件();
蔬菜=新的可观察到的采集();
添加(新的TestModel{Title=“番茄”,Type=“水果”});
添加(新的TestModel{Title=“Zucchini”,Type=“vegeture”});
添加(新的TestModel{Title=“番茄”,Type=“蔬菜”});
添加(新的TestModel{Title=“Romaine”,Type=“Fruit”});
添加(新的TestModel{Title=“Zucchini”,Type=“vegeture”});
lstView.ItemsSource=蔬菜;
BindingContext=这个;
}
}
 <ListView  x:Name="lstView" HorizontalOptions="Fill" HasUnevenRows="True"  RefreshAllowed="true" IsPullToRefreshEnabled="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame CornerRadius="10" BackgroundColor="White" Margin="0,5,0,5">
                              <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
                                            <controls:TestControl TestText="{Binding Title}"  VerticalOptions="Center"/>
                                            <Label Text="{Binding Type}" FontSize="Medium" TextColor="#F0BB7F" FontAttributes="Bold" VerticalOptions="Center"/>
                              </StackLayout>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
 public partial class MainPage : ContentPage
 {
    public ObservableCollection<TestModel> veggies { get; set; }

    public MainPage()
    {
        InitializeComponent();

        veggies = new ObservableCollection<TestModel>();

        veggies.Add(new TestModel { Title = "Tomato", Type = "Fruit" });
        veggies.Add(new TestModel { Title = "Zucchini", Type = "Vegetable" });
        veggies.Add(new TestModel { Title = "Tomato" , Type = "Vegetable" });
        veggies.Add(new TestModel { Title = "Romaine", Type = "Fruit" });
        veggies.Add(new TestModel { Title = "Zucchini", Type = "Vegetable" });

        lstView.ItemsSource = veggies;

        BindingContext = this;
    }
}