我的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#,因为我有很多这样的实例。
答案 0 :(得分:20)
创建一个名为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();
}
}
}
在另一个页面中使用它,即 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>
答案 1 :(得分:5)
将代码插入ContentView。在ContentPages中调用此代码。
https://forums.xamarin.com/discussion/87415/how-to-display-content-from-one-xaml-inside-another
答案 2 :(得分:2)
在这种情况/困境中,您可以利用Xamarin的ContentView类。
您可以将ContentView视为ContentPage的子类或子级。所以,基本上,如果你有一个Xamarin页面是一个ContentPage。 ContentView可以是ContentPage中的视图或子元素。它们都有自己的结构:
ContentPage XAML(例如MainContentPage.xaml)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage>
<ContentPage.Content>
<StackLayout x:Name="MainLayout">
...
</StackLayout>
</ContentPage.Content>
</ContentPage>
ContentView XAML(例如ChildContentView.xaml)
<?xml version="1.0" encoding="utf-8" ?>
<ContentView>
<ContentView.Content>
<StackLayout>
...
</StackLayout>
</ContentView.Content>
</ContentView>
ContentPage类(例如MainContentPage.cs)可能看起来像这样:
namespace Your.Package
{
public class MainContentPage : ContentPage
{
public MainContentPage()
{
//Some code here, binding, initialization etc.
//Create an object of the ChildContentView code behind/class
ChildContentView childView = new ChildContentView();
//Add the childView to the MainLayout of the ContentPage
this.MainLayout.Children.Add(childView);
InitializeComponent();
}
}
}
在您的ChildContentView.cs(后面的代码)中,您基本上可以执行与在任何ContentPage类后面的代码中可以执行的操作相同的编码。您可以添加StackLayout,可以进行绑定,也可以使用TemplatedView父类中的其他继承方法。有关详细信息,请参阅this link。