如何创建可用于所有xamarin表单页面的页脚?

时间:2017-03-23 12:14:44

标签: xaml xamarin xamarin.forms footer

我正在创建一个应用程序,其中我想要所有xaml页面通用的页脚。我尝试了在此链接上给出的解决方案xamarin forum

但它显示部分类bcoz的错误我正在使用 xamarin xaml form 页面继承类 PageToInherit 。如果我只使用页面(仅.cs文件) )然后没有错误。

public partial class TodoList : PageToInherit
{
    public TodoList()
    {
        InitializeComponent();
        this.Title = "TodoList page";
        Button button = new Button();
        button.Text = "BUTTON 1";

        MainStackLayout.Children.Add(button);

        Content = MainStackLayout;
    }
}

2 个答案:

答案 0 :(得分:3)

我想你可以看看TemplatedPage

创建ControlTemplate

<?xml version="1.0" encoding="utf-8" ?>
<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Mobile.App">
  <Application.Resources>
    <ResourceDictionary>
      <ControlTemplate x:Key="MainPageTemplate">
        <StackLayout>
          <Label Text="Header Content" FontSize="24" />         
          <ContentPresenter />
        </StackLayout>
      </ControlTemplate>
    </ResourceDictionary>
  </Application.Resources>
</Application>

应用ControlTemplate

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               x:Class="Mobile.MainPage"
               ControlTemplate="{StaticResource MainPageTemplate}">

  <Label Text="Main Page Content" FontSize="18" />

</ContentPage>

使用TemplateBinding

<ControlTemplate x:Key="MainPageTemplate">
  <StackLayout>
    <Label Text="{TemplateBinding BindingContext.HeadingText}" FontSize="24" />         
    <ContentPresenter />
  </StackLayout>
</ControlTemplate>

这里是Blog

答案 1 :(得分:1)

对于仍在寻找此事的人。

使用ContentView创建可重复使用的xaml视图。

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxx.FooterView">
    <ContentView.Content>
        <RelativeLayout VerticalOptions="End" HorizontalOptions="Fill" HeightRequest="42" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}">
                <Label HeightRequest="42" VerticalTextAlignment="Center" Text="Copyright © 2017. All rights reserved." HorizontalTextAlignment="Center" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" />
            </RelativeLayout>
    </ContentView.Content>
</ContentView>

并在您的页面中使用它,就像任何控件一样

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxx.Page1" xmlns:local="clr-namespace:xxx;assembly=xxx" >
    <local:FooterView VerticalOptions="End" HeightRequest="45"/>
//other views and layouts
</ContentPage>

希望它有所帮助。