0

我正在尝试将由图标表示的注释添加到 RichTextBlock 中,然后将 Tapped 事件处理程序添加到所有这些中。我发现在以编程方式将其插入页面之前,我无法将处理程序添加到 XAML 中,因此我需要找到具有特定命名模式的每个元素并为每个元素添加一个处理程序。

这是我的代码:

private void assignNoteHandlers()
    {
        //For each note expected on screen
        for ( int i = 0; i < currentTextFile.noteCount; i++ ){
            try
            {
                string noteName = String.Format("note{0}", i);
                Image noteIcon = (Image)pageRoot.FindName(noteName);
                noteIcon.Tapped += noteIcon_Tapped;
            }
            catch
            {
                Debug.WriteLine("The Image note{0} was not found", i);
                continue;
            }
        }
    }

当我查看屏幕时,我可以看到注释图标按原样插入到文本中。

屏幕上的文本来自一个 HTML 文件,我以编程方式在其中为每个注释插入此字符串:

string noteXML = string.Format("<note name=\"note{0}\" src=\"Assets/TEMPNoteIcon.png\" />", i);

使用此 xslt 模板将伪 HTML 转换为 XAML:

<xsl:template match="NOTE | note">
<Span>
  <InlineUIContainer>
    <Image Stretch="None" >
      <xsl:attribute name="x:Name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <!-- DISABLED
      <xsl:attribute name="Name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      -->
      <xsl:attribute name="Source">
        <xsl:value-of select="@src"/>
      </xsl:attribute>
      <xsl:apply-templates />
    </Image>
  </InlineUIContainer>
</Span>
</xsl:template>

我尝试过同时使用上面的“Name”和“x:Name”(但我不能同时使用两者)。

所以,我的问题是,在第一个代码块(最后运行)中,Image noteIcon 始终为空。

我知道这是一个非常复杂的问题,但是任何人都可以帮我解决这个问题吗?

编辑:这是填充 RichTextBlock 的代码(我从网上下载的),

private static async void HtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Get the target RichTextBlock
        RichTextBlock richText = d as RichTextBlock;
        if (richText == null) return;

        // Wrap the value of the Html property in a div and convert it to a new RichTextBlock
        string xhtml = string.Format("<div>{0}</div>", e.NewValue as string);
        RichTextBlock newRichText = null;
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            // In design mode we swallow all exceptions to make editing more friendly
            string xaml = "";
            try {
                xaml = await ConvertHtmlToXamlRichTextBlock(xhtml);
                newRichText = (RichTextBlock)XamlReader.Load(xaml);
            }
            catch (Exception ex) {
                string errorxaml = string.Format(@"
                    <RichTextBlock 
                     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                    >
                        <Paragraph>An exception occurred while converting HTML to XAML: {0}</Paragraph>
                        <Paragraph />
                        <Paragraph>HTML:</Paragraph>
                        <Paragraph>{1}</Paragraph>
                        <Paragraph />
                        <Paragraph>XAML:</Paragraph>
                        <Paragraph>{2}</Paragraph>
                    </RichTextBlock>",
                    ex.Message,
                    EncodeXml(xhtml),
                    EncodeXml(xaml)
                );
                newRichText = (RichTextBlock)XamlReader.Load(errorxaml);
            } // Display a friendly error in design mode.
        }
        else
        {
            // When not in design mode, we let the application handle any exceptions
            string xaml = await ConvertHtmlToXamlRichTextBlock(xhtml);
            newRichText = (RichTextBlock)XamlReader.Load(xaml);
        }

        // Move the blocks in the new RichTextBlock to the target RichTextBlock
        richText.Blocks.Clear();
        if (newRichText != null)
        {
            for (int i = newRichText.Blocks.Count - 1; i >= 0; i--)
            {
                Block b = newRichText.Blocks[i];
                newRichText.Blocks.RemoveAt(i);
                richText.Blocks.Insert(0, b);
            }
        }
    }
4

2 回答 2

0

事实证明,在尝试将注释处理程序附加到图像之前,我的代码没有等待 RichTextBlock 被填充。奇怪,但很简单。

于 2013-06-29T21:11:02.120 回答
0

您不需要通过 XAML 创建 RichTextBlock。你可以这样做:

var rtb = new RichTextBlock();
var paragraph = new Paragraph();
paragraph.Inlines.Add(new Run { Text = "Hello" });
paragraph.Inlines.Add(new InlineUIContainer { Child = new Button { Content = "World" } });
rtb.Blocks.Add(paragraph);
((Grid)this.Content).Children.Add(rtb);
于 2013-06-21T23:25:41.453 回答