2016-04-13 21 views
3

私はリストビューのItemTappedをバインドして、プリズムを使って詳細ページをナビゲートする方法を見つけようとしています。 私はDelegateCommandと試みるが、私はエラーがあります:私は<Button>ナビゲーション作品にこのDelegateCommandをバインドするとViewModelでListViewのItemTappedプロパティをバインドして詳細ページをナビゲートする方法はありますか?

public class UsersViewModel : BindableBase 
{ 

    ..... some bindable objects 

    INavigationService _navigationService; 
    public DelegateCommand ShowUserDetail { get; set; } 

    public UsersViewModel (INavigationService navigationService) 
    { 
     _navigationService = navigationService; 
     ShowUserDetail = new DelegateCommand(OnShowUserDetail); 
    } 

    public void OnShowUserDetail() 
    { 
     var par = new NavigationParameters(); 
     par.Add("user", SelectedUser); 
     _navigationService.Navigate("UserDetail", par); 
    } 

    .... 

Exception is: XamlParseException - Position 15:7. No Property of name ItemTapped found

ビュー:

<ListView 
    ItemsSource="{Binding UsersList}" 
    SelectedItem="{Binding SelectedUser}" 
    ItemTapped="{Binding ShowUserDetail}" 
    RowHeight="65" > 
    <ListView.ItemTemplate> 
    <DataTemplate> 
     <ViewCell> 
     <StackLayout Padding="20" HorizontalOptions="FillAndExpand"> 
      <Label Text="{Binding Name}" TextColor="Blue" FontSize="15"/> 
      <Label Text="{Binding Email}" TextColor="Gray" FontSize="11"/> 
     </StackLayout> 
     </ViewCell> 
    </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

のViewModelを。 おそらく、これはプリズムに関連していませんが、私はこれを使用するための例を見つけることができません。おかげさまで

答えて

3

ボタンには、バインドするCommandプロパティがありますが、ListViewのItemTappedはイベントハンドラが必要なイベントです。あなたはバインディングを使用したい場合は、動作を使用する必要があります:

<ListView 
    ItemsSource="{Binding UsersList}" 
    SelectedItem="{Binding SelectedUser}" 
    RowHeight="65" > 
    <ListView.Behaviors> 
    <b:EventToCommand EventName="ItemTapped" Command="{Binding ShowUserDetail}" /> 
    </ListView.Behaviors> 
    <ListView.ItemTemplate> 
    <DataTemplate> 
     <ViewCell> 
     <StackLayout Padding="20" HorizontalOptions="FillAndExpand"> 
      <Label Text="{Binding Name}" TextColor="Blue" FontSize="15"/> 
      <Label Text="{Binding Email}" TextColor="Gray" FontSize="11"/> 
     </StackLayout> 
     </ViewCell> 
    </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

出典:https://forums.xamarin.com/discussion/comment/180600/#Comment_180600

EDIT: EventToCommandthis NuGet packageでの動作です。

+0

速い応答ありがとう – vsasa

+0

もちろん、私は今日これを試した後 – vsasa

+0

ここにドキュメントがあります:https://developer.xamarin.com/guides/xamarin-forms/behaviors/reusable/event-to-command-振る舞い/ しかし、ナゲットパッケージhttps://www.nuget.org/packages/Xamarin.Forms.Behaviors/もあります。 EventToCommandBehaviorという名前のカスタムメソッドを記述する必要がありますか? すみません、私は比較的新しいxamarinです。 – vsasa

1

私はBartsスニペットも何とか動作すると信じていますが、どうやって実装すればいいのか分かりません。私はより良い説明といくつかの作業コードの例をお勧めします。

解決策が見つかりました。私はこのラインを設定し、ヘッダに

<ListView 
    ItemsSource="{Binding UsersList}" 
    SelectedItem="{Binding SelectedUser}"> 

    <b:Interaction.Behaviors> 
     <b:BehaviorCollection> 
      <b:EventToCommand EventName="ItemTapped" Command="{Binding ShowUserDetail}"/> 
     </b:BehaviorCollection> 
    </b:Interaction.Behaviors> 
    ... 

(マッピングされたB:

(ポータブルで、すべてのサブプロジェクトのiOS、アンドロイド...)「型の動作xamarin」nugetパッケージを追加した後、私はリストビューでは、この追加しました名前空間に):

xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors" 

もう一つは、IOSプラットフォームのために、これは

)ため、エラー例外の、それはここで xamarin-behaviors-with-xamarin-forms-ios説明しています(AppDelegate.cs/FinishedLaunching方法で配置する必要があります
Xamarin.Behaviors.Infrastructure.Init(); 

"ItemTapped"が正しく機能します。

関連する問題