Xamarin.Forms - "include"是否有局部 View 的机制?

标签 xamarin user-controls templating xamarin-forms

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>

然后你最终得到

enter image description here

引用自:http://www.xamarinhelp.com/xamarin-forms-page-templates/

关于Xamarin.Forms - "include"是否有局部 View 的机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203493/

相关文章:

ios - 使用 Xamarin 的 iOS 多路径 TCP

.net - 如何从标准的非托管非 .NET 应用程序调用 C++/CLI (.NET) DLL?

javascript - 如何访问不同页面中的hiddenfield值?

elixir - 在 Elixir(非 Phoenix)应用程序中渲染部分 EEx 模板

jsf - 如何在 Facelets 模板中引用 CSS/JS/图片资源?

c# - 导航栏的 View 杆底部如何放置?

android - 无法访问文件 :///android_asset 中的文件

c# - 多点连接 : iOS and Android

jquery - 如何在同一网页上多次使用Web用户控件?

jsf-2 - 覆盖 <ui :define>