目前,有一个带有Xamarin导航的问题,它会导致目标页面的内容隐藏在页面顶部的导航栏下面。我正在尝试制作一个控制模板,以便将内容包装在一个具有足够的上限的容器中,以便将内容从导航栏下推到屏幕上。按照这里的指导,我现在有--
<?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>-似乎不管用.我在{RelativeSource TemplatedParent}获得了一个Intellisense高亮显示,其中包含以下错误XLS0414:
找不到'RelativeSource‘类型。确认您没有丢失程序集引用,并且所有引用的程序集都已生成。
在这里正确设置BindingContext需要做些什么?
发布于 2020-06-03 13:03:44
看来我需要再往下读一页。稍后,它有一节描述了如何在其中声明带TemplateBinding的Pass参数,
TemplateBinding标记扩展是创建使用RelativeSource标记扩展将模板中的根元素的BindingContext设置为模板父元素的RelativeSource的替代方法。TemplateBinding标记扩展消除了RelativeSource绑定,并将绑定表达式替换为TemplateBinding表达式。
而且更进一步,
使用TemplateBinding标记扩展相当于将模板中根元素的BindingContext设置为具有RelativeSource标记扩展的模板父元素,然后使用绑定标记扩展解析子对象的绑定。实际上,TemplateBinding标记扩展创建了一个来源为RelativeBindingSource.TemplatedParent的绑定。
应用这一点,我能够将上面的代码更改为:
<?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>而且效果很好。
https://stackoverflow.com/questions/62173193
复制相似问题