Xamarin.Forms 有包含的概念吗?
我正在创建一个跨所有页面共享标题的应用程序。有没有办法一次性创建标题并将其包含在所有页面上?更好的是,有没有办法创建模板或可重用的布局,您可以将所有内容放在每个页面中?这将是一个类似于 .NET MVC 的 _Layout
的概念。文件。
最佳答案
你需要的是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/
关于Xamarin.Forms - "include"是否有局部 View 的机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203493/