【问题标题】:Mono Clipboard fix单声道剪贴板修复
【发布时间】:2015-02-19 16:08:22
【问题描述】:

我正在 Visual Studio 中开发一个可在 Windows 和 MAC 上运行的应用程序(在 Mono 框架 3.6.0 上)。 MAC上有一些问题似乎无法解决:

  1. 复制/粘贴功能不起作用。我知道有人问过:

Clipboard.GetText() always returns empty string in Mono on Mac

NSPasteboard and MonoMac

但我不能在 Winforms 中使用 NSPasteboard。

  1. 应用程序 GUI 在 MAC 上相当缓慢。我有一个 TabControl,当我切换选项卡时,有时来自另一个选项卡的控件会保留并通过其他选项卡控件混合。对于可部署的应用程序来说非常糟糕。 报告了类似的错误:

http://lists.ximian.com/pipermail/mono-bugs/2010-December/107562.html

这些问题有什么解决方法吗?

谢谢

【问题讨论】:

  • 您可以查看当前操作系统,然后选择要使用的方法。例如:mono.wikia.com/wiki/Detecting_the_execution_platform
  • 好的,我已经在进行平台检查,但是如何在 Windows 上通过 VisualStudio 使用 MonoMac 的 NSPasteboard。据我所知,该框架只能在安装了 MonoDevelop 和 xcode 的 MAC 上使用。

标签: c# winforms macos mono


【解决方案1】:

以防万一其他人可能需要它,解决方案是使用 pbcopy/pbpaste 实现复制/粘贴。这是一个帮助类,可用于在 OSX 上复制/粘贴:

public class MacClipboard
{
    /// <summary>
    /// Copy on MAC OS X using pbcopy commandline
    /// </summary>
    /// <param name="textToCopy"></param>
    public static void Copy(string textToCopy)
    {
        try
        {
            using (var p = new Process())
            {

                p.StartInfo = new ProcessStartInfo("pbcopy", "-pboard general -Prefer txt");
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = false;
                p.StartInfo.RedirectStandardInput = true;
                p.Start();
                p.StandardInput.Write(textToCopy);
                p.StandardInput.Close();
                p.WaitForExit();
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
        }


    }

    /// <summary>
    /// Paste on MAC OS X using pbpaste commandline
    /// </summary>
    /// <returns></returns>
    public static string Paste()
    {
        try
        {
            string pasteText;
            using (var p = new Process())
            {

                p.StartInfo = new ProcessStartInfo("pbpaste", "-pboard general");
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();
                pasteText = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
            }

            return pasteText;
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
            return null;
        }

    }
}

不需要像 xsel 那样在 MAC 上进行额外安装。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2018-09-17
    • 2014-04-30
    相关资源
    最近更新 更多