Condividi tramite


Creating reusable resources

A resource is an object that can be reused in different places in your Blend for Visual Studio 2012 application.

The most common use of a resource is in reusing a color throughout your application. For example, you can change a color resource in one place to automatically update the appearance of every control that refers to that color resource. This concept also applies to reusing style or template resources to make controls of a specific type look or behave the same way. Changing one asset that has multiple relationships is easier than changing many instances of data throughout your application.

Virtually everything can be converted to some kind of a resource:

  • A gradient, solid color, or tile brush can be converted to a brush resource and then applied as a brush to other objects.

    For an example, see Create a brush resource or color resource.

  • An image control can be converted to an image brush, drawing brush, or visual brush resource and then applied as a brush to other objects. Use the conversion tools that are available under Make Brush Resource on the Tools menu.

  • Any panel or other control on the artboard can be converted to a drawing brush or visual brush resource and then applied as a brush to other objects. Use the conversion tools that are available under Make Brush Resource on the Tools menu.

  • A property value in the Properties panel can be converted to a resource value and then applied to properties of the same type.

    For an example, see Create a resource.

  • A style or control template can be converted to a resource and then applied to a control of the same type (for example, a button template can be applied to other buttons).

    For more information, see Styling a control that supports templates.

    For examples, see Create a style and Create or modify a template.

    [!NOTA]

    Style and control template resources can be selected from the Assets panel and then drawn on the artboard, just like system controls. For more information about the controls in the Assets panel, see Assets panel.

Resources can be created at various levels or scopes in your application. The scope where a resource is defined determines where it is available in your application. You can define resources at the following scopes:

  • An object-level resource can only be applied to the object that was used to create the resource, or to its child objects.

  • A document-level resource is defined in the document where it was created and can only be applied to objects in the same document.

  • An application-level resource is defined in the App.xaml file and can be applied anywhere within your application.

  • A dictionary-level resource is defined in a separate file called a resource dictionary. Resources in a resource dictionary can be applied anywhere within your application because resource dictionaries are linked to your project in the App.xaml file. An additional benefit of using resource dictionaries is that you can package a set of resources into a resource dictionary file and reuse the entire file in another application.

    [!NOTA]

    Blend comes with a resource dictionary that contains a set of style and template resources for common system controls in Windows Presentation Foundation (WPF) projects. For more information, see Styling a control that supports templates. For specific examples, see Styling tips for WPF Simple Styles.

    [!NOTA]

    Microsoft Expression Design uses resource dictionaries to export art assets that can be imported into an Blend application and accessed from the Resources panel. For an example, see Import XAML that is exported from Expression Design.

Existing resources can be moved between different scopes by dragging them between locations in the Resources panel. Additionally, you can access an existing resource for editing from the Resources panel.

For information about editing resources, see Modify a resource.

For a roadmap of the Resources panel, see the Resources panel.

For information about applying a resource to an object on the artboard, see Apply or remove a resource.

When applying a resource that is a style or control template, you can use the additional method that is described in Apply or remove a resource.