2010-12-16 30 views
1

我從MSDN文檔中瞭解到,無法使用XAML屬性導出屬於RichTextBox一部分的圖像。這很好,我可以通過選擇並手動查看塊來解決這個問題。使用xaml將圖像載入silverlight richtextarea

我的問題是,如果我手動重新構建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。

+0

如果我有時間,我想我可以得到符號Silverlight和調試到代碼,只是不想花這多少時間就可以了! :( – Andy 2010-12-17 09:44:55

回答

2

RichTextBox上的Xaml屬性不支持InlineUIContainer,無論是輸入還是輸出。

一個解決我會嘗試首先是使用XamlReader您的XAML,而不是然後將結果添加到收藏RichTextBox.Blocks -

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

這也行不通,因爲XamlReader會出現同樣的錯誤。 – 2010-12-17 12:58:35

+0

哇,趕緊!:)我發佈時沒有看到您的回覆! @Akash Xaml讀者確實很有用! :) – Andy 2010-12-17 13:31:16

+0

謝謝。非常好。 – 2012-01-25 13:50:19

0

好吧,我已經找到一種方法來做到這一點,所有的 - 不會將XAML直接加載到使用XAML屬性的RTB中。

 // 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); 
      } 
     } 

要與圖像到RTB我已經恢復到加載XAML成對象首先使用XamlReader對象,然後一個接一個地添加這些塊,按照該代碼加載XAML不像我所能幫助的那樣整潔,但我猜XAML屬性並不解析InlineUIElements。

Andy。

0

在XAML中./desert.jpg源碼不起作用。相反,使用這一個

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

這裏有兩個重要的關鍵詞第一個是你的命名空間ProjectBus

的secondone固定「組件

那麼你的ImagePath你需要寫。 否則,即使它可以在designtime上顯示,有時它在運行時也不起作用。

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

希望幫助