如何在longpress用户上显示自定义弹出页面?每个选项都必须有一个新闻事件。
我没有看到与此相关的话题,但是它们没有帮助。
这是我的代码:
for (int l = 1; l < ustkatmasasayisi; l++)
{
var buttonustkat = new Button
{
Text = l.ToString(),
HeightRequest = 45,
WidthRequest = 45,
Margin = 5,
CornerRadius = 100,
};
buttonustkat.Clicked += ustkatbuton;
ustkat.Children.Add(buttonustkat);
}
async void ustkatbuton(object o, EventArgs args)
{
secilenmasa = buttonustkat.Text;
secilenkonum = "Üst kat";
await Navigation.PushModalAsync(new menu());
}
我必须在buttonustkat
上设置longpress事件
答案 0 :(得分:1)
您可以使用自定义渲染器
创建自定义按钮
using System;
using Xamarin.Forms;
namespace App13
{
public class MyButton:Button
{
public EventHandler LongPress { get; set; }
}
}
using System;
using UIKit;
using App13;
using App13.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace App13.iOS
{
public class MyButtonRenderer:ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
var longPress = new UILongPressGestureRecognizer(()=> {
var myButton = Element as MyButton;
myButton.LongPress?.Invoke(myButton, new EventArgs());
});
Control.AddGestureRecognizer(longPress);
}
}
}
}
using System;
using Android.Content;
using App13;
using App13.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly:ExportRenderer(typeof(MyButton),typeof(MyButtonRenderer))]
namespace App13.Droid
{
public class MyButtonRenderer : ButtonRenderer
{
public MyButtonRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
Control.LongClick += Control_LongClick;
}
}
private void Control_LongClick(object sender, LongClickEventArgs e)
{
var mybutton = Element as MyButton;
mybutton.LongPress?.Invoke(mybutton,new EventArgs());
}
}
}
那么您就可以处理长按事件
var buttonustkat = new MyButton
{
Text = l.ToString(),
HeightRequest = 45,
WidthRequest = 45,
Margin = 5,
CornerRadius = 100,
};
buttonustkat..LongPress += Button_LongPress;
private void Button_LongPress(object sender, EventArgs e)
{
}