Xamarin.Forms - 重用c#布局

时间:2017-01-11 19:20:25

标签: c# xamarin xamarin.forms

我有一个用c#编码的布局,这是一个我希望在我所有其他活动中使用的“菜单”。有没有办法在我的其他页面中包含或重用此布局?

1 个答案:

答案 0 :(得分:1)

您只需将StackLayout添加到自己的类中,然后在任意位置重复使用。如果你想获得想象力并添加可绑定属性和类似的东西,那么有一些例子here for Xamarin's guide或另一个例子here

namespace App.Controls {

    public class CustomMenu : StackLayout {
        //Custom stuff here
    }
}

然后在你的XAML中使用它:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:App.Controls;assembly=App"
             x:Class="App.Pages.MyMenuPage">
  <controls:CustomMenu/>
</ContentPage>

或者在C#中:

public class MyContentPage : ContentPage {

    public MyContentPage() { Content = new CustomMenu(); }
}