I'm stuck trying to reuse an control template for a independent ContentPage as well as a ContentPage in a CarouselPage...
The main problem is that the CarouselPage doesn't support the ControlTemplate property. Therefore I'm forced to insert a ContentPage in the DataTemplate of the CarouselPage. This ContentPage then can get the ControlTemplate assigned but I run into the problem that the BindingContext is not the root of the ViewModel.
I'll also try to explain the issues with code:
I've create the template as shown below.
<!-- 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>
The template is working correctly for the ContentPage shown below.
<ContentPage ...
ControlTemplate="{StaticResource LoaderViewTemplate}">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
...
</StackLayout>
</ContentPage>
But it doesn't work in the CarouselPage as shown below.
<CarouselPage ...
ItemsSource="{Binding Tournament.Rounds}">
<CarouselPage.ItemTemplate>
<DataTemplate>
<ContentPage ControlTemplate="{StaticResource LoaderViewTemplate}">
...
</ContentPage>
</DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>
The BindingContext in the CarouselPage becomes a TournamentRoundModel from the Tournament.Rounds collection.
Does any one has an idea on how I can reach the root of the ViewModel within the independent ContentPage and the CarouselPage nested ContentPage?
Kind regards, Jop Middelkamp
First of all if you need each
ContentPageinCarousalPageto be able to refer to the root view-model, while providing the same to theControlTemplate's binding(s).Simplest way to do that would be to extend
ContentPageto support a bindable property to hold this reference (to root view-model).Then you can update your shared control-template as:
Sample usage would look like:
and,
Furthermore, if
IsBusyis the only property that you need to refer inControlTemplate- you can create anIsBusybindable property in extended content-page; instead ofRootViewModel.