0
聽起來很簡單,它讓我難過!從外部程序集中使用帶有靜態資源的usercontrols
我想在一個外部程序集的ResourceDictionary中使用一個用戶控件,但我在運行時遇到異常。
下面是如何重現:
- 創建Silverlight類庫稱爲MyControls.dll
創建一個名爲SuperControl的用戶控件:
<UserControl.Resources> <ResourceDictionary Source="MyControls;component/Styles.xaml" x:Key="Styles" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <TextBlock Style="{StaticResource MyStyle}" Text="Hello"/> </Grid>
創建Styles.xaml ResourceDictionary中和加:
<Style x:Key="MyStyle" TargetType="TextBlock"> <Setter Property="FontSize" Value="15"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="TextWrapping" Value="Wrap"/> <Setter Property="Margin" Value="0,15,0,4"/> <Setter Property="HorizontalAlignment" Value="Left"/> </Style>
創建Silverlight應用程序調用SL並添加Mycontrols作爲參考
在MainPage.xaml中網格,添加:
<MyControls:SuperControl />
這將編譯,但在運行應用程序「無法分配給屬性'System.Windows.ResourceDictionary.Source'。 [行:10職位:36]?」
我已將此添加到應用程序的App.xaml
<ResourceDictionary Source="/MyControls;component/Styles.xaml" />
同樣的錯誤... :(
有什麼想法
我有類似的問題和原因是在引用文件時使用反斜槓(\)而不是正斜槓(/)。 VS中的xaml解析器能夠解析位置,但運行時正在生成錯誤。希望這會幫助別人。 – 2011-08-09 18:27:09