Is it possible to include a block of XAML in another block of XAML?

后端 未结 3 1355
遇见更好的自我
遇见更好的自我 2021-02-07 03:13

I have XAML that looks like this:


   
      

        
3条回答
  •  长情又很酷
    2021-02-07 03:41

    In this case/dilemma, you can leverage Xamarin's ContentView class.

    You can think of ContentView as a sub-class or child of a ContentPage. So, basically, if you have a Xamarin page that is a ContentPage. And a ContentView can be a view or sub-element within a ContentPage. Both of them can have their own structure:

    A ContentPage XAML (ex. MainContentPage.xaml)

     
        
           
              
              ...
              
           
        
    

    A ContentView XAML (ex. ChildContentView.xaml)

     
        
           
              
              ...
              
           
        
    

    And the ContentPage class (ex. MainContentPage.cs) would look possibly like this:

     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();
              }
          }
     }
    

    And in your ChildContentView.cs (code behind) you can basically do the same coding as what you can do in the code behind of any ContentPage class. You can add StackLayout, you can do binding, and you can use other inherited methods from the TemplatedView parent class. See this link for more information.

提交回复
热议问题