reusable template
Eduardo Gomez Romero
465
Reputation points
In my collectionView, I have this
<CollectionView
Margin="20"
ItemsSource="{Binding Turbines}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:TurbinePin">
<!-- Use SwipeView for mobile -->
<OnIdiom x:TypeArguments="View">
<OnIdiom.Phone>
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Delete"
BackgroundColor="Red"
Command="{Binding Source={x:RelativeSource AncestorType={x:Type vm:TurbinesCollectionPageViewModel}}, Path=DeleteTurbineCommand}"
CommandParameter="{Binding Turbine}">
<SwipeItem.IconImageSource>
<FontImageSource Glyph="{x:Static constant:MaterialFonts.Delete}"
FontFamily="ma" />
</SwipeItem.IconImageSource>
</SwipeItem>
</SwipeItems>
</SwipeView.RightItems>
<Border>
<Border.StrokeShape>
<RoundRectangle CornerRadius="8" />
</Border.StrokeShape>
<Grid Padding="10"
ColumnDefinitions="*,30"
RowDefinitions="*,*">
<Label Text="{Binding Turbine.Name}" />
<Label Grid.Row="1"
Text="{Binding Turbine.Address}" />
<Label Grid.RowSpan="2"
Grid.Column="1"
FontFamily="ma"
FontSize="Medium"
HorizontalTextAlignment="End"
Text="{x:Static constant:MaterialFonts.Info}"
VerticalTextAlignment="Center" />
</Grid>
</Border>
</SwipeView>
</OnIdiom.Phone>
<OnIdiom.Desktop>
<Border>
<Border.StrokeShape>
<RoundRectangle CornerRadius="8" />
</Border.StrokeShape>
<Grid Padding="10"
ColumnDefinitions="*,30"
RowDefinitions="*,*">
<Label Text="{Binding Turbine.Name}" />
<Label Grid.Row="1"
Text="{Binding Turbine.Address}" />
<Label Grid.RowSpan="2"
Grid.Column="1"
FontFamily="ma"
FontSize="Medium"
HorizontalTextAlignment="End"
Text="{x:Static constant:MaterialFonts.Info}"
VerticalTextAlignment="Center" />
<!-- Delete button for desktop -->
<Button Grid.Column="1"
Command="{Binding Source={x:RelativeSource AncestorType={x:Type vm:TurbinesCollectionPageViewModel}}, Path=DeleteTurbineCommand}"
CommandParameter="{Binding Turbine}"
VerticalOptions="Center"
HorizontalOptions="End">
<Button.ImageSource>
<FontImageSource Glyph="{x:Static constant:MaterialFonts.Delete}"
FontFamily="ma" />
</Button.ImageSource>
</Button>
</Grid>
</Border>
</OnIdiom.Desktop>
</OnIdiom>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
As you can see I am duplicating this
<Border>
<Border.StrokeShape>
<RoundRectangle CornerRadius="8" />
</Border.StrokeShape>
<Grid Padding="10"
ColumnDefinitions="*,30"
RowDefinitions="*,*">
<Label Text="{Binding Turbine.Name}" />
<Label Grid.Row="1"
Text="{Binding Turbine.Address}" />
<Label Grid.RowSpan="2"
Grid.Column="1"
FontFamily="ma"
FontSize="Medium"
HorizontalTextAlignment="End"
Text="{x:Static constant:MaterialFonts.Info}"
VerticalTextAlignment="Center" />
</Grid>
I am also duplicating the deleteCommand and the fontImageSource
the only diferene is that in phones, I have a swipe and desktop a button with an icon
so how can I reduce this ode as much as posible
Sign in to answer