是否可以在另一个XAML块中包含一块XAML?

Ala*_*an2 14 xamarin xamarin.forms

我的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>
Run Code Online (Sandbox Code Playgroud)

代码显示在两页上.我想把它留作XAML.

有没有办法可以将这个XAML放入一个文件中,并将其包含在两个页面中的每个页面的其他XAML中.请注意,我不想将所有内容转换为C#,因为我有很多像这样的实例.

sme*_*sme 21

创建一个名为Templates的目录,然后创建一个新的View类,MyCustomGrid如下所示:

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>
Run Code Online (Sandbox Code Playgroud)

模板/ MyCustomGrid.xaml.cs

using Xamarin.Forms;

namespace MyProject.Templates
{
    public partial class MyCustomGrid : Grid
    {
        public MyCustomGrid()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要在另一个页面中使用它,即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>
Run Code Online (Sandbox Code Playgroud)


小智 5

在ContentView中插入代码.在ContentPages中调用此代码.

https://forums.xamarin.com/discussion/87415/how-to-display-content-from-one-xaml-inside-another

  • 你能为此提供一个例子而不是链接吗?谢谢 (3认同)