Xamarin.Forms - 创建主布局模板的方法?

时间:2016-04-28 01:53:29

标签: templates layout xamarin xamarin.forms

在Xamarin.Forms(C#代码, XAML)中,有没有办法创建可以在多个页面上重用的布局模板?例如,我想在我的移动应用程序中使用持久性页眉和页脚,但我不想在每个页面上都包含它们。许多软件技术(例如Jade,.NET MVC _Layout.cshtml文件等)允许创建可以重用的布局模板,并且可以将内容“注入”。 Xamarin.Forms有这样的东西吗?

注意:问题与this one相同,但有没有办法在C#代码中执行此操作?

2 个答案:

答案 0 :(得分:2)

Xamarin Forms 2.1引入了self.main_widget = QtGui.QWidget(self) self.plot_widget = QtGui.QWidget(self.main_widget) self.plot_widget.setGeometry(250,180,500,600) self.figure = plt.figure() self.plotting = FigureCanvas(self.figure) self.plot() plot_box = QtGui.QVBoxLayout() plot_box.addWidget(self.plotting) self.plot_widget.setLayout(plot_box) ControlTemplate

http://xamarinhelp.com/xamarin-forms-page-templates/

  

TemplatedPage控件模板提供了在运行时轻松主题和重新设置主题应用程序页面的功能。

     

要在应用程序级别定义ControlTemplate,必须创建一个表示ControlTemplate的类。该类应该从用于模板的布局派生,如以下代码示例所示:

Xamarin.Forms
  

以下代码示例显示了将TealTemplate应用于ContentView的ContentPage:

class TealTemplate : Grid
{
  public TealTemplate ()
  {
    ...
    var contentPresenter = new ContentPresenter ();
    Children.Add (contentPresenter, 0, 1);
    Grid.SetColumnSpan (contentPresenter, 2);
    ...
  }
}

class AquaTemplate : Grid
{
  ...
}

答案 1 :(得分:0)

就你个人而言,我个人会选择a templated view

我将与您分享一个例子,因为在发布给nuget时,这个文档记录很少,而且您可以在博客上找到大多数文档。

PCL:

public class ApplicationHeader : TemplatedView
{
    protected override void OnChildAdded(Element child)
    {
        base.OnChildAdded(child);
        SetInheritedBindingContext(child, BindingContext);
    }

    #region HomeButtonStyle

    public static BindableProperty HomeButtonStyleProperty = BindableProperty.Create<ApplicationHeader, Style>(d => d.HomeButtonStyle, default(Style));

    public Style HomeButtonStyle
    {
        get { return (Style) GetValue(HomeButtonStyleProperty); }
        set { SetValue(HomeButtonStyleProperty, value); }
    }

    #endregion HomeButtonStyle

    #region HomeButtonCommand

    public static BindableProperty HomeButtonCommandProperty = BindableProperty.Create<ApplicationHeader, ICommand>(d => d.HomeButtonCommand, default(ICommand), defaultBindingMode: BindingMode.OneWay);

    public ICommand HomeButtonCommand
    {
        get { return (ICommand) GetValue(HomeButtonCommandProperty); }
        set { SetValue(HomeButtonCommandProperty, value); }
    }

    #endregion HomeButtonCommand

    #region CommandAreaContent


    public static BindableProperty CommandAreaContentProperty = BindableProperty.Create<ApplicationHeader, View>(d => d.CommandAreaContent, default(View), defaultBindingMode: BindingMode.OneWay);

    public View CommandAreaContent
    {
        get { return (View) GetValue(CommandAreaContentProperty); }
        set { SetValue(CommandAreaContentProperty, value); }
    }

    #endregion CommandAreaContent

    #region HeaderTextLine1

    public static BindableProperty HeaderTextLine1Property = BindableProperty.Create<ApplicationHeader, string>(d => d.HeaderTextLine1, default(string), defaultBindingMode: BindingMode.OneWay);

    public string HeaderTextLine1
    {
        get { return (string) GetValue(HeaderTextLine1Property); }
        set { SetValue(HeaderTextLine1Property, value); }
    }

    #endregion HeaderTextLine1

    #region HeaderTextLine2

    public static BindableProperty HeaderTextLine2Property = BindableProperty.Create<ApplicationHeader, string>(d => d.HeaderTextLine2, default(string), defaultBindingMode: BindingMode.OneWay);

    public string HeaderTextLine2
    {
        get { return (string) GetValue(HeaderTextLine2Property); }
        set { SetValue(HeaderTextLine2Property, value); }
    }

    #endregion HeaderTextLine2

    #region HeaderTapCommand

    public static BindableProperty HeaderTapCommandProperty = BindableProperty.Create<ApplicationHeader, ICommand>(d => d.HeaderTapCommand, default(ICommand), defaultBindingMode: BindingMode.OneWay);

    public ICommand HeaderTapCommand
    {
        get { return (ICommand) GetValue(HeaderTapCommandProperty); }
        set { SetValue(HeaderTapCommandProperty, value); }
    }

    #endregion HeaderTapCommand
}

的App.xaml

请记住,当前的资源声明必须是为了避免app.xaml异常。                                                                                                                                                                                                                                                                                                           

                            <controls:EnhancedButton Padding="24,0,24,0" IsVisible="{TemplateBinding HomeButtonCommand, Converter={StaticResource NullToBoolConverter}, ConverterParameter={x:Static x:Boolean.TrueString}}" Style="{StaticResource ButtonThemedDark}" Grid.Column="0" Text="=" Command="{TemplateBinding HomeButtonCommand}"></controls:EnhancedButton>
                            <Grid Grid.Column="1" extensions:GestureExtensions.TapCommand="{TemplateBinding HeaderTapCommand}" BackgroundColor="{StaticResource ColorThemeAccent}">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Label Grid.Row="1" Text="{TemplateBinding HeaderTextLine1}" Style="{StaticResource LabelShellHeaderLine1}" IsVisible="{TemplateBinding HeaderTextLine1, Converter={StaticResource NullToBoolConverter}, ConverterParameter={x:Static x:Boolean.TrueString}}"></Label>
                                <Label Grid.Row="2" Text="{TemplateBinding HeaderTextLine2}" Style="{StaticResource LabelShellHeaderLine2}" IsVisible="{TemplateBinding HeaderTextLine2, Converter={StaticResource NullToBoolConverter}, ConverterParameter={x:Static x:Boolean.TrueString}}"></Label>
                            </Grid>
                            <controls:EnhancedContentPresenter Grid.Column="2" Content="{TemplateBinding CommandAreaContent}"></controls:EnhancedContentPresenter>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>