xamarin - 从 ControlTemplate 绑定(bind)到 ContentPage 标题

标签 xamarin xamarin.forms

我有一个相当简单的 Xamarin.Forms应用程序,带有 ContentPage和一个 ControlTemplateApp.Xaml ResourceDictionary

我正在尝试绑定(bind)到 ContentPage.Title父 ContentPage 的属性。但是以下似乎不起作用..

内容页

<ContentPage
    x:Class="Inhouse.Mobile.Pcl.Views.MoveLocationView"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    ControlTemplate="{StaticResource MainPageTemplate}"
    Title="Move Location"
    ...

控制模板

<?xml version="1.0" encoding="utf-8" ?>
<Application
    x:Class="Inhouse.Mobile.Pcl.App"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="MainPageTemplate">

                <cut for Brevity ...>

                    <StackLayout BackgroundColor="#333333">
                        <Label Text="{TemplateBinding Page.Title}"></Label>
                    </StackLayout>

                    <ScrollView Grid.Row="2" >
                        <ContentPresenter VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
                    </ScrollView>

            </ControlTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我不确定我实际上应该绑定(bind)什么

即我的尝试是

<Label Text="{TemplateBinding Page.Title}"></Label>

最佳答案

我的问题是我在 StackLayout 中并且需要设置

Horizo​​ntalOptions="StartAndExpand" 这样我就可以看到它。

此外,标题就足够了

{TemplateBinding Title}

<StackLayout Grid.Row="1" BackgroundColor="#333333">
   <Label Text="{TemplateBinding Title}" TextColor="AliceBlue" HorizontalOptions="StartAndExpand"/>
</StackLayout>

关于xamarin - 从 ControlTemplate 绑定(bind)到 ContentPage 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45341184/

相关文章:

c# - 尝试使用带有 c# 的 xamarin android 打开默认浏览器

xaml - XAML 资源字典中的算术运算

xamarin.forms - 当用户点击导航时打开通知列表页面

xamarin - 基于跨多个文件的其他样式的样式

xamarin.ios - Monodevelop - 它什么时候保存 csproj 文件?

c# - 尝试将经度和纬度转换为地址名称,但它跳过了太多帧

c# - 如何在 ios xamarin 的文本字段中禁用空格字符

c# - 如何使用 Xamarin for Visual Studio 在真实的 Android 设备上进行调试?

Xamarin WebView 缩放以适应

listview - 如何将绑定(bind)上下文标签值保存到xamarin表单中的字符串