2016-07-26 13 views
0

Xamarinフォームで自分のリストにコンテキストアクションを実装しようとしていますが、動作させることができません。 私はXAMLを使用せず、代わりにコードでレイアウトを作成しています。 https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actionsの手順に従おうとしていて、「編集」をクリックしたときに新しいページをプッシュしたいと思います。Xamarinフォームのコンテキストアクションの実装

私のコードを整理して、私の弱い試みを取り除いて動作させました。

は、これが私のカスタムリストのセルである:

public class PickerListCell : TextCell 
{ 
    public PickerListCell() 
    { 
     var moreAction = new MenuItem { Text = App.Translate ("Edit") }; 
     moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding (".")); 
     moreAction.Clicked += async (sender, e) => { 
      var mi = ((MenuItem)sender); 
      var option = (PickerListPage.OptionListItem)mi.CommandParameter; 

      var recId = new Guid (option.Value); 

      // This is where I want to call a method declared in my page to be able to push a page to the Navigation stack 

     }; 
     ContextActions.Add (moreAction); 
    } 
} 

そして、ここで私のモデルである:

public class OptionListItem 
{ 
    public string Caption { get; set; } 

    public string Value { get; set; } 
} 

そして、これがページです:あなたはで見ることができるように

public class PickerPage : ContentPage 
{ 
    ListView listView { get; set; } 

    public PickerPage (OptionListItem [] items) 
    { 
     listView = new ListView() ; 

     Content = new StackLayout { 
      Children = { listView } 
     }; 

     var cell = new DataTemplate (typeof (PickerListCell)); 
     cell.SetBinding (PickerListCell.TextProperty, "Caption"); 
     cell.SetBinding (PickerListCell.CommandParameterProperty, "Value"); 


     listView.ItemTemplate = cell; 
     listView.ItemsSource = items; 
    } 

    // This is the method I want to activate when the context action is called 
    void OnEditAction (object sender, EventArgs e) 
    { 
     var cell = (sender as Xamarin.Forms.MenuItem).BindingContext as PickerListCell; 

     await Navigation.PushAsync (new RecordEditPage (recId), true); 
    } 

} 

コード内の私のコメント、私は物事がどこにないと信じているかを示しました。

人を助けてください! ありがとう!

答えて

0

いくつかの投稿、特にこの1つのhttps://forums.xamarin.com/discussion/27881/best-practive-mvvm-navigation-when-command-is-not-availableの助けを借りて、私はそれが見た目に完全に満足していないが、私は以下の解決策に出た。コマンドがMessagingCenterを使用して実行されているとき

マイカスタムセルは今発表:

public class PickerListCell : TextCell 
{ 

    public PickerListCell() 
    { 
     var moreAction = new MenuItem { Text = App.Translate ("Edit") }; 
     moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding (".")); 
     moreAction.Clicked += async (sender, e) => { 
      var mi = ((MenuItem)sender); 
      var option = (PickerListPage.OptionListItem)mi.CommandParameter; 

      var recId = new Guid (option.Value); 

      // HERE I send a request to open a new page. This looks a 
      // bit crappy with a magic string. It will be replaced with a constant or enum 
      MessagingCenter.Send<OptionListItem, Guid> (this, "PushPage", recId); 
     }; 
     ContextActions.Add (moreAction); 
    } 
} 

そして、私のPickerPageのコンストラクタで、私がメッセージングサービスにこのサブスクリプションを追加しました:

MessagingCenter.Subscribe<OptionListItem, Guid> (this, "PushPage", (sender, recId) => { 
      Navigation.PushAsync (new RecordEditPage (recId), true); 
     }); 

すべてこれはただの作品しかし、これが意図された方法であるかどうかはわかりません。私はバインディングがメッセージングサービスなしでこれを解決できるはずですが、ページ上のメソッドにバインドする方法、モデルのみにバインドする方法を見つけることができず、メソッドでモデルを汚染したくありませんそれらはXFに依存します。

1

おそらくあなたにとって遅すぎるかもしれませんが、他人を助けることができます。私がこれを行う方法を見つけたのは、ViewCellの作成時にページのインスタンスを渡すことです。

public class PickerListCell : TextCell 
{ 
    public PickerListCell (PickerPage myPage) 
    { 
     var moreAction = new MenuItem { Text = App.Translate ("Edit") }; 
     moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding (".")); 
     moreAction.Clicked += async (sender, e) => { 
      var mi = ((MenuItem)sender); 
      var option = (PickerListPage.OptionListItem)mi.CommandParameter; 

      var recId = new Guid (option.Value); 

      myPage.OnEditAction(); 

     }; 
     ContextActions.Add (moreAction); 
    } 
} 

だから、あなたのページで:

public class PickerPage : ContentPage 
{ 
    ListView listView { get; set; } 

    public PickerPage (OptionListItem [] items) 
    { 
     listView = new ListView() ; 

     Content = new StackLayout { 
      Children = { listView } 
     }; 

     var cell = new DataTemplate(() => {return new PickerListCell(this); });    
     cell.SetBinding (PickerListCell.TextProperty, "Caption"); 
     cell.SetBinding (PickerListCell.CommandParameterProperty, "Value"); 


     listView.ItemTemplate = cell; 
     listView.ItemsSource = items; 
    } 


    void OnEditAction (object sender, EventArgs e) 
    { 
     var cell = (sender as Xamarin.Forms.MenuItem).BindingContext as PickerListCell; 

     await Navigation.PushAsync (new RecordEditPage (recId), true); 
    } 

} 
関連する問題