Hello,
If you want to use different DataTemplate for different platforms, the most recommended way is to dynamically generate it during rendering through DataTemplateSeclector
.
You can refer to the following documents and code snippets:
Code:
public class PlatformDatatemplateSelector : DataTemplateSelector
{
public DataTemplate PhoneTemplate { get; set; }
public DataTemplate DeskTopTemplate { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
#if WINDOWS
return DeskTopTemplate;
#else
return PhoneTemplate;
#endif
}
}
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MauiApp91"
x:Class="MauiApp91.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="PhoneTemplate">
<SwipeView>
...
</SwipeView>
</DataTemplate>
<DataTemplate x:Key="DeskTopTemplate">
<Border>
...
</Border>
</DataTemplate>
<vm:PlatformDatatemplateSelector x:Key="platformDatatemplateSelector"
PhoneTemplate="{StaticResource PhoneTemplate}"
DeskTopTemplate="{StaticResource DeskTopTemplate}"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.BindingContext>
<vm:MyViewModel/>
</ContentPage.BindingContext>
<VerticalStackLayout >
<CollectionView
Margin="20"
ItemsSource="{Binding Turbines}" ItemTemplate="{StaticResource platformDatatemplateSelector}">
</CollectionView>
</VerticalStackLayout>
</ContentPage>
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.