【发布时间】:2017-10-13 22:15:20
【问题描述】:
我一直在尝试为独立的ContentPage 以及CarouselPage 中的ContentPage 重用控制模板...
主要问题是CarouselPage 不支持ControlTemplate 属性。因此,我不得不在CarouselPage 的DataTemplate 中插入ContentPage。这个ContentPage 然后可以分配ControlTemplate,但我遇到了BindingContext 不是ViewModel 的根的问题。
我也会尝试用代码解释问题:
我已经创建了如下所示的模板。
<!-- Loader view template -->
<ControlTemplate x:Key="LoaderViewTemplate">
<AbsoluteLayout Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<!-- Content -->
<ContentPresenter AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" />
<!-- Loader -->
<BoxView IsVisible="{TemplateBinding BindingContext.IsBusy}" BackgroundColor="Green" Opacity="0.5" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" />
<StackLayout IsVisible="{TemplateBinding BindingContext.IsBusy}" Padding="6" BackgroundColor="Gray" Orientation="Horizontal" AbsoluteLayout.LayoutBounds="0.5, 0.5, -1, -1" AbsoluteLayout.LayoutFlags="PositionProportional">
<ActivityIndicator Color="White" IsRunning="{TemplateBinding BindingContext.IsBusy}" VerticalOptions="Center" WidthRequest="20" HeightRequest="20" />
<Label TextColor="White" Text="Loading..." VerticalOptions="Center" />
</StackLayout>
</AbsoluteLayout>
</ControlTemplate>
对于如下所示的ContentPage,模板正常工作。
<ContentPage ...
ControlTemplate="{StaticResource LoaderViewTemplate}">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
...
</StackLayout>
</ContentPage>
但它在CarouselPage 中不起作用,如下所示。
<CarouselPage ...
ItemsSource="{Binding Tournament.Rounds}">
<CarouselPage.ItemTemplate>
<DataTemplate>
<ContentPage ControlTemplate="{StaticResource LoaderViewTemplate}">
...
</ContentPage>
</DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>
CarouselPage 中的BindingContext 成为Tournament.Rounds 集合中的TournamentRoundModel。
有没有人知道我如何在独立的ContentPage 和嵌套的ContentPage 中到达ViewModel 的根?
亲切的问候, 乔普·米德尔坎普
【问题讨论】:
标签: xamarin xamarin.forms controltemplate controltemplates control-template