我的 XAML 看起来像这样:
<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>
该代码出现在两页上。我想将其保留为 XAML。
有没有一种方法可以将此 XAML 放入一个文件中,并将其包含在两个页面中每个页面的其他 XAML 中。请注意,我不想将所有内容都转换为 C#,因为我有很多这样的实例。
创建一个名为Templates的目录,然后创建一个新的View类,MyCustomGrid
像这样:
模板/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>
模板/MyCustomGrid.xaml.cs
using Xamarin.Forms;
namespace MyProject.Templates
{
public partial class MyCustomGrid : Grid
{
public MyCustomGrid()
{
InitializeComponent();
}
}
}
要在另一个页面中使用它,即我的页面.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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)