使用 xaml 将图像加载到 silverlight richtextarea 中

发布于 2024-10-07 15:07:16 字数 1636 浏览 2 评论 0原文

我从 MSDN 文档中知道,您无法使用 XAML 属性导出属于 RichTextBox 一部分的图像。这很好,我可以通过手动反思和查看块来解决这个问题。

我的问题是,如果我手动重新构建 XAML 以包含图像,RichTextBox 是否能够从 xaml 加载它。

我已经实现了反射和手动 XAML 导出,并且无需图像即可完美运行。

对于图像,它会生成以下内容:

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph TextAlignment="Left" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  >
<Run Text="Test" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  />
</Paragraph>
<Paragraph TextAlignment="Left" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  >
<InlineUIContainer>
<Image Source="./desert.jpg" Height="150" Width="200" />
</InlineUIContainer>
<Run Text="" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  />
</Paragraph>
</Section>

我通过 XAML 属性将其反馈到 RTB 并中断! (异常是无用的,只是一个 IllegalArgmentException 说“值”。

如果你只取出 InlineUIContainer 部分,那就没问题了!

我无法确定是否可能是图像位置错误或 RichTextBox 只是不接受图像的问题 (

我认为可能从 xaml 指定图像的唯一原因是因为 MSDN 文档显示了它: http://msdn.microsoft.com/en-us/library/ee681613(VS.95).aspx

有什么想法吗?

Ta,

Andy。

I know from MSDN documentation that you cannot export an Image which is part of a RichTextBox using the XAML property. This is fine, I can work around that by relection and looking through the blocks manually.

My question is, if I re-build the XAML manually to include an Image, would the RichTextBox be able to load it from xaml.

I've implemented the reflection and manual XAML export already and it works perfectly without images.

With images it produces this:

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph TextAlignment="Left" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  >
<Run Text="Test" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  />
</Paragraph>
<Paragraph TextAlignment="Left" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  >
<InlineUIContainer>
<Image Source="./desert.jpg" Height="150" Width="200" />
</InlineUIContainer>
<Run Text="" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000"  />
</Paragraph>
</Section>

Which I feed back into the RTB via the XAML property and breaks! (The exception is useless, just an IllegalArgmentException saying 'Value'.

If you take out just the InlineUIContainer section its fine!

I can't work out if its possibly a problem with the image location being wrong or the RichTextBox just not accepting images apart from in-code.

The only reason why I think it's possibly to specify an image from xaml is because the MSDN documents shows it: http://msdn.microsoft.com/en-us/library/ee681613(VS.95).aspx.

Any ideas?

Ta,

Andy.

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

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

发布评论

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

评论(3

何以笙箫默 2024-10-14 15:07:16

RichTextBox 上的 Xaml 属性不支持 in 或 out 的 InlineUIContainer

我首先尝试的一种解决方法是在 xaml 上使用 XamlReader,然后将结果添加到 RichTextBox.Blocks 集合中:-

 Section section = (Section)XamlReader.Load(yourXaml);
 yourRTB.Blocks.Add(section);

The Xaml property on the RichTextBox does not support InlineUIContainer either in or out.

One work around I would try first is to use the XamlReader on your xaml instead then add the result to the RichTextBox.Blocks collection:-

 Section section = (Section)XamlReader.Load(yourXaml);
 yourRTB.Blocks.Add(section);
庆幸我还是我 2024-10-14 15:07:16

好吧,我找到了一种方法来做到这一点,无论如何,不​​要使用 XAML 属性将 XAML 直接加载到 RTB 中。

要将带有图像的 XAML 加载到 RTB 中,我必须恢复为首先使用 XamlReader 对象将 XAML 加载到对象中,然后按照以下代码逐一添加块:

        // Load up the XAML using the XamlReader
        Object o = XamlReader.Load(xamlTb.Text);
        if (o is Section)
        {
            // Make sure its a section and clear out the old stuff in the rtb
            Section s = o as Section;
            rtb.Blocks.Clear();

            // Remove the blocks from the section first as adding them straight away
            // to the rtb will throw an exception because they are a child of two controls.
            List<Block> tempBlocks = new List<Block>();
            foreach (Block block in s.Blocks)
            {
                tempBlocks.Add(block);
            }
            s.Blocks.Clear();

            // Add them block by block to the RTB
            foreach (Block block in tempBlocks)
            {
                rtb.Blocks.Add(block);
            }
        }

不像我所帮助的那么简洁,但我猜测 XAML 属性只是不解析 InlineUIElements。

安迪.

Well, I've found a way to do it, all-be-it not loading the XAML straight into the RTB using the XAML property.

To load XAML with images into the RTB I've had to revert to loading the XAML into objects first using the XamlReader object, then adding the blocks one by one, as per this code:

        // Load up the XAML using the XamlReader
        Object o = XamlReader.Load(xamlTb.Text);
        if (o is Section)
        {
            // Make sure its a section and clear out the old stuff in the rtb
            Section s = o as Section;
            rtb.Blocks.Clear();

            // Remove the blocks from the section first as adding them straight away
            // to the rtb will throw an exception because they are a child of two controls.
            List<Block> tempBlocks = new List<Block>();
            foreach (Block block in s.Blocks)
            {
                tempBlocks.Add(block);
            }
            s.Blocks.Clear();

            // Add them block by block to the RTB
            foreach (Block block in tempBlocks)
            {
                rtb.Blocks.Add(block);
            }
        }

Not as neat as I would have helped, but I guess the XAML property just doesn't parse InlineUIElements.

Andy.

荒芜了季节 2024-10-14 15:07:16

在 XAML ./desert.jpg 源中将不起作用。相反,使用这个

<Image Source="YourNameSpaceBus;component/images/desert.jpg" 
Height="150" Width="200" />

这里有两个重要的关键字,第一个是您的命名空间ProjectBus

第二个是固定的“组件”,

然后是您需要编写的图像路径。
否则,即使它在设计时可以显示,有时它在运行时也不起作用。

比如

<Image Source="AHBSBus;component/images/mail.png" Stretch="None" Height="23">
</Image>

希望有帮助

In XAML ./desert.jpg source will not work. Instead use this one

<Image Source="YourNameSpaceBus;component/images/desert.jpg" 
Height="150" Width="200" />

Here are two important keywords first one is your namespace ProjectBus

The secondone is fixed "component"

then your imagepath you need to write.
Otherwise even if its displayable on designtime sometimes it doesnt work on runtime.

such as

<Image Source="AHBSBus;component/images/mail.png" Stretch="None" Height="23">
</Image>

Hope helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文