复制表情符号“文本” 来自在 wpf 中显示图像的 richtextbox

发布于 2024-07-30 20:58:01 字数 110 浏览 5 评论 0原文

我正在寻找一种良好、干净的方法来从显示表情符号的富文本框中复制文本。 想想 Skype,您可以在其中选择一个聊天,它会复制表情图像并将其转换为文本表示(笑脸图像到:)等)。 我正在使用 MVVM 模式。

I am looking for a good, clean way to enable copying of text from a richtextbox displaying emoticons. Think of skype, where you can select a chat and it will copy the emoticon images and convert them to their textual representations (smiley image to :) etc). I am using the MVVM pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2024-08-06 20:58:01

我不知道如何将 RichTextBox 内容解析配置为文本。 下面是使用 xml linq 的一种方法。 正则表达式可能效果更好,但我很烂。 将 ConvertToText 方法传递给 RichTextBox 的 FLowDocument。

private static string ConvertToText(FlowDocument flowDocument)
{
    TextRange textRangeOriginal =
        new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

    string xamlString;
    using (MemoryStream memoryStreamOriginal = new MemoryStream())
    {
        textRangeOriginal.Save(memoryStreamOriginal, DataFormats.Xaml);
        xamlString = ASCIIEncoding.Default.GetString(memoryStreamOriginal.ToArray());
    }

    XElement root = XElement.Parse(xamlString);

    IEnumerable<XElement> smilies =
        from element in root.Descendants()
        where (string)element.Attribute("FontFamily") == "Wingdings" && IsSmiley(element.Value)
        select element;

    foreach (XElement element in smilies.ToList())
    {
        XElement textSmiley = new XElement(element.Name.Namespace + "Span",
                                           new XElement(element.Name.Namespace + "Run", GetTextSmiley(element.Value)));
        element.ReplaceWith(textSmiley);
    }

    using (MemoryStream memoryStreamChanged = new MemoryStream())
    {
        StreamWriter streamWriter = new StreamWriter(memoryStreamChanged);
        streamWriter.Write(root.ToString(SaveOptions.DisableFormatting));
        streamWriter.Flush();
        FlowDocument flowDocumentChanged = new FlowDocument();
        TextRange textRangeChanged =
            new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
        textRangeChanged.Load(memoryStreamChanged, DataFormats.Xaml);
        return textRangeChanged.Text;
    }
}

private static string GetTextSmiley(string value)
{
    switch (value)
    {
        case "J" :
            return ":)";
        case "K" :
            return ":|";
        case "L" :
            return ":(";
        default :
            throw new ArgumentException();
    }
}

private static bool IsSmiley(string value)
{
    return value == "J" || value == "K" || value == "L";
}

I don't know of a way to configure the parsing of RichTextBox content to text. Below is one way which uses xml linq. Regular expressions might work better but I suck at them. Pass ConvertToText method teh FLowDocument of your RichTextBox.

private static string ConvertToText(FlowDocument flowDocument)
{
    TextRange textRangeOriginal =
        new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

    string xamlString;
    using (MemoryStream memoryStreamOriginal = new MemoryStream())
    {
        textRangeOriginal.Save(memoryStreamOriginal, DataFormats.Xaml);
        xamlString = ASCIIEncoding.Default.GetString(memoryStreamOriginal.ToArray());
    }

    XElement root = XElement.Parse(xamlString);

    IEnumerable<XElement> smilies =
        from element in root.Descendants()
        where (string)element.Attribute("FontFamily") == "Wingdings" && IsSmiley(element.Value)
        select element;

    foreach (XElement element in smilies.ToList())
    {
        XElement textSmiley = new XElement(element.Name.Namespace + "Span",
                                           new XElement(element.Name.Namespace + "Run", GetTextSmiley(element.Value)));
        element.ReplaceWith(textSmiley);
    }

    using (MemoryStream memoryStreamChanged = new MemoryStream())
    {
        StreamWriter streamWriter = new StreamWriter(memoryStreamChanged);
        streamWriter.Write(root.ToString(SaveOptions.DisableFormatting));
        streamWriter.Flush();
        FlowDocument flowDocumentChanged = new FlowDocument();
        TextRange textRangeChanged =
            new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
        textRangeChanged.Load(memoryStreamChanged, DataFormats.Xaml);
        return textRangeChanged.Text;
    }
}

private static string GetTextSmiley(string value)
{
    switch (value)
    {
        case "J" :
            return ":)";
        case "K" :
            return ":|";
        case "L" :
            return ":(";
        default :
            throw new ArgumentException();
    }
}

private static bool IsSmiley(string value)
{
    return value == "J" || value == "K" || value == "L";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文