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

前端 未结 2 1372
日久生厌
日久生厌 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:14

    Thank you very much IdoT, It did work for me, but after adding some lines. As this will help in making templates/custom controls/sub forms, easily added/shared across Xamarin.Forms.

    Here is my full work based on your suggestion, so it can be used as is with others:

    HeaderNavigationBar.xaml

    
    
    
        

    As you can see, added:

    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    

    and in the HeaderNavigationBar.cs, it was declared as StackLayout:

    HeaderNavigationBar.cs

    using Xamarin.Forms;
    
    namespace App9.MVC.Views
    {
        public partial class HeaderNavigationBar : StackLayout
        {
            public HeaderNavigationBar()
            {
                InitializeComponent();
            }
        }
    }
    

    then for the page that will hold it/show it:

    MainView.xaml

    
    
    
        
            
                

    As you can notice, the namespace has the full path, in the MainView:

    xmlns:common="clr-namespace:App9.MVC.Views;assembly=App9"
    

    Also, you can notice that there is a button called External 1, this will also show with the Internal buttons, as the control is a StackLayout, so it can handle adding more controls.

    Screenshot:

    Also for Page inside another Page:

    • https://github.com/twintechs/TwinTechsFormsLib
    • http://blog.twintechs.com/advanced-xamarin-forms-techniques-for-flexible-and-performant-cross-platform-apps-part-5-page-in-page-embedding

    Again thanks goes to IdoT.

提交回复
热议问题