Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include a block of XAML in another block of XAML?

Tags:

I have XAML that looks like this:

<Grid VerticalOptions="Start">    <Grid.ColumnDefinitions>       <ColumnDefinition Width="Auto" />       <ColumnDefinition Width="*" />    </Grid.ColumnDefinitions>    <Label Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" Text="Exclude Hidden" Style="{StaticResource helpDetail}" />    <Label Grid.Row="0" Grid.Column="1" HorizontalOptions="Start" Text="All cards except those tagged as hidden" Style="{StaticResource helpDetail}" />    <Label Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" Text="Include Hidden" Style="{StaticResource helpDetail}" />    <Label Grid.Row="1" Grid.Column="1" HorizontalOptions="Start" Text="All cards including those tagged as hidden" Style="{StaticResource helpDetail}" />    <Label Grid.Row="2" Grid.Column="0" HorizontalOptions="Start" Text="Favorites" Style="{StaticResource helpDetail}" />    <Label Grid.Row="2" Grid.Column="1" HorizontalOptions="Start" Text="Only cards tagged as favorites" Style="{StaticResource helpDetail}" />    <Label Grid.Row="3" Grid.Column="0" HorizontalOptions="Start" Text="Hidden" Style="{StaticResource helpDetail}" />    <Label Grid.Row="3" Grid.Column="1" HorizontalOptions="Start" Text="Only those cards tagged as hidden" Style="{StaticResource helpDetail}" /> </Grid> 

The code appears on two pages. I would like to leave it as XAML.

Is there a way that I can put this XAML into a file and include it in the other XAML's for each of the two pages. Note that I don't want to convert everything to C# as I have many instances like this.

like image 579
Alan2 Avatar asked Jan 02 '18 02:01

Alan2


People also ask

What replaced XAML?

The XAML Previewer has been deprecated in Visual Studio 2019 version 16.8 and Visual Studio for Mac version 8.8, and replaced by the XAML Hot Reload feature in Visual Studio 2019 version 16.9 and Visual Studio for Mac version 8.9.

How do I add XAML files to WPF project?

Just right click the project, choose Add -> Window and that will add a new xaml file along with its corresponding .

Is XAML compiled?

XAML can be optionally compiled directly into intermediate language (IL) with the XAML compiler (XAMLC).


1 Answers

Create a directory called Templates, and then create a new View class, MyCustomGrid like so:

Templates/MyCustomGrid.xaml:

<?xml version="1.0" encoding="UTF-8"?> <Grid xmlns="http://xamarin.com/schemas/2014/forms"     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"     x:Class="MyProject.Templates.MyCustomGrid">     <Grid.ColumnDefinitions>         <ColumnDefinition Width="Auto" />         <ColumnDefinition Width="*" />     </Grid.ColumnDefinitions>     <Label Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" Text="Exclude Hidden" Style="{StaticResource helpDetail}" />     <Label Grid.Row="0" Grid.Column="1" HorizontalOptions="Start" Text="All cards except those tagged as hidden" Style="{StaticResource helpDetail}" />     <Label Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" Text="Include Hidden" Style="{StaticResource helpDetail}" />     <Label Grid.Row="1" Grid.Column="1" HorizontalOptions="Start" Text="All cards including those tagged as hidden" Style="{StaticResource helpDetail}" />     <Label Grid.Row="2" Grid.Column="0" HorizontalOptions="Start" Text="Favorites" Style="{StaticResource helpDetail}" />     <Label Grid.Row="2" Grid.Column="1" HorizontalOptions="Start" Text="Only cards tagged as favorites" Style="{StaticResource helpDetail}" />     <Label Grid.Row="3" Grid.Column="0" HorizontalOptions="Start" Text="Hidden" Style="{StaticResource helpDetail}" />     <Label Grid.Row="3" Grid.Column="1" HorizontalOptions="Start" Text="Only those cards tagged as hidden" Style="{StaticResource helpDetail}" /> </Grid> 

Templates/MyCustomGrid.xaml.cs

using Xamarin.Forms;  namespace MyProject.Templates {     public partial class MyCustomGrid : Grid     {         public MyCustomGrid()         {             InitializeComponent();         }     } } 

To use it in another page, ie MyPage.xaml:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"     xmlns:templates="clr-namespace:MyProject.Templates;assembly=MyProject"     x:Class="MyProject.MyPage">      <templates:MyCustomGrid />  </ContentPage> 
like image 96
sme Avatar answered Oct 16 '22 21:10

sme