Xamarin.forms 是否有方法识别在listview中打开/关闭的上下文菜单?
我正在xamarin表单项目的listview中使用上下文操作(ViewCell.ContextActions)。由于我有一个“添加新”按钮浮动在列表顶部,我想识别上下文菜单何时打开或关闭(这样我可以在上下文菜单打开时从屏幕上删除该按钮)。 我的问题:xamarin表单中是否有任何方法可以识别上下文菜单何时打开?Xamarin.forms 是否有方法识别在listview中打开/关闭的上下文菜单?,xamarin.forms,Xamarin.forms,我正在xamarin表单项目的listview中使用上下文操作(ViewCell.ContextActions)。由于我有一个“添加新”按钮浮动在列表顶部,我想识别上下文菜单何时打开或关闭(这样我可以在上下文菜单打开时从屏幕上删除该按钮)。 我的问题:xamarin表单中是否有任何方法可以识别上下文菜单何时打开? 谢谢你的帮助 您可以在自定义渲染器中检测上下文菜单的打开/关闭状态 iOS示例 [程序集:ExportRenderer(typeof(Xamarin.Forms.ListView)、t
谢谢你的帮助 您可以在自定义渲染器中检测上下文菜单的打开/关闭状态 iOS示例
[程序集:ExportRenderer(typeof(Xamarin.Forms.ListView)、typeof(MyRenderer))]
命名空间FormsApp.iOS
{
类MyRenderer:ListViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null)
{
//退订
}
if(例如NewElement!=null)
{
Control.Source=新的NativeOsListViewSource(例如,作为ListView的新元素);
}
}
}
公共类NativeIOListViewSource:UITableViewSource
{
IList表格项目;
列表视图列表视图;
public NativeIOListViewSource(ListView视图)
{
listView=视图;
tableItems=view.ItemsSource作为IList;
}
public override void committeeditingstyle(UITableView tableView、UITableViewCellEditingStyle editingStyle、NSIndexPath indexPath)
{
committedItingStyle(tableView、editingStyle、indexPath);
}
公共覆盖void WillBeginEditing(UITableView tableView,nsindepath indepath)
{
(App.Current作为App.MainPage.DisplayAlert(“,”打开“,”确定”);
}
公共重写void DidEndEditing(UITableView tableView,nsindepath indepath)
{
(App.Current作为App.MainPage.DisplayAlert(“,”关闭“,”确定”);
}
公共覆盖UITableViewCell GetCell(UITableView tableView,NSIndexPath indexPath)
{
UITableViewCell=tableView.DequeueReusableCell(“UITableViewCell”)作为UITableViewCell;
if(单元格==null)
{
单元格=新的UITableViewCell(UITableViewCellStyle.Default,“UITableViewCell”);
}
cell.textlab.Text=tableItems[indexPath.Row]作为字符串;
返回单元;
}
公共覆盖第九行第九节(UITableView表格视图,第九节)
{
返回表项。计数;
}
}
}
您可以在表单中创建自定义listview,并设置全局变量(isOpen)来存储状态。在中打开的上下文菜单将开始编辑
方法,并在编辑
方法中关闭
您可以在自定义渲染器中检测上下文菜单的打开/关闭状态 iOS示例
[程序集:ExportRenderer(typeof(Xamarin.Forms.ListView)、typeof(MyRenderer))]
命名空间FormsApp.iOS
{
类MyRenderer:ListViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null)
{
//退订
}
if(例如NewElement!=null)
{
Control.Source=新的NativeOsListViewSource(例如,作为ListView的新元素);
}
}
}
公共类NativeIOListViewSource:UITableViewSource
{
IList表格项目;
列表视图列表视图;
public NativeIOListViewSource(ListView视图)
{
listView=视图;
tableItems=view.ItemsSource作为IList;
}
public override void committeeditingstyle(UITableView tableView、UITableViewCellEditingStyle editingStyle、NSIndexPath indexPath)
{
committedItingStyle(tableView、editingStyle、indexPath);
}
公共覆盖void WillBeginEditing(UITableView tableView,nsindepath indepath)
{
(App.Current作为App.MainPage.DisplayAlert(“,”打开“,”确定”);
}
公共重写void DidEndEditing(UITableView tableView,nsindepath indepath)
{
(App.Current作为App.MainPage.DisplayAlert(“,”关闭“,”确定”);
}
公共覆盖UITableViewCell GetCell(UITableView tableView,NSIndexPath indexPath)
{
UITableViewCell=tableView.DequeueReusableCell(“UITableViewCell”)作为UITableViewCell;
if(单元格==null)
{
单元格=新的UITableViewCell(UITableViewCellStyle.Default,“UITableViewCell”);
}
cell.textlab.Text=tableItems[indexPath.Row]作为字符串;
返回单元;
}
公共覆盖第九行第九节(UITableView表格视图,第九节)
{
返回表项。计数;
}
}
}
您可以在表单中创建自定义listview,并设置全局变量(isOpen)来存储状态。在中打开的上下文菜单将开始编辑
方法,并在编辑
方法中关闭
[assembly: ExportRenderer(typeof(Xamarin.Forms.ListView), typeof(MyRenderer))]
namespace FormsApp.iOS
{
class MyRenderer : ListViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe
}
if (e.NewElement != null)
{
Control.Source = new NativeiOSListViewSource(e.NewElement as ListView);
}
}
}
public class NativeiOSListViewSource : UITableViewSource
{
IList<string> tableItems;
ListView listView;
public NativeiOSListViewSource(ListView view)
{
listView = view;
tableItems = view.ItemsSource as IList<string>;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
base.CommitEditingStyle(tableView, editingStyle, indexPath);
}
public override void WillBeginEditing(UITableView tableView, NSIndexPath indexPath)
{
(App.Current as App).MainPage.DisplayAlert("", "Open", "OK");
}
public override void DidEndEditing(UITableView tableView, NSIndexPath indexPath)
{
(App.Current as App).MainPage.DisplayAlert("", "Close", "OK");
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell("UITableViewCell") as UITableViewCell;
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, "UITableViewCell");
}
cell.TextLabel.Text = tableItems[indexPath.Row] as string;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return tableItems.Count;
}
}
}