Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单控制模板TapGestureRecognitor命令绑定_C#_Xamarin_Xamarin.forms_Controltemplate_Theming - Fatal编程技术网

C# Xamarin表单控制模板TapGestureRecognitor命令绑定

C# Xamarin表单控制模板TapGestureRecognitor命令绑定,c#,xamarin,xamarin.forms,controltemplate,theming,C#,Xamarin,Xamarin.forms,Controltemplate,Theming,我有一个ControlTemplate,我打算用它为我的应用程序中的大多数页面提供基本功能 关于该想法的良好参考: 我在尝试将TapGestureRecognitor绑定到绑定上下文中的ICommand时遇到了一些奇怪的情况。我可以使用一个按钮绑定到视图中的ICommand属性,但根本不用TapGestureRecognitor 我有一个后向箭头图像,我想在大多数页面上使用它进行向后导航,如果我可以让命令绑定到我的视图(就像使用按钮时那样),使用带有TapGestureRecognit

我有一个ControlTemplate,我打算用它为我的应用程序中的大多数页面提供基本功能

关于该想法的良好参考:

我在尝试将TapGestureRecognitor绑定到绑定上下文中的ICommand时遇到了一些奇怪的情况。我可以使用一个按钮绑定到视图中的ICommand属性,但根本不用TapGestureRecognitor

我有一个后向箭头图像,我想在大多数页面上使用它进行向后导航,如果我可以让命令绑定到我的视图(就像使用按钮时那样),使用带有TapGestureRecognitor的图像似乎是一种自然的选择

下面框架上方的按钮是一个实验,以证明命令编码正确。奇怪的是,按钮的工作方式与命令的绑定完全相同,而TGR却没有做任何事情

我的控制模板XAML的一部分:

<Button Grid.Row="1" HorizontalOptions="Start" VerticalOptions="Start" IsVisible="{TemplateBinding BindingContext.ShowBack}" Text ="Back" Command="{TemplateBinding BindingContext.BackNavCommand}" Margin="90,0,0,0"/>

<!-- LATE EDIT - The TGR on this Frame is doomed not to work.  The whole frame should be defined BELOW the Content Presenter.  Read on below for why. -->
<Frame Grid.Row="1" HorizontalOptions="Start" VerticalOptions="Start" IsVisible="{TemplateBinding BindingContext.ShowBack}" BackgroundColor="Cyan" BorderColor="Transparent" HasShadow="False" HeightRequest="50" WidthRequest="50">
<Frame.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform ="iOS" Value="5,25,0,0"/>
<On Platform ="Android" Value="5,5,0,0"/>
<On Platform ="UWP" Value="5,5,0,0"/>
</OnPlatform>
</Frame.Padding>
<Frame.GestureRecognizers>
<TapGestureRecognizer x:Name="backTapped" Command="{TemplateBinding BindingContext.BackNavCommand}" />
</Frame.GestureRecognizers>
<Image x:Name="backImage"  Source="back.png" Aspect="AspectFit" HeightRequest="20" WidthRequest="30" VerticalOptions="Center" HorizontalOptions="Center" InputTransparent="True"/>
</Frame>
<ContentPresenter Grid.Row="1"/>

我设置了包含图像青色的框架的背景色,以确保我的点击区域显示出来。它显然在那里,但点击它永远不会激活TGR和我为它设置的相关命令


注意:填写ContentPresenter的“我的内容”有一个透明的背景,以显示主页的背景图像。这是这个场景的关键,因为很难分辨前面是什么。

啊哈!正如经常发生的那样,当你开始清晰地定义你的问题时,你会激发出解决问题的想法。

我以前遇到过这个问题,但已经忘记了:Z顺序

我知道我的ControlTemplate控件将共享Content Presenter的空间,因为它全部放在网格中。第1行。为了让这个TapGestureRecognitor正常工作,我需要在ContentPresenter下方的ControlTemplate XAML中定义它。通过这样做,控件仍然会在完全相同的位置结束,但它会在模板内容的顶部结束,因为它是稍后定义的(可以说是在视觉堆栈的更高位置)

就这样,TGR工作。这个按钮似乎不那么有礼貌。按钮工作正常,如下图所示,位于ContentPresenter上方

注意:我发现这是一种很好的方法,可以将大量重复使用的ActivityIndicator放入我的ControlTemplate中。同样,它必须位于ContentPreseneter的下方,否则微调器将显示在背景中,在输入字段之间,等等

提示:可能不与模板控件共享空间,但如果您这样做,并且希望模板控件可以在前面访问,请在它们下面定义模板中的ContentPresenter

关于模板绑定的说明。

许多地方都注意到,当绑定ControlTemplate控件时,您使用以下语法(例如,绑定在此处可见):

这里所指的BindingContext的确切含义取决于。。。。如果您的页面在其构造函数中将BindingContext设置为“this”,则模板将在模板正在用内容包装的视图中查找可绑定属性“ShowBack”。
但是,如果将模板使用内容包装的视图的BindingContext设置为ViewModel对象,则BindingContext将在那里查找名为ShowBack的可绑定属性。从实验中学到这一点,我想与大家分享

IsVisible="{TemplateBinding BindingContext.ShowBack}"