2017-08-14 36 views
0

我目前正在为我的应用程序在一个新的ContentPage中工作,并且我有一些DataTemplates控件。Xamarin.Forms-x:引用第二级

我想在我的ContentPage的ViewModel中的一个DataTemplates中使用命令,但是我不确定如何执行正确的引用才能正常工作。这是我的XAML代码:

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="MaverickMobileOnline.ImagesListingPage" 

    --MANY NAMESPACE REFERENCES-- 
    > 


    <ContentPage.Resources> 
     <ResourceDictionary> 
      <c:ItemTappedEventArgsConverter x:Key="ItemTappedConverter" /> 
      <c:ItemAppearingEventArgsConverter x:Key="ItemAppearingConverter" /> 
      <c:BooleanNegationConverter x:Key="not" /> 
     </ResourceDictionary> 
    </ContentPage.Resources> 

    <StackLayout Spacing="0"> 
     <commonViews:MainCustomNavBar MinimumHeightRequest="120" /> 


     <controls:PullToRefreshLayout 
      x:Name = "layout" 
      RefreshCommand="{Binding btn_reload_businesses_images_click}"  
      IsEnabled = "True"   
      IsRefreshing="{Binding Path=is_businesses_loading}" > 

      <ScrollView 
      x:Name = "scrollView" 
      HorizontalOptions="FillAndExpand" 
      VerticalOptions="FillAndExpand"> 

       <templates:ItemsStack 
        Padding="0" 
        Margin="0,10,0,10" 
        x:Name="itmStack" 
        BackgroundColor="White" 
        ItemsSource="{Binding Path=photos_list}"> 

       <templates:ItemsStack.ItemTemplate> 
        <DataTemplate> 

         <artina:GridOptionsView 

           Padding="10,0" 
           ColumnSpacing="10" 
           RowSpacing="10" 
           VerticalOptions="Fill" 
           HeightRequest="120" 
           ColumnCount="3" 
           RowCount="1" 
           ItemsSource="{Binding Path=.}"> 

           <artina:GridOptionsView.ItemTemplate> 
            <DataTemplate> 

             <ContentView 
              xmlns="http://xamarin.com/schemas/2014/forms" 
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
              x:Class="MaverickMobileOnline.GalleryImageItemTemplate" 
              xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" 
              xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"> 

              <ContentView.Content> 
               <ffimageloading:CachedImage 
                FadeAnimationEnabled="true" 
                Aspect="AspectFill" 
                VerticalOptions="FillAndExpand" 
                HorizontalOptions="FillAndExpand" 

                LoadingPlaceholder="advertising_photo_placeholder.png" 
                Source="{Binding Path=image_medium}" /> 
              </ContentView.Content> 

              <ContentView.GestureRecognizers> 
               <TapGestureRecognizer 
                -- HERE! -- 

                Command="{Binding Source={x:Reference X}, Path=BindingContext.command_name}" 
                CommandParameter="{Binding image_medium}" 
               /> 
              </ContentView.GestureRecognizers> 

             </ContentView> 

            </DataTemplate> 
           </artina:GridOptionsView.ItemTemplate> 
          </artina:GridOptionsView> 

        </DataTemplate> 
       </templates:ItemsStack.ItemTemplate> 
      </templates:ItemsStack> 

      </ScrollView> 
     </controls:PullToRefreshLayout> 

    </StackLayout> 

</ContentPage> 

请看看标记“ - HERE! - ”后面的代码。

PS:我只是为了提高性能而改进此布局。

任何帮助将不胜感激。

UPDATE:

视图模型:

public RelayCommand<string> btn_image_tap_preview 
{ 
    get 
    { 
     return new RelayCommand<string>(
      OpenImagePreview 
     ); 
    } 
} 

//* Image tap 
private async void OpenImagePreview(string url) 
{ 
    //* Some code 
} 

更新XAML:

<StackLayout x:Name="mainStack" Spacing="0"> 
     <commonViews:MainCustomNavBar MinimumHeightRequest="120" /> 
.... 
<ContentView.GestureRecognizers> 
    <TapGestureRecognizer Command="{Binding Source={x:Reference mainStack}, Path=BindingContext.btn_image_tap_preview}" 
         CommandParameter="{Binding image_medium}" /> 
</ContentView.GestureRecognizers> 

我调试,但我不能达到视图模型的命令。

回答

0

您可以使用x:Reference并从您的内容页面(如StackLayout)获取任何布局或元素的绑定上下文。由于其绑定上下文是页面的ViewModel,因此它会在第一级本身找到您要查找的命令。你可以直接使用它。

<StackLayout x:Name="myStackLayout" Spacing="0"> 
     <commonViews:MainCustomNavBar MinimumHeightRequest="120" /> 
.... 
<ContentView.GestureRecognizers> 
    <TapGestureRecognizer Command="{Binding Source={x:Reference myStackLayout}, Path=BindingContext.command_name}" 
         CommandParameter="image_medium" /> 
</ContentView.GestureRecognizers> 

或者你可以设置绑定上下文TapGesture

<TapGestureRecognizer BindingContext="{Binding Source={x:Reference myStackLayout.BindingContext}}" Command="{Binding command_name}" 

DataTemplates似乎并不与x:Reference之前V1.4.4,您可以在论坛上发帖阅读更多关于很好地工作 - x:Reference not working?。 在新版本中的实现可以在答案here中看到。 如果您有兴趣修复的Bugzilla问题是here

+0

嗨,感谢您的回复。我正在尝试使用您的解决方案,但我无法完成工作。我更新了我的问题。提前致谢! –

+0

你有你的命令参数绑定。它不需要约束力。 –

+0

我删除了绑定,但它仍然没有工作,任何其他的想法?提前致谢。 –