xaml ControlTemplate 中的 TemplateBinding 不工作

TemplateBinding in xaml ControlTemplate is not working

在我位于 App.xaml 的 ControlTemplate 之外,我尝试从使用的 ViewModel 中获取一个布尔值 属性 以使元素(在本例中为 activityIndi​​cator)在内容 xaml 中可见.

属性:

Private bool isLoading;
public bool IsLoading
{
   get => this.isLoading;
   set => this.SetProperty(ref this.isLoading, value);
}

内容页:

ControlTemplate="{StaticResource Template__Page_Scrollable}"

ControlTemplate(我会将ActivityIndi​​cator集成到StackLayout中,但首先我只想通过将背景颜色设置为Aqua来显示StackLayout本身):

    <ControlTemplate x:Key="Template__Page_Scrollable">
        <AbsoluteLayout x:Name="ActivityIndicator">
            <ScrollView Style="{StaticResource Page_Scrollable__ScrollContainer}" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
                <ContentPresenter AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All"/>
            </ScrollView>
            <StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" 
                         IsEnabled="{TemplateBinding Parent.BindingContext.IsLoading}" 
                         IsVisible="{TemplateBinding Parent.BindingContext.IsLoading}" BackgroundColor="Aqua">
            </StackLayout>
        </AbsoluteLayout>
    </ControlTemplate>

根据我的研究,这应该可以工作,我收到消息 "Connot resolve symbol 'Parent'" 没有 'Parent' 结果我总是得到 true。

我试过例如:

如果您确实在 ContentPage 上设置了 ControlTemplate,例如:

<ContentPage 
...
ControlTemplate="{StaticResource Template__Page_Scrollable}">

这是不正确的。 ControlTemplate 中的 Parent 指的是托管控件模板的视图的父视图。 ContentPage 没有父视图。

相反,您需要在 ContentPage 中的 ContentView 上设置控件模板,例如:

<ContentPage ...>
    <ContentView ControlTemplate="{StaticResource Template__Page_Scrollable}" >
       ...
    </ContentView>
</ContentPage>