xmlns:s ="clr-namespace:System;assembly=mscorlib"
xml 네임스페이스를 접두어 s로 정의하는데, 이것은 resources 컬렉션 내에서 double 구조체를 참조할 수 있다.
StaticResource라는 클래스는 사실 존재하지 않는다. 다만 MarkupExtension에서 상속받은 StaticResourceExtension 클래스가 있는데, 이것이 ResourceKey 프로퍼티를 가지고 있다. StaticResource가 마크업 확장으로 분류되는 이유는 이것이 일반적으로 C#코드에서 가증했던 방법을 XAML에서 제공하기 때문이다.
여기서 StaticResourceExtension클래스는 특정한 키로 원하는 값을 사전에서 찾아 제공하는 역활을 한다.
<Window x:Class="ResourceWPF.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s ="clr-namespace:System;assembly=mscorlib" Title="Window1" Height="300" Width="300"> <Window.Resources> <s:Double x:Key="fontsizeLarge">18.7</s:Double> <s:Double x:Key="fontsizemedium">14</s:Double> <s:Double x:Key="fontsizesmall">10</s:Double> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Content="LargSize" FontSize="{StaticResource fontsizeLarge}" Grid.Row="0"></Button> <Button Content="LargSize" FontSize="{StaticResource fontsizemedium}" Grid.Row="1"></Button> <Button Content="LargSize" Grid.Row="2"> <Button.FontSize> <StaticResource ResourceKey="fontsizesmall"></StaticResource> </Button.FontSize> </Button> </Grid> </Window>
|
한Resources컬렉션에서 모든 키는 서로 유일해야 하지만 2개의 서로 다른 Resources컬렉션에서는 동일한 키가 사용될 수 있다.
부모 stactpanel에서 brushtext라는 키를 가진 초록색의 solidColorBrush가 Resources컬렉션에 정의되어 있다. 또한 첫 번쨰 자식 StackPanel은 brushtext라는 동일한 이름을 키로 빨간색의 SolidColorBrush를 정의했다. 2개의 버튼은 그것이 Foreground프로퍼티를 brushtext키를 이용해 설정했는데, 첫 번쨰 버튼은 빨간색 문자열을 출력하고 두번쨰 brushtext라는 리소스를 가지지않은 stackpanel에 있는 버튼은 부모의 리소스를 사용해 문자열을 초록색으로 출력한다.
엘리먼트나 컨트롤을 리서스로 정의하는 것도 가능하다. 하지만 한 번만 가능하다. 리소스로 생성된 Button 객체는 하나의 객체이므로 그 Button이 패널의 자식이라면 이Button은 동일한 패널 속에서 다른 것의 자식이나 다른 패널의 자식이 될 수 없다. 또한 StaticResource 엘리먼트로 이 Button을 참조하는 순간 이 Button에 대해 어떤 것도 변경할 수 없기 때문에 리소스로 Button을 생성하는 것은 특별한 장점이 없다.
<Window x:Class="ResourceWPF.ResourceLookup" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ResourceLookup" Height="300" Width="300"> <StackPanel Name="stack1"> <StackPanel.Resources> <Button x:Key="btn" FontSize="24" Content="Resource Button"></Button>
<SolidColorBrush x:Key="brushtext" Color="Green"></SolidColorBrush> </StackPanel.Resources> <StackPanel> <StackPanel.Resources> <SolidColorBrush x:Key="brushtext" Color="Red"></SolidColorBrush> </StackPanel.Resources> <Button Margin="24" Content="Button Red" HorizontalAlignment="Center" Foreground="{StaticResource brushtext}"></Button> </StackPanel> <Button Content="Button Green" HorizontalAlignment="Center" Foreground="{StaticResourcebrushtext}"></Button> <StaticResource ResourceKey="btn"></StaticResource>
</StackPanel> </Window> |
컨트롤과 엘리먼트를 리소스로 정의하고 싶다는 것은 엘리먼트의 프로퍼티 전부가 아닌 그중에서 필요한 몇 개의 엘리먼트의 프로퍼티를 설정하기 위해 리소스의 사용을 고려하는 것일 것이다. 이를 통해 리소스가 여러 엘리먼트나 컨트롤 사이에서 공유되는 하나의 객체임이 명백해진다. 엘리먼트가 FindResource 메소드가 호출하는 데 있어서 엘리먼트의 Resources컬렉션에 특정 리소스를 찾을 뿐만 아니라 엘리먼트 트리상에 있는 엘리먼트의 조상에서도 리소스를 찾는다는 것이다.
<Application x:Class="ResourceWPF.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="ResourceLookup.xaml"> <Application.Resources>
</Application.Resources> </Application> |
이것은 애플리케이션 범위의 Resources절이 WPF 프로그래밍에 얼마나 중요한지를 알려준다. Resources절을 사용해 애플리케이션 범위의 그라디언트 브러시를 정의하여 테스트를 만들었다.
<Application x:Class="ResourceWPF.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="ResourceLookup.xaml"> <Application.Resources> <LinearGradientBrush x:Key="brushGradient" StartPoint="0,0" EndPoint="1,1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0" Color="black"></GradientStop> <GradientStop Offset="0.5" Color="Green"></GradientStop> <GradientStop Offset="1" Color="Red"></GradientStop> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Application.Resources> </Application> |
<Window x:Class="ResourceWPF.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"> <StackPanel> <TextBlock Text="Gradient text" Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"></TextBlock> <TextBlock Text="Of black green red" Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"></TextBlock> <TextBlock Text="Makes and app pretty" Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"></TextBlock> <TextBlock Text="Make an app bold" Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"></TextBlock> </StackPanel> </Window> |