您如何允许用户从Xamarin.Forms标签复制和粘贴 [英] How do you allow users to copy and paste from an Xamarin.Forms label

查看:245
本文介绍了您如何允许用户从Xamarin.Forms标签复制和粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何允许用户从Xamarin.Forms标签复制和粘贴?

How do you allow users to copy and paste from an Xamarin.Forms Label?

在任何平台上单击文本,默认设置不允许突出显示,因此不允许复制和粘贴.

Click on the text on any platform the default settings don't allow highlighting and therefore copying and pasting.

任何帮助将不胜感激.

推荐答案

您可以做的是将标签包裹在手势识别器中:

What you could do is wrap your label in a gesture recogniser:

<Label Text="Test">
    <Label.GestureRecognizers>
        <TapGestureRecognizer
            Tapped="YourFunctionToHandleMadTaps" 
            NumberOfTapsRequired="1" 
        />
   </Label.GestureRecognizers>
</Label>

这将触发您的功能,在该功能中,您可以进入剪贴板并复制和粘贴.但是,我还没有找到一种简单的方法来访问Xamarin.Forms中的剪贴板,因此您必须使用依赖项服务.

This will trigger your function and in that function you can get to the clipboard and copy and paste. However I haven't been able to find an easy way to access the clipboard in Xamarin.Forms so you have to use the dependency service.

Xamarin.Forms依赖服务文档

这是我进行剪贴板数据访问的方式.请注意,在我的项目中,我只需要从剪贴板获取数据,因此此代码仅向您展示如何访问剪贴板数据:

Here is how I did my clipboard data access. Please note that in my project I only needed to nab data from the clipboard so this code just shows you how to access the clipboard data:

  1. 在您的X.F项目中创建一个接口,例如:

  1. Create an interface in you X.F project eg:

    public interface IClipBoard
    {
        String GetTextFromClipBoard();
    }

  • 在您的移动项目中实现界面:

  • Implement the interface in your mobile projects:

    Android:
        public string GetTextFromClipBoard ()
        {
            var clipboardmanager = (ClipboardManager)Forms.Context.GetSystemService (Context.ClipboardService);
            var item = clipboardmanager.PrimaryClip.GetItemAt(0);
            var text = item.Text;
            return text;
        }
    
    iOs:
        public string GetTextFromClipBoard ()
        {
            var pb = UIPasteboard.General.GetValue ("public.utf8-plain-text");
            return pb.ToString ();
        }
    

  • 别忘了在顶部添加Assembly位:

    Don't forget to add the Assembly bits at the top:

        iOs: [assembly: Dependency (typeof (ClipBoard_iOs))]
        Android: [assembly: Dependency (typeof (ClipBoard_Droid))]
    

    1. 从您的X.F函数调用依赖项服务

    1. Call the dependency service from you X.F function

        public void YourFunctionToHandleMadTaps(Object sender, EventArgs ea)
        {
            var clipboardText = DependencyService.Get<IClipBoard> ().GetTextFromClipBoard ();
    
            YourFunctionToHandleMadTaps.Text = clipboardText;
        }
    

    这篇关于您如何允许用户从Xamarin.Forms标签复制和粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆