WPF发送包含超链接的聊天消息

时间:2018-03-27 12:26:37

标签: c# wpf

我遇到一个小问题,我使用c#创建了一个私人聊天消息系统。现在我需要的是一种向其他人发送可点击链接的方法。

当从列表中选择一个人时,我按下“邀请”按钮,消息将显示在消息框中,例如“对于User1:从此链接加入

private void InvBtn_Click(object sender, RoutedEventArgs e)
{
    selectedUser = UsersListBox.SelectedItem.ToString();
    if (selectedUser != login)
    {
        MessageBox.Show("Select other user than yourself");
        return;
    }
    else
    {               
        Msg.Text = selectedUser + " join from this 'link' ";
    }
}

发送给另一个人后,获取消息给RichTextBox说

来自user2:从链接

加入

没有必要打开一个网站,但其他形式的更多细节。

2 个答案:

答案 0 :(得分:0)

您需要使用超链接按钮创建自定义MessageBox。

试试这个,这里你需要正确设置高度和宽度属性....并使构造函数接受参数,以便用户可以按照他们想要的方式设计它。

 public class CustomMessageBox
    {
        public CustomMessageBox()
        {
            Window w = new Window();
            DockPanel panel = new DockPanel();
            TextBlock tx = new TextBlock();
            Paragraph parx = new Paragraph();
            Run run1 = new Run("Text preceeding the hyperlink.");
            Run run2 = new Run("Text following the hyperlink.");
            Run run3 = new Run("Link Text.");
            Hyperlink hyperl = new Hyperlink(run3);
            hyperl.NavigateUri = new Uri("http://search.msn.com");
            tx.Inlines.Add(hyperl);
            panel.Children.Add(tx);
            w.Content = panel;
            w.Show();
        }
    }

来源:https://social.msdn.microsoft.com/Forums/vstudio/en-US/57fcd28b-6e9e-4529-a583-892c8f6d7cc8/hyperlink-in-wpf-message-box?forum=wpf

答案 1 :(得分:0)

首先,您需要想出一种在短信中包含特殊标记的方法。您可以使用现有容器格式(XML,JSON等)打包整个消息,也可以简单地在文本中包含特殊标记,例如:

Hi User1, join from [This link:12345].

您可以将标记添加到其他内容中,例如粗体**bold**),斜体*italics*)或实际的超链接网站。

另一方面,您将需要一个检测此特殊标记的解析器,并将其替换为可单击的链接。在以下示例中,我使用Regex查找并替换[Text:Command]格式的所有文本。

private IEnumerable<Inline> Parse(string text)
{
    // Define the format of "special" message segments
    Regex commandFinder = new Regex(@"\[(?<text>.+)\:(?<command>.+)]");

    // Find all matches in the message text
    var matches = commandFinder.Matches(text);

    // remember where to split the string so we don't lose other 
    // parts of the message
    int previousMatchEnd = 0;

    // loop over all matches
    foreach (Match match in matches)
    {
        // extract the text fore it
        string textBeforeMatch = text.Substring(previousMatchEnd, match.Index - previousMatchEnd);
        yield return new Run(textBeforeMatch);

        previousMatchEnd = match.Index + match.Length;

        // extract information and create a clickable link
        string commandText = match.Groups["text"].Value;
        string command = match.Groups["command"].Value;

        // it would be better to use the "Command" property here,
        // but for a quick demo this will do
        Hyperlink link = new Hyperlink(new Run(commandText));
        link.Click += (s, a) => { HandleCommand(command); };

        yield return link;
    }

    // return the rest of the message (or all of it if there was no match)
    if (previousMatchEnd < text.Length)
        yield return new Run(text.Substring(previousMatchEnd));
}

在收到邮件的方法中,您可以像这样简单地集成它:

// Where you receive the text
// This probably is just a `txtOutput.Text += ` until now
private void OnTextReceived(string text)
{
    txtOutput.Inlines.AddRange(Parse(text));
}

// the method that gets invoked when a link is clicked
// and you can parse/handle the actual command
private void HandleCommand(string command)
{
    MessageBox.Show("Command clicked: " + command);
}

消息Hi User1, join from [this link:1234567890]将显示为Hi User1, join from this link,并会在点击时调用HandleCommand("1234567890")

enter image description here