Sök…


Introduktion

I de nya Windows 10-applikationerna finns det många sätt att hänvisa till en resurs i XAML-koden eller i koden bakom. Först av allt måste du förklara resurserna på någon tillgänglig plats. Det enkla sättet är att förklara en ResourceDictionary i sammanhang, låt oss säga på den aktuella sidan.

1. Resource Dictionary

Utdrag från MainPage.xaml

<Page
    x:Class="MyNewApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyNewApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <!-- Creates a resource dictionary in the page context -->
        <ResourceDictionary>
            <!-- This is a solid color brush resource
                 NOTE: Each resource inside a resource dictionary must have a key -->
            <SolidColorBrush x:Key="ColorRed">Red</SolidColorBrush>
        </ResourceDictionary>
    </Page.Resources>

    <!-- Using ThemeResource in here to access a resource already defined -->
    <Grid Background="{ThemeResource ColorRed}">

    </Grid>
</Page>

2. Globala resurser

Resursordböcker är endast tillgängliga inom det sammanhang som de deklarerades, så om vi tänkte hänvisa till resurser som deklareras i en sidas sammanhang från en annan sida kommer de inte att hittas. Så om vi behöver globala resurser för att definieras som de som följer med ramverket gör vi det i App.xaml

Utdrag från App.xaml

<Application
    x:Class="MyNewApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Dark">

    <Application.Resources>
        <ResourceDictionary>
            <SolidColorBrush x:Key="ColorRed">Red</SolidColorBrush>
        </ResourceDictionary>
    </Application.Resources>
</Application>

På det här sättet kan vi komma åt ColorRed från vilken som helst i vår app. Men vänta, vi vill inte infektera den lilla filen med alla våra apps resurser! Så vi gör MergedDictionaries

3. Sammanlagda ordböcker

Nästan vanligtvis är saker lite mer komplexa och för att stödja skalbarhet bör vi dela upp saker. Så vi kan definiera olika filer som innehåller olika resursordböcker, det vill säga resurser för UI-kontrollers teman, resurser för texter och så vidare, sedan slår vi dem samman i App.xaml-fil.

Utdrag från App.xaml

<Application
    x:Class="MyNewApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Dark">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Assets/Themes/GeneralStyles.xaml"/>
                <ResourceDictionary Source="/Assets/Themes/TemplatedControls.xaml"/>
                <ResourceDictionary Source="/Assets/Strings/Texts.xaml"/>
                <ResourceDictionary Source="/Assets/Strings/ErrorTexts.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Du kan skapa en ny ordboksfil genom att högerklicka på Asset-mappen [Lägg till -> Ny artikel]

ange bildbeskrivning här

4. Få tillgång till resurser

Vi måste nu komma åt våra deklarerade resurser, för att göra det från XAML-koden använder vi {ThemeResource ResourceKey} eller {StaticResource ResourceKey}

to be continued later.


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow