【问题标题】:How do I change the mouse cursor in Xamarin.forms when hovering over a hyperlink?将鼠标悬停在超链接上时如何更改 Xamarin.forms 中的鼠标光标?
【发布时间】:2019-08-28 18:44:35
【问题描述】:

Xamarin.forms 3.3.0 update 中建议通过以下方式创建超链接:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="This app is written in C#, XAML, and native APIs using the" />
                <Span Text=" " />
                <Span Text="Xamarin Platform" FontAttributes="Bold" TextColor="Blue" TextDecorations="Underline">
                    <Span.GestureRecognizers>
                       <TapGestureRecognizer 
                            Command="{Binding TapCommand, Mode=OneWay}"
                            CommandParameter="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/"/>
                     </Span.GestureRecognizers>
                </Span>
                <Span Text="." />
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
</Label>

通常,在 Windows 上,鼠标光标在悬停在超链接上时会发生变化。 Xamarin.forms 中是否有办法获得相同的鼠标光标变化?

【问题讨论】:

  • 你的问题是关于 UWP 的吧?因为在手机上没有指针
  • 我认为 Span 还不支持此功能,甚至不打算支持。你应该打开一个请求这个的问题
  • @Christian - 你在开发移动应用吗?如果是,那么您如何获得光标?
  • 是的,UWP中需要光标。
  • 是的,光标在UWP中主要是需要的,但Android也支持mouse cursors

标签: xamarin xamarin.forms uwp mouseover mouse-cursor


【解决方案1】:

我认为您可以为 UWP 创建自定义渲染器。 例如,像这样:

[assembly: ExportRenderer(typeof(HyperLinkLabel), typeof(HyperLinkLabel_UWP))]
namespace MyApp.UWP.CustomRenders
{
    public class HyperLinkLabel_UWP: LabelRenderer
    {
        private readonly Windows.UI.Core.CoreCursor OrigHandCursor = Window.Current.CoreWindow.PointerCursor;

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                Control.PointerExited += Control_PointerExited;
                Control.PointerMoved += Control_PointerMoved;
            }
        }

        private void Control_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            Windows.UI.Core.CoreCursor handCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 1);
            if (handCursor != null)
                Window.Current.CoreWindow.PointerCursor = handCursor;
        }

        private void Control_PointerExited(object sender,     Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if (OrigHandCursor != null)
                Window.Current.CoreWindow.PointerCursor = OrigHandCursor;
        }
    }
}

【讨论】:

  • 这是理想的编程体验,只需复制粘贴即可。谢谢!
猜你喜欢
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 2013-08-01
  • 1970-01-01
相关资源
最近更新 更多