Compartilhar via


Passo a passo: Gerenciando Recursos em seu Projeto WPF

Resources are .NET objects that can be accessed and used by your application. Examples of resources include brushes for color schemes or images. You can use the ResourceDictionary class to organize all of your resources into one easy-to-manage file and access them from your application.

In this walkthrough, you perform the following tasks:

When you are finished, you will know how to add a ResourceDictionary to your application and to access resources contained in that ResourceDictionary.

ObservaçãoObservação

Caixas de diálogo e comandos de menu que você vê podem diferir das descritas na Help dependendo das suas configurações ativas ou edição. Para alterar as configurações, escolha Import and Export Settings sobre o Ferramentas menu. For more information, see Trabalhando com configurações.

Pré-requisitos

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Adding a ResourceDictionary

The first step is to add a ResourceDictionary to your application.

To add a ResourceDictionary to your application

  1. Create a new WPF Application project in Visual Basic or Visual C# named ManageResources. For more information, see Como: Criar um novo projeto de aplicativo WPF.

    MainWindow. XAML é aberto no WPF Designer.

  2. Em Solution Explorer, à direita, clique no projeto de ManageResources de-e selecione Adicionar | Dicionário de recurso.

    The Add New Item dialog box opens.

  3. Verify that Dictionary1.xaml appears in the Name box and click Add.

    A new ResourceDictionary named Dictionary1.xaml is added to your project and is opened in the WPF Designer.

Adding a Resource to the ResourceDictionary

The added ResourceDictionary can be edited in the XAML editor and can serve as a central repository for your resources. You will now add a resource to your ResourceDictionary.

To add a resource to your ResourceDictionary

  1. Make sure Dictionary1.xaml is opened in the WPF Designer.

  2. In XAML view, add the following XAML markup after the opening <ResourceDictionary> tag:

    <SolidColorBrush Color="Green" x:Key="myBrush"></SolidColorBrush>
    
  3. On the File menu, select Save All.

    You have added a SolidColorBrush named myBrush as a resource to be available to your application.

Accessing the Resource

In this procedure, you will access the SolidColorBrush resource and use it in your main window.

To access a resource

  1. Abra MainWindow. XAML.

  2. No modo XAML , adicione o seguinte XAML após a <Window> Iniciar a marca, mas antes do <Grid> marca de início:

        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Dictionary1.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
    

    Essa ação importa o ResourceDictionary definido em Dictionary1.xaml e mescla com outros dicionários de recurso no projeto.

  3. From the Toolbox, drag a Button control onto the window.

  4. No janela Propriedades, role até o Background propriedade.

  5. Na borda da coluna esquerda, clique na marcador de propriedade de herança (Ícone de herança de marcador de propriedade).

    Aparecerá um menu .

    Dica

    Você também pode direita-clique na linha para exibir o menu.

  6. Clique em Aplicar o recurso.

    O selecionador de recurso é exibida.

  7. Clique o Local para baixo para expandir a seção.

    O pincel com a myBrush chave é exibida.

    Seletor de recursos

  8. Clique na myBrush chave para aplicar o recurso.

    O plano de fundo do botão é alterado para verde.

  9. Clique fora do selecionador de recurso para fechar o seletor.

  10. Press F5 to run the application.

    The background of the button is green.

Consulte também

Tarefas

Como: Get and Set Application-Scope Resources

Referência

Resources

FindResource

Conceitos

Visão geral sobre Recursos

Recursos e código

Outros recursos

Usando recursos

XAML e código no criador de WPF