- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当用户点击应用程序中的标签时,我正在尝试检测 Xamarin 中的按下和释放事件,以实现长按手势。
我已经从另一个 StackOverflow 问题创建了一个 LongPressBehavior 类,但标签没有附加按下和释放事件。
我可以使用按钮,但我仍然需要格式化文本 (FormattedString),其中包含用户可以单击以导航到网站的 URL 链接。
C#
public class LongPressBehavior : BehaviorBase<ExtendedLabel>
{
private readonly object _syncObject = new object();
private const int Duration = 1000;
//timer to track long press
private Timer _timer;
//the timeout value for long press
private readonly int _duration;
//whether the button was released after press
private volatile bool _isReleased;
/// <summary>
/// Occurs when the associated button is long pressed.
/// </summary>
public event EventHandler LongPressed;
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command),
typeof(ICommand), typeof(LongPressBehavior), default(ICommand));
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(LongPressBehavior));
/// <summary>
/// Gets or sets the command parameter.
/// </summary>
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
/// <summary>
/// Gets or sets the command.
/// </summary>
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
protected override void OnAttachedTo(ExtendedLabel button)
{
base.OnAttachedTo(button);
this.BindingContext = button.BindingContext;
button.Clicked += Button_Pressed;
//button.Released += Button_Released;
}
protected override void OnDetachingFrom(ExtendedLabel button)
{
base.OnDetachingFrom(button);
this.BindingContext = null;
button.Clicked -= Button_Pressed;
//button.Released -= Button_Released;
}
/// <summary>
/// DeInitializes and disposes the timer.
/// </summary>
private void DeInitializeTimer()
{
lock (_syncObject)
{
if (_timer == null)
{
return;
}
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_timer.Dispose();
_timer = null;
Debug.WriteLine("Timer disposed...");
}
}
/// <summary>
/// Initializes the timer.
/// </summary>
private void InitializeTimer()
{
lock (_syncObject)
{
_timer = new Timer(Timer_Elapsed, null, _duration, Timeout.Infinite);
}
}
private void Button_Pressed(object sender, EventArgs e)
{
Debug.WriteLine("Pressed");
_isReleased = false;
InitializeTimer();
}
private void Button_Released(object sender, EventArgs e)
{
Debug.WriteLine("Released");
_isReleased = true;
DeInitializeTimer();
}
protected virtual void OnLongPressed()
{
var handler = LongPressed;
handler?.Invoke(this, EventArgs.Empty);
if (Command != null && Command.CanExecute(CommandParameter))
{
Command.Execute(CommandParameter);
}
}
public LongPressBehavior()
{
_isReleased = true;
_duration = Duration;
}
public LongPressBehavior(int duration) : this()
{
_duration = duration;
}
private void Timer_Elapsed(object state)
{
DeInitializeTimer();
if (_isReleased)
{
return;
}
Device.BeginInvokeOnMainThread(OnLongPressed);
}
}
public class ExtendedLabel : Label
{
private event EventHandler Pressed;
private event EventHandler Released;
public string Name
{
get; set;
}
public void DoClick()
{
Pressed.Invoke(this, null);
}
public event EventHandler Clicked
{
add
{
lock (this)
{
Pressed += value;
TapGestureRecognizer g = new TapGestureRecognizer();
g.Tapped += (s, e) => Pressed?.Invoke(s, e);
GestureRecognizers.Add(g);
}
}
remove
{
lock (this)
{
Pressed -= value;
GestureRecognizers.Clear();
}
}
}
public event EventHandler UnClicked
{
add
{
lock (this)
{
Released += value;
TapGestureRecognizer g = new TapGestureRecognizer();
g.Tapped += (s, e) => Released?.Invoke(s, e);
GestureRecognizers.Add(g);
}
}
remove
{
lock (this)
{
Released -= value;
GestureRecognizers.Clear();
}
}
}
}
XAML
<controls:ExtendedLabel HorizontalOptions="Fill" HorizontalTextAlignment="End" TextColor="White" FormattedText="{Binding OutFormattedBody}">
<controls:ExtendedLabel.Behaviors>
<behaviors:LongPressBehavior LongPressed="OnClick"/>
</controls:ExtendedLabel.Behaviors>
</controls:ExtendedLabel>
我希望能够在标签上点击并按住大约一秒钟以触发 LongPress 事件,但仍然能够单击可能位于 FormattedString 中的链接。
最佳答案
您可以尝试在各个平台上实现效果。首先,在 Forms 项目中定义效果类:
public class PressedEffect : RoutingEffect
{
public PressedEffect() : base("MyApp.PressedEffect")
{
}
public static readonly BindableProperty LongPressedCommandProperty = BindableProperty.CreateAttached("LongPressedCommand", typeof(ICommand), typeof(PressedEffect), (object)null);
public static ICommand GetLongPressedCommand(BindableObject view)
{
return (ICommand)view.GetValue(LongPressedCommandProperty);
}
public static void SetLongPressedCommand(BindableObject view, ICommand value)
{
view.SetValue(LongPressedCommandProperty, value);
}
public static readonly BindableProperty LongParameterProperty = BindableProperty.CreateAttached("LongParameter", typeof(object), typeof(PressedEffect), (object)null);
public static object GetLongParameter(BindableObject view)
{
return view.GetValue(LongParameterProperty);
}
public static void SetLongParameter(BindableObject view, object value)
{
view.SetValue(LongParameterProperty, value);
}
public static readonly BindableProperty LongRelesedCommandProperty = BindableProperty.CreateAttached("LongRelesedCommand", typeof(ICommand), typeof(PressedEffect), (object)null);
public static ICommand GetLongRelesedCommand(BindableObject view)
{
return (ICommand)view.GetValue(LongRelesedCommandProperty);
}
public static void SetLongRelesedCommand(BindableObject view, ICommand value)
{
view.SetValue(LongRelesedCommandProperty, value);
}
}
Android 实现:
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(AndroidPressedEffect), "PressedEffect")]
namespace LongPressedDemo.Droid
{
public class AndroidPressedEffect : PlatformEffect
{
private bool _attached;
public AndroidPressedEffect()
{
}
protected override void OnAttached()
{
if (!_attached)
{
if (Control != null)
{
Control.LongClickable = true;
Control.Touch += Control_Touch;
}
else
{
Container.LongClickable = true;
Container.Touch += Control_Touch;
}
_attached = true;
}
}
private void Control_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
if (e.Event.Action == MotionEventActions.Down)
{
var command = PressedEffect.GetLongPressedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
else if (e.Event.Action == MotionEventActions.Up)
{
var command = PressedEffect.GetLongRelesedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
}
protected override void OnDetached()
{
if (_attached)
{
if (Control != null)
{
Control.LongClickable = true;
Control.Touch -= Control_Touch;
}
else
{
Container.LongClickable = true;
Container.Touch -= Control_Touch;
}
_attached = false;
}
}
}
}
iOS 实现:
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(iOSPressedEffect), "PressedEffect")]
namespace LongPressedDemo.iOS
{
public class iOSPressedEffect : PlatformEffect
{
private bool _attached;
private readonly UILongPressGestureRecognizer _longPressRecognizer;
public iOSPressedEffect()
{
_longPressRecognizer = new UILongPressGestureRecognizer(HandleLongClick);
}
protected override void OnAttached()
{
if (!_attached)
{
if (Control != null)
{
Control.AddGestureRecognizer(_longPressRecognizer);
Control.UserInteractionEnabled = true;
}
else
{
Container.AddGestureRecognizer(_longPressRecognizer);
}
_attached = true;
}
}
private void HandleLongClick(UILongPressGestureRecognizer recognizer)
{
if (recognizer.State == UIGestureRecognizerState.Began)
{
var command = PressedEffect.GetLongPressedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
else if (recognizer.State == UIGestureRecognizerState.Ended)
{
var command = PressedEffect.GetLongRelesedCommand(Element);
command?.Execute(PressedEffect.GetLongParameter(Element));
}
}
protected override void OnDetached()
{
if (_attached)
{
if (Control != null)
{
Control.RemoveGestureRecognizer(_longPressRecognizer);
}
else
{
Container.RemoveGestureRecognizer(_longPressRecognizer);
}
_attached = false;
}
}
}
}
最后,消耗了这个效果:
<Label Text="Welcome to Xamarin.Forms!"
local:PressedEffect.LongPressedCommand="{Binding PressedCommand}"
local:PressedEffect.LongRelesedCommand="{Binding ReleasedCommand}">
<Label.Effects>
<local:PressedEffect/>
</Label.Effects>
</Label>
关于c# - Xamarin 中是否有针对标签上的 GestureRecognizer 的新闻和发布 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56797488/
我最近开始接触网络编程,我完全不知所措。我已经开始学习基础知识、html、css 和 javascript。在我的第一个网页上,我有兴趣为我发布更新的地方实现一个下拉新闻部分。我能举出的最好的例子就是
我的 Wordpress 主题在我放大评论框时没有扩展其内容区域,因此整个内容都在页脚上。页脚保持固定在页面底部,但当我展开评论框时不会自行向下推... 我尝试阅读其他问题,但我没有解决那个问题。 代
我正在运行 TYPO3 V6 和最新版本的 tx_news(不是 tt_news),当我尝试更改 LIST 显示的排序顺序时,插件中的设置不会覆盖 Typoscript 设置。似乎没有办法更改 Lis
根据 Google 允许您的文章/新闻出现在 Google 新闻中: Display a three-digit number. The URL for each article must conta
我想问一下是否可以使用表单提交后发送到用户邮件的唯一代码创建注册,我不确定如何正确地做。 for example : The user enters his email and the system
我目前正在使用在 Google 新闻语料库上训练的 Word2Vec 模型(来自 here)由于这只针对 2013 年之前的新闻进行训练,因此我需要根据 2013 年之后的新闻更新向量并在词汇表中添加
所有的麻烦都开始了,我无法按画廊的右箭头(右箭头出现在图片中) 我看到只有当我将栏移到右侧时,我才能点击箭头。 如您所见,我在 Firefox 中打开了 F12,指向了网站的右侧部分。 我看到它是空的
我有一些代码运行良好,它只是添加了一个水平新闻提要(或我列出的任何信息)它运行良好,没有闪烁,但是当我向它添加更多数据时,它似乎需要一段时间才能加载并且速度变化?我还有很多信息要添加到其中,但我不想在
我有点坚持 RewriteRule 301,从旧新闻 ID 更改为新新闻 ID 这是我尝试过的: RewriteRule ^/news/0(.*)$ /news/$1 [L,R=301] 假设新闻 U
我对 Google 新闻 RSS 提要的使用有疑问。 Google 新闻帮助说明了这一点: Why Google might block an RSS feed In some cases, Goog
我想在我的网站上加入新闻,但主要主题必须是“书籍”或“作者”等。基本上,我需要选择我提到的这两个或作者姓名等自定义主题。 但我不知道怎么做,因为文档真的很差(或者我找不到)。 添加它的查询参数是什么?
最好的方法是什么? 我想解析新闻,然后使用关键字之类的内容过滤它们并找到匹配项。 有人已经这样做了吗?而且,这是合法的吗? 最佳答案 您可以使用 google 新闻网址 http://news.goo
我有一个获取热门新闻头条的 js 函数。它已按 promise 返回,但我无法访问这些文章。这是我的代码 function news09() { var url = 'https://ne
我想让我的 TableView 加载更多动画,例如 Facebook 新闻,并在底部显示动画指示器事件。 有什么指导可以帮助我做到这一点吗?谢谢。 最佳答案 有几种方法可以做到这一点 在最后一个 in
我正在为我的网站创建一个新闻系统。我有一个主页 {index.php}(显示所有文章)和一个文章页面 (article.php) 我遇到的问题是在文章页面上选择内容。 当你点击 index.php 上
我已成功调用新闻 API 并将结果放入 DataFrame,但仅限于第 1 页。 def get_articles(keyword): all_articles = newsapi.get_eve
我有一个适合我网站的新闻 slider ,我想使用它,但我遇到了一个小问题。完成 HTML 和 CSS 后,我需要旋转“展示柜”,现在我已经尝试使用 Jquery 的不同指令,但一点运气都没有。有人可
因此,我必须根据编写的 javascript 文件(如下)创建搜索查询,并且还必须使用此 URL 来创建搜索查询。在 URL 末尾,您可以添加任何您喜欢的搜索词。例如,我们将搜索食物:https://
我在 TYPO3 8.7.13 中使用来自 Georg Ringer 的新闻扩展。 如果没有选择图像进行预览,扩展程序会显示一个虚拟图像。是否可以改用文章中的第一张图片? 谢谢 最佳答案 当然,您需要
我是typo3 的新手,我需要有关新闻扩展和$TCA 配置的帮助。 我做了一个名为“Activité”的扩展,它从 News 扩展而来。这很顺利。我创建了一些自定义字段,并且能够从“常规”选项卡中已经
我是一名优秀的程序员,十分优秀!