RichTextBlock 中的超链接使应用程序在单击时崩溃 [英] Hyperlink in RichTextBlock makes the application crash on clicking

查看:30
本文介绍了RichTextBlock 中的超链接使应用程序在单击时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Windows Phone 8.1 应用程序中,我有一个带有超链接的 RichTextBlock.我在超链接的点击事件中添加了一些代码块.问题是,每当我单击超链接时,我的应用程序都会因访问冲突"异常而崩溃.这仅在键盘可见时发生.如果键盘被辞职,那么我不会得到例外.此外,这只发生在 WP8.1 设备而不是 W10 设备上.下面是我的代码.

in my Windows Phone 8.1 application I have a RichTextBlock with Hyperlinks in it. I've added some block of code in the click event of the Hyperlink. The problem is that whenever I click on the Hyperlink, my application crashes with an 'Access Violation' exception. This only happens when the keyboard is visible. If the keyboard is resigned then I do not get the exception. Also, this only happens on a WP8.1 device and not a W10 device. Below is my code.

XAML

<StackPanel>
    <RichTextBlock FontSize="40" x:Name="rtb"/>
    <TextBox x:Name="tb"/>
</StackPanel>

背后的代码

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    rtb.Blocks.Add(ContentForRichTextBox("9910918444"));
}

这就是我为 RichTextBox 构建内容的方式

This is how I'm building the content for the RichTextBox

public Paragraph ContentForRichTextBox(string plainString)
{
    Paragraph richContent = new Paragraph();
    try
    {
        string[] plainStringSplit = plainString.Split(' ');
        foreach (var word in plainStringSplit)
        {
            var number = new Hyperlink();
            number.Foreground = new SolidColorBrush(Colors.DarkBlue);
            number.Inlines.Add(new Run { Text = word });
            number.Click += (s, e) =>
            {
                try
                {
                    Windows.ApplicationModel.Calls.PhoneCallManager.ShowPhoneCallUI(word, "Some dude");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception HelperMethod   number.Click : " + ex.ToString());
                }
            };
            richContent.Inlines.Add(number);

            //add a space
            if (plainStringSplit.Last() != word)
            {
                richContent.Inlines.Add(new Run { Text = " " });
            }
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Exception HelperMethod ContentForRichTextBox : " + ex.ToString());
    }
    return richContent;
}

每当我点击超链接并且我的文本框处于焦点时,应用程序就会崩溃并出现异常

Whenever I click on the Hyperlink and my TextBox is in focus, the application crashes with the exception

The program '[2992] App5.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

任何帮助将不胜感激.

推荐答案

我在使用超链接的 WP8.1 应用程序上遇到了同样的问题,导致访问冲突.我使用 am HyperlinkBut​​ton 而不是 Hyperlink 获得了解决方法.要添加 HyperlinkBut​​ton,您需要通过以下方式将其包含在 inlineUIContainer 元素中:

I had the same problem on a WP8.1 app with the HyperLink causing an Access Violation. I get a workaround using am HyperlinkButton instead of an Hyperlink. To add an HyperlinkButton you need to contain it inside an inlineUIContainer element this way:

InlineUIContainer uiContainer = new InlineUIContainer();
HyperlinkButton b = new HyperlinkButton();
b.Content = runText.Text;
b.NavigateUri = uri;
b.Tag = uri;
b.Click += (s, args) =>
{
    //Your click logic here.
};
uiContainer.Child = b;
paragraph.Inlines.Add(uiContainer);

希望这也能帮到你!

这篇关于RichTextBlock 中的超链接使应用程序在单击时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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