アイコンで表されるメモを 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);
この疑似 HTML は、次の xslt テンプレートを使用して 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 が常に null であることです。
これは非常に複雑な質問であることはわかっていますが、誰かがこれを理解するのを手伝ってくれますか?
編集: RichTextBlock を埋めるコードは次のとおりです (Web からダウンロードしました)。
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);
}
}
}