xaml - xaml ControlTemplate 中的 TemplateBinding 不起作用
问题描述
在 App.xaml 中放置的 ControlTemplate 中,我尝试从使用的 ViewModel 获取布尔属性,以使元素(在本例中为 activityIndicator)在 Content xaml 中可见。
财产:
Private bool isLoading;
public bool IsLoading
{
get => this.isLoading;
set => this.SetProperty(ref this.isLoading, value);
}
内容页:
ControlTemplate="{StaticResource Template__Page_Scrollable}"
ControlTemplate(我会将 ActivityIndicator 集成到 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>
由于我的研究,这应该可以通过我收到消息“无法解析符号'父'”而没有'父'我总是得到真实的结果。
我试过例如:
- 额外设置 BindingContext
- IsEnabled="{TemplateBinding BindingContext.IsLoading}"
- IsEnabled="{TemplateBinding IsLoading}"
- IsEnabled="{绑定 IsLoading}"
解决方案
如果你真的有你的ControlTemplate
设置ContentPage
,例如:
<ContentPage
...
ControlTemplate="{StaticResource Template__Page_Scrollable}">
这是不正确的。Parent
in 是ControlTemplate
指托管控件模板的视图的父视图。ContentPage 没有父视图。
相反,您需要在 ContentPage 的 ContentView 上设置控件模板,例如:
<ContentPage ...>
<ContentView ControlTemplate="{StaticResource Template__Page_Scrollable}" >
...
</ContentView>
</ContentPage>
推荐阅读
- svn - Tortoise SVN - 无法连接到 URL ' ' 处的存储库,SSL 通信期间发生错误
- javascript - 带有 emoji-mart 的 window.getSelection() 问题(Picker 组件)
- mysql - “where 子句”中有一个未知列“用户名”
- python - 如何在 Matplotlib 中制作 groupby 条形图
- reactjs - 在反应中实施redux后地图功能不起作用
- php - 从表中获取活动和非活动记录
- android - 错误:当我在数据库中有更多表时,AppDatabase_Impl.java 中的代码太大
- angular - 更新操作期间在 Angular 中管理本地存储数据的问题
- react-native - 如何在 React Native 中实现 Twilio SDK 调用?
- ios - 谷歌/Facebook 退出不是持久的