我想在Xamarin Forms的列表中實現上下文動作,但無法讓它工作。 我不使用XAML,而是在代碼中創建我的佈局。 我試圖按照https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actions中的步驟操作,並且當點擊「編輯」時我想推新頁面。實現Xamarin Forms上下文動作
我清理了我的代碼,並刪除了我無法企及的工作。
所以這是我的自定義列表格:
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);
}
}
正如你可以看到我在代碼中的評論,我已經指出我認爲事情缺失的地方。
請幫助傢伙! 謝謝!