Hello,
I double-check the xaml code snippets you provided. On the view on phone, you added the swipe view (and a grid) without the delete button. On the view on desktop, there is Grid wrapping the label.
The structure of views varies greatly, and the reusable part is the following:
<Label Text="{Binding Turbine.Name}" />
<Label
Grid.Row="1"
Text="{Binding Turbine.Address}" />
<Label
Grid.Row="2"
Text="{Binding Turbine.LocalizedInstalationDateTime}" />
It's not very necessary to recuse the template. If you still want to recuse these three labels, you should create a custom control with bindable properties. And you could refer to the following code:
Custom Grid
<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ReuseCollectionview.CustomGrid"
x:Name="this">
<Grid BindingContext="{x:Reference this}"
Padding="10"
ColumnDefinitions="*,30"
RowDefinitions="*,*">
<Label Text="{Binding Name}" />
<Label Grid.Row="1"
Text="{Binding Address}" />
<Label Grid.RowSpan="2"
Grid.Column="1"
FontFamily="ma"
FontSize="Medium"
HorizontalTextAlignment="End"
Text="x:Static constant:MaterialFonts.Info"
VerticalTextAlignment="Center" />
</Grid>
</Grid>
public partial class CustomGrid : Grid
{
public static readonly BindableProperty NameProperty = BindableProperty.Create(nameof(Name), typeof(string), typeof(CustomGrid), string.Empty);
public static readonly BindableProperty AddressProperty = BindableProperty.Create(nameof(Address), typeof(string), typeof(CustomGrid), string.Empty);
public string Name
{
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
public string Address
{
get => (string)GetValue(AddressProperty);
set => SetValue(AddressProperty, value);
}
public CustomGrid()
{
InitializeComponent();
}
}
usage
<local:CustomGrid Name="{Binding Turbine.Name}" Address="{Binding Address}"></local:CustomGrid>
If you want to wrapper the whole view (Swipe view/Grid) or the whole DataTemplate, please feel free to let me know, we can work together to figure it out.
Update
I understand that you want to use the DataTemplateSelector
to choose the template, please refer to the following:
public class ChooseDataTemplateSelector : DataTemplateSelector
{
public DataTemplate PhoneTemplate { get; set; }
public DataTemplate DeskTopTemplate { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
return (DeviceInfo.Current.Idiom == DeviceIdiom.Desktop)? DeskTopTemplate : PhoneTemplate;
}
}
The page (Xaml)
<ContentPage ...
xmlns:local ="XXX">
<ContentPage.Resources>
<DataTemplate x:Key="PhoneTemplate">
<SwipeView >
...// your view on phone
</SwipeView>
</DataTemplate>
<DataTemplate x:Key="DeskTopTemplate">
<Border>
// your view on desktop
</Border>
</DataTemplate>
<local:ChooseDataTemplateSelector x:Key="SelectDataTemplateSelector"
PhoneTemplate="{StaticResource PhoneTemplate}"
DeskTopTemplate="{StaticResource DeskTopTemplate}" />
</ContentPage.Resources>
<CollectionView
Margin="20"
ItemsSource="{Binding Turbines}" ItemTemplate="{StaticResource SelectDataTemplateSelector}">
</CollectionView>
</ContentPage>
Best Regards,
Wenyan Zhang
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.