【问题标题】:Loading an image into silverlight richtextarea using xaml使用 xaml 将图像加载到 silverlight richtextarea
【发布时间】:2012-04-05 17:49:16
【问题描述】:

我从 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 说 'Value'。

如果你只取出 InlineUIContainer 部分就可以了!

我无法确定这是否可能是图像位置错误或 RichTextBox 只是不接受除代码外的图像的问题。

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

有什么想法吗?

塔,

安迪。

【问题讨论】:

  • 如果我有时间,我想我可以得到silverlight 的符号并调试到代码中,只是不想花这么多时间在上面! :(

标签: silverlight image xaml richtextbox


【解决方案1】:

RichTextBox 上的 Xaml 属性不支持 InlineUIContainer 输入或输出。

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

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

【讨论】:

  • 这也不起作用,因为 XamlReader 会出现同样的错误。
  • 哇,快! :) 我发帖时没有看到你的回复!谢天谢地,@Akash Xaml 阅读器确实有效! :)
【解决方案2】:

好吧,我已经找到了一种方法,它不使用 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。

安迪。

【讨论】:

    【解决方案3】:

    在 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>
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多