How to add ResourceDictionary in Phone class library project and access it

自作多情 提交于 2019-12-07 05:36:22

问题


I am working on a project where i have child project which is referencing the library project.

In my Library project(Phone class library) how do i create ResourceDictionary.xaml where i need to add some styles and use it in xaml files and as well as .cs files.

I need to access styles in ResourceDictionary.xaml in my xaml files as well as .cs files how to do it ?


回答1:


Create a folder as Generic in the root folder and have your resource files there...

To access it in XAML

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Generic.xaml" />
  </ResourceDictionary.MergedDictionaries>

or

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/AssembleName;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>

To access it in .cs

new URI(pack://application:,,,/AssembleName;component/Generic.xaml)



回答2:


OK, Finally i solved it myself. here is how i solved it.

When you create Phone Class Library Project you will not the ResourceDictionary.xaml by default, so what create a xaml page,like how you create a normal xaml file.

Right click on solution ->Add New Item ->Windows Phone Portrait Page.

Now remove the .cs file that is created, add in the .XAML file add your style which is within ResourceDictionary tag.

The above is all done in Library project.

Now in the child app:

<Application.Resources>
        <ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="LibraryResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

suppose you have a style defined in ResourceDictionary as MainScreenButtonStyle you can set in this way

button.Style = (Style)Application.Current.Resources["MainScreenButtonStyle"] as Style;


来源:https://stackoverflow.com/questions/21671030/how-to-add-resourcedictionary-in-phone-class-library-project-and-access-it

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