[WPF] Resource WPF 2012. 3. 19. 17:05
리소스 내에 우리들은 일반적으로 왠만한 이미지 등과 같은 파일들을 저장하고 이용한다. 다음은 그것을 꺼내어 쓰는 내용이다.

 //System.Drawing.Icon icon = WpfApplication16.Properties.Resources.icon
            System.Drawing.Bitmap bmp = WpfApplication16.Properties.Resources.Penguins;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            ms.Seek(0, System.IO.SeekOrigin.Begin);
            BitmapFrame frame = BitmapFrame.Create(ms);
            image1.Source = frame;

이미지를 불러서 처리하는 내용을 보면 상단에 아이콘 처리 역시 동일하게 할 수 있음을 볼 수 있다.

하지만 아이콘이 차이가 있다면 WPF에서는 Windows.Media.ImageSource 형식을 이용한다는 것이다.

그래서 MemoryStream을 이용하여 메모리 상에 올려 놓고 이미지 데이터로 인코딩하여 연결 한 것이다.

사운드 재생의 경우 SoundPlayer 클래스를 쓰면 된다. 이 역시 Media.SoundPlayer 아래 있다.
[WPF] Resource WPF 2012. 3. 14. 17:42

리소스는 런타임 당시 이용되어 쓰이는 데이터를 말하는 것이다. 아래는 리소스를 이용하여 컨트롤 변경 하는 예제이다.

앞서 포스팅 한 글에도 Window.Resource 를 정의하여 이용 했는데 그곳에 정의 된 내용을 쓰는 것과 동일한 내용이다.

<Window.Resources>
        <Style x:Key="LabelStyle1" TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FFFF0000"/>
            <Setter Property="Background" Value="#00FF0000"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
        <Style x:Key="LabelStyle2" TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FF002CFF"/>
            <Setter Property="Background" Value="#00FF0000"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="FontFamily" Value="Gulim"/>
            <Setter Property="FontSize" Value="20"/>
        </Style>

    </Window.Resources>

위와 같이 리소스를 정의 하고,

<Label Height="28" Margin="14,33,12,0" Name="label1" VerticalAlignment="Top" Padding="0"
               Style="{DynamicResource null}">Test</Label>
        <Button Height="23" HorizontalAlignment="Left" Margin="14,0,0,46" Name="btn1" VerticalAlignment="Bottom"  Width="75" Click="btn1_Click">Button Test</Button>
        <Button Height="23" Margin="105,0,98,46" Name="btn2" VerticalAlignment="Bottom"  Width="75" Click="btn2_Click">Resource1</Button>
        <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,46" Name="btn3" VerticalAlignment="Bottom"  Width="75" Click="btn3_Click">Resource2</Button>

와 같이 컨트롤을 올린 후 이벤트에 style을 FindResource를 이용하여 적용 시키면 된다. 명시적으로 캐스팅 해 주는 것을 잊지 말자.