【发布时间】:2016-03-24 15:08:45
【问题描述】:
Xamarin.Forms 有包含的概念吗?
我正在创建一个在所有页面上都有一个共享标题的应用。有没有办法创建一次标题并将其包含在所有页面上?更好的是,有没有一种方法可以创建模板或可重用布局,您可以将所有内容放入每个页面?这将是与 .NET MVC 的 _Layout 文件类似的概念。
【问题讨论】:
标签: xamarin user-controls templating xamarin-forms
Xamarin.Forms 有包含的概念吗?
我正在创建一个在所有页面上都有一个共享标题的应用。有没有办法创建一次标题并将其包含在所有页面上?更好的是,有没有一种方法可以创建模板或可重用布局,您可以将所有内容放入每个页面?这将是与 .NET MVC 的 _Layout 文件类似的概念。
【问题讨论】:
标签: xamarin user-controls templating xamarin-forms
是的。您可以为此使用用户控件。您可以仅使用 XAML 或代码。我将解释 XAML 方式。
只需添加一个新的 XAML 页面并将根类型从 ContentPage 更改为 StackLayout。根类型可以是其他所有布局或控件。你必须决定什么最适合。
MyControl.xaml
<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App6.MyControl">
<Label Text="{Binding Name}" />
<Label Text="{Binding Age}" />
<Label Text="{Binding CatAmount}" />
</StackLayout>
我们将属性Name, Age, CatAmount 绑定到三个不同的标签。我们假设,该控件的BindingContext 是PersonData 类型的对象(见下文)。
在后面的代码中,您还必须更改类型。
MyControl.xaml.cs
public partial class MyControl : StackLayout
{
public MyControl()
{
InitializeComponent();
}
}
在您的页面中,您必须添加一个新的命名空间(例如 local 指向您的程序集,例如 App6 或 MyApp.Whatever)。然后您可以通过local:MyControl 使用它。在我们的示例控件中,我们将BindingContext 绑定到Person,这是我们页面的BindingContext 的一个属性,即(在我们的例子中)页面本身。如果您的控件位于子命名空间中,则必须相应地更改命名空间部分。
Page2.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App6;assembly=App6"
x:Class="App6.Page2">
<local:MyControl BindingContext="{Binding Person}"></local:MyControl>
</ContentPage>
Page2.xaml.cs
public class PersonData
{
public string Name { get; set; }
public int Age { get; set; }
public int CatAmount { get; set; }
}
public partial class Page2 : ContentPage
{
public PersonData Person { get; set; }
public Page2()
{
Person = new PersonData {Age = 28, Name = "Sven", CatAmount = 2};
InitializeComponent();
BindingContext = this;
}
}
在您提到的场景中,您可以简单地从ContentPage 继承并添加您的公共元素并将您继承的 Page 作为页面的基类。
TemplatedPage - Xamarin.Forms 2.1
他们在 Xamarin.Forms 2.1 中引入了TemplatedPage。您可以在此处找到示例:http://xfcomplete.net/general/2016/01/20/control-templates/。带有ContentPresenter 的LoginView 示例完全适合您的场景。
【讨论】:
你需要的是2.1.0中引入的ControlTemplate。
在 Application.Resources 的 ResourceDictionary 中创建一个控件模板。
<?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>
然后在你的 ContentPage 中,分配 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>
然后你会得到
引用自:http://www.xamarinhelp.com/xamarin-forms-page-templates/
【讨论】: