Xamarin.Forms:我可以将一个 ContentPage 或 ContentView 嵌入到另一个 ContentPage 中吗 [英] Xamarin.Forms: Can I embed one ContentPage or ContentView into another ContentPage

查看:19
本文介绍了Xamarin.Forms:我可以将一个 ContentPage 或 ContentView 嵌入到另一个 ContentPage 中吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Xamarin 项目中有多个 ContentPage xaml 文件.我想将一个共享的 xaml 嵌入到每个 ContentPage 中.共享的 xaml 部分没有什么特别之处(它不需要做任何特定于平台的事情).难道不应该像在 ContentPage 的 xaml 中嵌入标记以包含共享的 xaml 文件一样简单吗?有人能指出我正确的方向吗?

I have multiple ContentPage xaml files in my Xamarin project. I want to embed a shared piece of xaml into each ContentPage. There is nothing particularly special about the shared piece of xaml (it does't need to do anything platform specific). Shouldn't it be just as easy as embedding a tag in the xaml of the ContentPage to include the shared xaml file? Can someone point me in the right direction?

推荐答案

您可以使用 ContentPage 的父子项(例如,一个包含所有内容的 StackLayout孩子),把它放在一个外部的 xaml 文件中,然后将该组件包含在您的每个 ContentPage 中.

You can take the parent child of your ContentPage (for example, a StackLayout that wraps all the children), put it in an external xaml file, and then include that component in each one of your ContentPages.

外部 xaml 文件将是 StackLayout 类型,而不是 ContentPage.

The external xaml file will be a StackLayout type, rather than a ContentPage.

编辑 - 添加了代码示例:

Edit - added a code sample:

让我们添加一个标题 StackLayout:我们在类后面添加一个代码:

Let's add a header StackLayout: we add a code behind class:

public partial class HeaderNavigationBar 
{
    public HeaderNavigationBar()
    {
        InitializeComponent();
    }
}

然后添加 XAML 代码:

Then add the XAML code:

<StackLayout x:Class="HeaderNavigationBar"
             Orientation="Horizontal"
             HorizontalOptions="FillAndExpand"
             Padding="10"
             BackgroundColor="White">

    <Image Source="burger_icon"
           HorizontalOptions="StartAndExpand"
           Aspect="AspectFit">
        <Image.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding SlideNavigationDrawerCommand}" />
        </Image.GestureRecognizers>
    </Image>
</StackLayout>

最后,在您想要重用组件的页面中 - 添加以下引用:<HeaderNavigationBar/>.

And finally, in the page where you want to reuse your component - add this reference:<HeaderNavigationBar />.

这篇关于Xamarin.Forms:我可以将一个 ContentPage 或 ContentView 嵌入到另一个 ContentPage 中吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆