Using Styles in merged ResourceDictonaries in Silverlight 5

落爺英雄遲暮 提交于 2019-12-11 01:38:47

问题


I've got a bunch of styles in my app.xaml and they are all being used just fine in my pages within my SL5 app. I'd like to move these styles to multiple resource dictionaries to make it more manageable and consumable.

First I copied a style to a new resource dictionary in the /Styles/ButtonStyles.xaml page in my project... a snippet looks like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Style x:Key="RegistrationsRolloverImage" TargetType="Button">
    <Setter Property="Template">...</Setter>
  </Style>

  <Style x:Key="FinancialLedgerRolloverImage" TargetType="Button">
    <Setter Property="Template">...</Setter>
  </Style>

</ResourceDictionary>

Next I added the following to my App.xaml:

<ResourceDictionary x:Key="MergedStyles">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Styles/ButtonStyles.xaml" />
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

It forced me to add a x:key to the ResourceDictionary tag as I kept getting a build error. Now it builds, but the buttons that use the style aren't getting the style. In fact I'm getting a JS error that it can't find a style with the name of the two styles in my resource dictionary. They work just fine if they are in the App.xaml, but not if they are in seperate resource dictionary. I reflected the generated DLL and can see the styles/buttonstyles.xaml in the DLL.

At a loss... and can't figure out what's wrong. Ideas?


回答1:


In the full App.xaml sample you want use "local" resources. But when you have "local" resources and you want to merge a resource directory the systax a little bit different.

Try it like this:

<Application ...>
  <Application.Resources>
    <ResourceDictionary>

      <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles/ButtonStyles.xaml" />
      </ResourceDictionary.MergedDictionaries>

      <Style x:Key="BaseTextBlock" TargetType="TextBlock">
         ...
      </Style>

    </ResourceDictionary>
  </Application.Resources>
</Application>



回答2:


Are they within the same project? Try something more like this in your app.xaml;

<Application.Resources>
   <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/YourResDictionaryContaining.Proj.Name;component/Styles/ButtonStyles.xaml" />
      </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</Application.Resources>

I have to do this to have consolidation of resourcedict's stored in one project, and then add that to the app.xaml of each other project to make them available globally. Currently I run about 6 Resource Dicts acros 20 projects in the same solution this way and it works great.



来源:https://stackoverflow.com/questions/10823709/using-styles-in-merged-resourcedictonaries-in-silverlight-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!