uma*_*ali 12 c# events cross-platform xamarin.forms
我希望我的事件在按下和释放按钮时触发,但我只能在Xamarin.Forms中找到Click事件.
我相信必须有一些工作来获得这个功能.我的基本需求是在按下按钮时启动一个过程,在释放按钮时停止.它似乎是一个非常基本的功能,但Xamarin.Forms现在没有它.
我在按钮上尝试了TapGestureRecognizer,但按钮只触发了点击事件.
MyButton.Clicked += (sender, args) =>
{
Log.V(TAG, "CLICKED");
};
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
Log.V(TAG, "TAPPED");
};
MyButton.GestureRecognizers.Add(tapGestureRecognizer);
Run Code Online (Sandbox Code Playgroud)
请记住,我需要这些活动才能在Android和iOS中使用.
uma*_*ali 22
最后我得到了@Jason建议的解决方案.开始了...
在PCL项目中创建Xamarin.Forms.Button的子类,具有事件处理功能
public class CustomButton : Button
{
public event EventHandler Pressed;
public event EventHandler Released;
public virtual void OnPressed()
{
Pressed?.Invoke(this, EventArgs.Empty);
}
public virtual void OnReleased()
{
Released?.Invoke(this, EventArgs.Empty);
}
}
Run Code Online (Sandbox Code Playgroud)在相应项目中创建特定于平台的按钮渲染器
对于安道尔
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
namespace WalkieTalkie.Droid.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var customButton = e.NewElement as CustomButton;
var thisButton = Control as Android.Widget.Button;
thisButton.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Down)
{
customButton.OnPressed();
}
else if (args.Event.Action == MotionEventActions.Up)
{
customButton.OnReleased();
}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于IOS
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace WalkieTalkie.iOS.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var customButton = e.NewElement as CustomButton;
var thisButton = Control as UIButton;
thisButton.TouchDown += delegate
{
customButton.OnPressed();
};
thisButton.TouchUpInside += delegate
{
customButton.OnReleased();
};
}
}
}
Run Code Online (Sandbox Code Playgroud)在页面中实例化自定义按钮
var myButton = new CustomButton
{
Text = "CustomButton",
HorizontalOptions = LayoutOptions.FillAndExpand
};
myButton.Pressed += (sender, args) =>
{
System.Diagnostics.Debug.WriteLine("Pressed");
};
myButton.Released += (sender, args) =>
{
System.Diagnostics.Debug.WriteLine("Pressed");
};
Run Code Online (Sandbox Code Playgroud)希望这能帮助别人:)
这也可以使用效果而不是完整的自定义渲染器来完成.有关如何操作的说明,请参阅此文章:
https://alexdunn.org/2017/12/27/xamarin-tip-xamarin-forms-long-press-effect/
如果该帖子消失,这里是您可以实现的代码:
在共享项目中:
/// <summary>
/// Long pressed effect. Used for invoking commands on long press detection cross platform
/// </summary>
public class LongPressedEffect : RoutingEffect
{
public LongPressedEffect() : base("MyApp.LongPressedEffect")
{
}
public static readonly BindableProperty CommandProperty = BindableProperty.CreateAttached("Command", typeof(ICommand), typeof(LongPressedEffect), (object)null);
public static ICommand GetCommand(BindableObject view)
{
return (ICommand)view.GetValue(CommandProperty);
}
public static void SetCommand(BindableObject view, ICommand value)
{
view.SetValue(CommandProperty, value);
}
public static readonly BindableProperty CommandParameterProperty = BindableProperty.CreateAttached("CommandParameter", typeof(object), typeof(LongPressedEffect), (object)null);
public static object GetCommandParameter(BindableObject view)
{
return view.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(BindableObject view, object value)
{
view.SetValue(CommandParameterProperty, value);
}
}
Run Code Online (Sandbox Code Playgroud)
在Android中:
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(AndroidLongPressedEffect), "LongPressedEffect")]
namespace AndroidAppNamespace.Effects
{
/// <summary>
/// Android long pressed effect.
/// </summary>
public class AndroidLongPressedEffect : PlatformEffect
{
private bool _attached;
/// <summary>
/// Initializer to avoid linking out
/// </summary>
public static void Initialize() { }
/// <summary>
/// Initializes a new instance of the
/// <see cref="T:Yukon.Application.AndroidComponents.Effects.AndroidLongPressedEffect"/> class.
/// Empty constructor required for the odd Xamarin.Forms reflection constructor search
/// </summary>
public AndroidLongPressedEffect()
{
}
/// <summary>
/// Apply the handler
/// </summary>
protected override void OnAttached()
{
//because an effect can be detached immediately after attached (happens in listview), only attach the handler one time.
if (!_attached)
{
if (Control != null)
{
Control.LongClickable = true;
Control.LongClick += Control_LongClick;
}
else
{
Container.LongClickable = true;
Container.LongClick += Control_LongClick;
}
_attached = true;
}
}
/// <summary>
/// Invoke the command if there is one
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
private void Control_LongClick(object sender, Android.Views.View.LongClickEventArgs e)
{
Console.WriteLine("Invoking long click command");
var command = LongPressedEffect.GetCommand(Element);
command?.Execute(LongPressedEffect.GetCommandParameter(Element));
}
/// <summary>
/// Clean the event handler on detach
/// </summary>
protected override void OnDetached()
{
if (_attached)
{
if (Control != null)
{
Control.LongClickable = true;
Control.LongClick -= Control_LongClick;
}
else
{
Container.LongClickable = true;
Container.LongClick -= Control_LongClick;
}
_attached = false;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在iOS中:
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(iOSLongPressedEffect), "LongPressedEffect")]
namespace iOSNamespace.Effects
{
/// <summary>
/// iOS long pressed effect
/// </summary>
public class iOSLongPressedEffect : PlatformEffect
{
private bool _attached;
private readonly UILongPressGestureRecognizer _longPressRecognizer;
/// <summary>
/// Initializes a new instance of the
/// <see cref="T:Yukon.Application.iOSComponents.Effects.iOSLongPressedEffect"/> class.
/// </summary>
public iOSLongPressedEffect()
{
_longPressRecognizer = new UILongPressGestureRecognizer(HandleLongClick);
}
/// <summary>
/// Apply the handler
/// </summary>
protected override void OnAttached()
{
//because an effect can be detached immediately after attached (happens in listview), only attach the handler one time
if (!_attached)
{
Container.AddGestureRecognizer(_longPressRecognizer);
_attached = true;
}
}
/// <summary>
/// Invoke the command if there is one
/// </summary>
private void HandleLongClick()
{
var command = LongPressedEffect.GetCommand(Element);
command?.Execute(LongPressedEffect.GetCommandParameter(Element));
}
/// <summary>
/// Clean the event handler on detach
/// </summary>
protected override void OnDetached()
{
if (_attached)
{
Container.RemoveGestureRecognizer(_longPressRecognizer);
_attached = false;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在XAML中
<Label Text="Long Press Me!" effects:LongPressedEffect.Command="{Binding ShowAlertCommand}" effects:LongPressedEffect.CommandParameter="{Binding .}">
<Label.Effects>
<effects:LongPressedEffect />
</Label.Effects>
</Label>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14824 次 |
| 最近记录: |