jby*_*yrd 5 user-controls templating xamarin xamarin-forms
Xamarin.Forms有一个包含概念吗?
我正在创建一个跨所有页面都有共享标头的应用.有没有办法创建一次标题并将其包含在所有页面上?更好的是,有没有办法创建模板或可重复使用的布局,您可以将所有内容放在每个页面内?这将是与.NET MVC _Layout文件类似的概念.
Ada*_*ley 12
您需要的是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>
Run Code Online (Sandbox Code Playgroud)
然后在您的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>
Run Code Online (Sandbox Code Playgroud)
然后你最终得到了
参考自:http://www.xamarinhelp.com/xamarin-forms-page-templates/
是.您可以使用用户控件.您可以仅使用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>
Run Code Online (Sandbox Code Playgroud)
我们将属性绑定Name, Age, CatAmount到三个不同的标签.我们假设,BindingContext该控件的类型是对象PersonData(见下文).在您的代码中,您还必须更改类型.
MyControl.xaml.cs
public partial class MyControl : StackLayout
{
public MyControl()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的页面中,您必须添加一个新的命名空间(例如local,指向您的程序集,例如App6或MyApp.Whatever).然后你可以通过它来使用它local:MyControl.在我们的示例控件中,我们绑定BindingContextto Person,它是我们的Page的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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在您提到的场景中,您可以简单地继承ContentPage并添加您的公共元素,并使用继承的Page作为页面的基类.
TemplatedPage - Xamarin.Forms 2.1
他们介绍了Xamarin.Forms 2.1 TemplatedPage.您可以在此处找到示例:http://xfcomplete.net/general/2016/01/20/control-templates/.适合您的场景的LoginView示例ContentPresenter.