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

查看:523
本文介绍了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文件中, 然后将该组件包含在您的每个ContentPages中.

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.

编辑-添加了代码示例:

让我们添加一个标题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天全站免登陆