uwp
UWPのリソース(StaticResource / ThemeResource)とResourceDictionary
サーチ…
前書き
新しいWindows 10アプリケーションには、XAMLコード内またはコード内のリソースを参照する方法が多数あります。まず、アクセス可能な場所にリソースを宣言する必要があります。簡単な方法は、現在のページで、 ResourceDictionaryをコンテキストで宣言することです。
1.リソースディクショナリ
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.グローバルリソース
リソースディクショナリは宣言されたコンテキスト内でのみアクセス可能です。したがって、あるページコンテキストで宣言されているリソースを別のページから参照する場合、リソース辞書は見つかりません。したがって、グローバルリソースが、フレームワークに付属するように定義される必要がある場合は、App.xamlで行います
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>
こうすることで、私たちのアプリケーション内のColorRedカラーリソースにアクセスすることができます。しかし、私たちはすべてのアプリケーションのリソースをその小さなファイルに感染させたくありません!だから私たちはMergedDictionariesをやる
3.結合された辞書
ほとんどの場合、物事は少し複雑で、スケーラビリティをサポートするためには物事を分割する必要があります。そこで、さまざまなリソースディクショナリ、つまりUIコントロールのテーマ、テキストのリソースなどのリソースを含むさまざまなファイルを定義し、それらをすべてApp.xamlファイルにまとめます。
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>
アセットフォルダ[Add - > New Item]を右クリックして新しい辞書ファイルを作成することができます。
4.リソースへのアクセス
{ThemeResource ResourceKey}または{StaticResource ResourceKey}を使用するXAMLコードから、宣言されたリソースにアクセスする必要があります{StaticResource ResourceKey}
to be continued later.
