简体   繁体   English

Xamarin TemplateControl 相对源绑定

[英]Xamarin TemplateControl RelativeSource Binding

Currently there is an issue with Xamarin Shell navigation that causes the destination page's content to be hidden underneath the navigation bar at the top of the page.目前 Xamarin Shell 导航存在问题,导致目标页面的内容隐藏在页面顶部的导航栏下方。 I am trying to make a control template to wrap the content in a container with a sufficient top-margin to shove the content down the screen, out from under the nav bar.我正在尝试制作一个控制模板来将内容包装在一个具有足够上边距的容器中,以便将内容从导航栏下方推到屏幕下方。 Following the guidance here , what I have currently --按照这里的指导,我目前所拥有的——

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Y12.Mobile.MovePlan.App">
    <Application.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="NavBarContentControlTemplate">
                <StackLayout
                    BindingContext="{Binding Source={RelativeSource TemplatedParent}}"
                    Margin="0, 50, 0, 0">
                    <Label Text="Hello Template!"></Label>
                    <ContentView Content="{Binding Content}"></ContentView>
                </StackLayout>
            </ControlTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

-- doesn't seem to work. ——好像没用。 I get an Intellisense highlight at {RelativeSource TemplatedParent} , with the following error XLS0414:我在{RelativeSource TemplatedParent}获得了 Intellisense 突出显示,并出现以下错误 XLS0414:

The type 'RelativeSource' was not found.找不到类型“RelativeSource”。 Verify that you are not missing an assembly reference and that all referenced assemblies have been built.确认您没有丢失程序集引用并且所有引用的程序集都已构建。

What do I need to do to set the BindingContext properly here?我需要做什么才能在此处正确设置BindingContext

Looks like I needed to read further down the page.看起来我需要进一步阅读页面。 Later it has a section describing how to Pass parameters with TemplateBinding where it states,稍后它有一个部分描述了如何使用 TemplateBinding 传递参数

The TemplateBinding markup extension is an alternative to creating a ControlTemplate that uses the RelativeSource markup extension to set the BindingContext of the root element in the template to its templated parent. TemplateBinding 标记扩展是创建 ControlTemplate 的替代方法,该 ControlTemplate 使用 RelativeSource 标记扩展将模板中根元素的 BindingContext 设置为其模板化父元素。 The TemplateBinding markup extension eliminates the RelativeSource binding, and replaces the Binding expressions with TemplateBinding expressions. TemplateBinding 标记扩展消除了RelativeSource 绑定,并将Binding 表达式替换为TemplateBinding 表达式。

And further,并进一步,

Using the TemplateBinding markup extension is equivalent to setting the BindingContext of the root element in the template to its templated parent with the RelativeSource markup extension, and then resolving bindings of child objects with the Binding markup extension.使用 TemplateBinding 标记扩展等效于将模板中根元素的 BindingContext 设置为使用 RelativeSource 标记扩展的模板化父项,然后使用 Binding 标记扩展解析子对象的绑定。 In fact, the TemplateBinding markup extension creates a Binding whose Source is RelativeBindingSource.TemplatedParent.实际上,TemplateBinding 标记扩展创建了一个其 Source 为 RelativeBindingSource.TemplatedParent 的 Binding。

Applying this, I was able to change the above code to the following:应用它,我能够将上面的代码更改为以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Y12.Mobile.MovePlan.App">
    <Application.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="NavBarContentControlTemplate">
                <StackLayout
                    Margin="0, 50, 0, 0">
                    <Label Text="Hello Template!"></Label>
                    <ContentView Content="{TemplateBinding Content}"></ContentView>
                </StackLayout>
            </ControlTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

And it works just fine.它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM