2017-08-14 16 views
0

私は現在、自分のアプリケーション用の新しいContentPageで作業しており、DataTemplatesでいくつかのコントロールを持っています。Xamarin.Forms -x:参照第2レベル

DataTemplatesのContentPageのViewModelでCommandを使用したいのですが、正しく動作するための正しい参照を行う方法がわかりません。ここに私の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:

のViewModel:

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> 

私がデバッグだが、私はViewModelにのコマンドに到達することはできません。

答えて

0

x:Referenceを使用して、StackLayoutのようなコンテンツページのレイアウトや要素のバインディングコンテキストを取得できます。バインド・コンテキストはページのViewModelであるため、最初のレベル自体で探しているコマンドがあります。あなたはそれを直接使うことができます。 x:Reference not working? -

<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> 

は、またはあなたがx:Reference前V1.4.4に、あなたはフォーラムの投稿で詳細を読むことができるとうまく動作するようには思えない

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

DataTemplates TapGesture

のバインディングコンテキストを設定することができます。 新しいバージョンでの実装は、回答 hereで確認できます。 興味があれば、修正されたBugzillaの問題は hereです。

+0

こんにちは、返信ありがとうございます。私はあなたのソリューションを試していますが、私はそれを動作させることはできません。私は私の質問を更新しました。前もって感謝します! –

+0

コマンド・パラメーターにバインディングがあります。拘束力はありません。 –

+0

Bindingを削除しましたが、まだ動作していません。前もって感謝します。 –

関連する問題