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

 

Homepage image
Sample of the Day RSS Feed

Sample Download: https://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 https://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

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

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

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