[Sample of Apr 7th] Define and Access Shared Resources in WPF Class Library - Microsoft All-In-One Code Framework - Site Home - MSDN Blogs
Microsoft All-In-One Code Framework - Developers' Pain Killer

[Sample of Apr 7th] Define and Access Shared Resources in WPF Class Library

Rate This
  • Comments 1

 

Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSWPFSharedResources-4d5f6e2a

Today’s sample demonstrates the approach of defining and using shared resources in WPF class library. Accessing shared resources using markup extension.  Normally in a WPF class library the resources used in the controls will not be reflected at the design time. This approach of combining the shared resources along with markup extension helps us to overcome this limitation.

The sample is written by Microsoft engineer: Shiva N Shankar.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

This sample demonstrates the approach of

Defining and using shared resources in WPF class library.

Accessing shared resources using markup extension.

Normally in a WPF class library the resources used in the controls will not be reflected at the design time. This approach of combining the shared resources along with markup extension helps us to overcome this limitation.

Running the Sample

  1. Open the solution in Visual Studio.
  2. Rebuild the solution.
  3. Under the “SharedResource_UsingMarkupExtension” project open the Design view of “CustomRect.xaml”. The page looks empty. This page is using shared resource without MarkupExtension.
  4. Open design view of “CustomRectangle.xaml”. We can see the image is displayed from the resource file” MyResources.xaml”.
  5. The design of user control “CustomRect.xaml” will be viewable only when used in a WPF application. We overcome this limitation by using markup extension.
  6. To test the working of this sample, hit “Ctrl+F5”.
  7. A new window will open. The right side with “Grey” background shows the usercontrol using SharedResource with MarkupExtension and the left side usercontrol use SharedResource without MarkupExtension.

image

Using the Code

This sample solves the problem having a common place to have application level resources in WPF class library by using Shared resources. Also, we can see the design of the user control in the WPF class library without being implemented in a WPF application by using markup extension.

You can add more property to the SharedResourceExtension and include more logic in the ProvideValue method to modify the code as per the requirement.

// Adding new parameter. private object defaultvalue; public object Default { 
    get     {         return defaultvalue;     }     set     { 
                defaultvalue = value;     } }    // Constructor accepting two parameter. 
public SharedResourceExtension(object key, object defaultvalue) { 
    Key = key;     this.Default = defaultvalue; }   
public override object ProvideValue(IServiceProvider serviceProvider) {     object localValue = SharedResourceDictionaryManager.SharedResourceDictionary[Key]; 
      if (localValue == null)     { 
        return SharedResourceDictionaryManager.SharedResourceDictionary[Key]; 
    }       return localValue; } 

 

More Information

http://msdn.microsoft.com/en-us/library/ee855815.aspx 

http://msdn.microsoft.com/en-us/library/ms747254.aspx

http://msdn.microsoft.com/en-us/library/ms747254.aspx

Leave a Comment
  • Please add 8 and 6 and type the answer here:
  • Post
  • Cool.

    However, it doesn't work when used in a Style or Template.

    I'm trying to used SharedResources in WPF in a hosted environment (thus no App.xaml or Application.Resources).

    And to get it to work in runtime AND design time. I've read Dr.WPF's "Managing Application Resources when WPF is Hosted" and it's working in runtime, but doesn't provide any support for design time.

    I've tried the DesignTimeResources trick described for Blend4 and supposed to work with VS2010, but it doesn't.

    The internet is full of posts on this topic, and no one seems to have an answer that really works. Sigh.

Page 1 of 1 (1 items)