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
Xamarin表单:从DataTemplate调用ViewModel命令_Xamarin_Xamarin.forms_Binding_Datatemplate - Fatal编程技术网

Xamarin表单:从DataTemplate调用ViewModel命令

Xamarin表单:从DataTemplate调用ViewModel命令,xamarin,xamarin.forms,binding,datatemplate,Xamarin,Xamarin.forms,Binding,Datatemplate,我在这里遇到了一个绑定问题 我在控件模板中创建了一个可绑定布局: <ContentView x:Name="SettingsMenu" ControlTemplate="{StaticResource HeaderTemplate}" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" AbsoluteLayout.LayoutFlags="All"> <ScrollView Orientation="Ve

我在这里遇到了一个绑定问题

我在控件模板中创建了一个可绑定布局:

<ContentView x:Name="SettingsMenu" ControlTemplate="{StaticResource HeaderTemplate}" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" 
AbsoluteLayout.LayoutFlags="All">               
    <ScrollView Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">                   
        <StackLayout x:Name="SettingsStack" BindableLayout.ItemsSource="{Binding Settings}" BindableLayout.ItemTemplateSelector="{StaticResource SettingsSelectorTemplate}" Orientation="Vertical" Spacing="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />           
    </ScrollView>        
</ContentView>

我要做的是在视图模型中调用一个命令。调用位于项目模板选择器中,作为App.xml中的资源字典

<ResourceDictionary>
    <DataTemplate x:Key="PlaceholderSettingsTemplate">
        ### SOME STUFF
     </DataTemplate>
     <DataTemplate x:Key="HeaderSettingsTemplate">
             ### SOME STUFF
         <Grid ...>
             <Grid.GestureRecognizers>
                 <TapGestureRecognizer Tapped="ButtonClick" Command="{Binding BindingContext.SettingsTap, Source={x:Reference SettingsPage}}" CommandParameter="{Binding}" />  ########## <--------- WHAT TO USE FOR SOURCE?
             </Grid.GestureRecognizers>
         </Grid>
     </DataTemplate>
     <data:SettingsSelector x:Key="SettingsSelectorTemplate" Placeholder="{StaticResource PlaceholderSettingsTemplate}" Heading="{StaticResource HeaderSettingsTemplate}" Content="{StaticResource ContentSettingsTemplate}" />
</ResourceDictionary>

###一些东西
###一些东西

##########您可以使用数据模板的网格(包装所有内容)找到
设置堆栈布局。由于
SettingsStack
与父内容视图具有相同的绑定上下文,因此您可以访问App.cs中的绑定上下文,如:

<DataTemplate x:Key="HeaderSettingsTemplate">
    <!--### SOME STUFF-->
    <Grid x:Name="ParentGrid">
        <Grid.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding Parent.BindingContext.SettingsTap, Source={x:Reference ParentGrid}}" CommandParameter="{Binding}" />
        </Grid.GestureRecognizers>
    </Grid>
</DataTemplate>


ParentGrid
的父项是当前页面上的
SettingsStack

您可以使用数据模板的网格找到
SettingsStack
堆栈布局,该网格包含所有内容。由于
SettingsStack
与父内容视图具有相同的绑定上下文,因此您可以访问App.cs中的绑定上下文,如:

<DataTemplate x:Key="HeaderSettingsTemplate">
    <!--### SOME STUFF-->
    <Grid x:Name="ParentGrid">
        <Grid.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding Parent.BindingContext.SettingsTap, Source={x:Reference ParentGrid}}" CommandParameter="{Binding}" />
        </Grid.GestureRecognizers>
    </Grid>
</DataTemplate>

ParentGrid
的父对象是当前页面上的
SettingsStack

从项目模板(即使它位于不同的文件中),您可以使用以下命令访问其祖先的视图模型:

Command="{Binding Source={RelativeSource AncestorType={x:Type SettingsViewModel}}, Path=SettingsTap}" CommandParameter="{Binding}"
在本例中,我只是假设祖先的视图模型的名称是setingsviewmodel,但您明白了这一点

下面是一篇关于相对绑定的有趣文章,它还描述了其他场景:

干杯。

从项目模板(即使它位于不同的文件中),您可以使用以下方法访问其祖先的视图模型:

Command="{Binding Source={RelativeSource AncestorType={x:Type SettingsViewModel}}, Path=SettingsTap}" CommandParameter="{Binding}"
在本例中,我只是假设祖先的视图模型的名称是setingsviewmodel,但您明白了这一点

下面是一篇关于相对绑定的有趣文章,它还描述了其他场景:

干杯