Xamarin.Forms: Can I embed one ContentPage or ContentView into another ContentPage

前端 未结 2 1371
日久生厌
日久生厌 2021-01-01 19:51

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

2条回答
  •  失恋的感觉
    2021-01-01 20:19

    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.

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

    Edit - added a code sample:

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

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

    Then add the XAML code:

    
    
        
            
                
            
        
    
    

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

提交回复
热议问题