I have a ResourceDictionary called CustomStyles.xaml within in my project. It is located in a subdirectory called RD, therefore its location is RD/CustomStyles.xaml.开发者_高级运维
My question is the following: I have a class-only file called CustomGroupBox.vb, and in the New(), I would like to access resources from the ResourceDictionary.
How can I do this since I have no corresponding XAML for CustomGroupBox.vb?
P.S. I'd like to note that CustomStyles.xaml has a BuildAction of Resource and not Content, therefore it is compiled along with the project, it does NOT output to the build directory (\bin). Therefore the following wouldn't work...
Me.Resources.Source = New Uri("RD\CustomStyles.xaml")
Seems like you need to use Application.Current.Resources to find your resources, assuming the resource dictionary is defined at the Application level. Example:
YourControl.Style = CType(Application.Current.Resources("OneOfYourStyles"), Style)
Edit: For a resource in an assembly and not the application, you can use Pack URI Syntax (link) to access the resource. It would probably look something like this:
Dim u as Uri = New Uri("/" + Me.GetType().Assembly.GetName().Name + ";RD/CustomStyles.xaml"
You can just pull the resource from the assembly.
This blog post will show you how.
精彩评论