리소스 (Resource)
.NET프로그래밍/Silverlight 3.0 2009. 12. 3. 09:45 |[MainPage.xaml]
<UserControl x:Class="RiaResource.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<!--[!] 외부에서 정의된 리소스 사용하기위한 링크 -->
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="FrmMergedResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<!-- 스타일/리소스 정의 -->
<Grid.Resources>
<SolidColorBrush x:Key="myColor" Color="Red" Opacity="0.8" />
</Grid.Resources>
<!-- 스타일 적용 -->
<StackPanel>
<!--[1] 태그에 직접 정의 -->
<TextBlock Text="RedPlus" Foreground="Red" />
<!--[2] 스타일/리소스를 사용해서 색상 적용 -->
<TextBlock Text="RedColor" Foreground="{StaticResource myColor}" />
<TextBlock Text="RedColor" Foreground="{StaticResource myColor}" />
<!--[3] 외부에서 정의된 리소스 사용 -->
<TextBlock Text="BlueColor" Foreground="{StaticResource myBlue}" />
<TextBlock Text="GreenColor" Foreground="{StaticResource myGreen}" />
<!--[4] 코드비하인드에서 C#코드로 리소스를 적용 -->
<TextBlock x:Name="lblRed" Text="RedColor" />
<TextBlock x:Name="lblGreen" Text="GreenColor" />
</StackPanel>
</Grid>
</UserControl>
[MainPage.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace RiaResource
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//[1] 그리드의 리소스를 사용
this.lblRed.Foreground = (this.LayoutRoot.Resources["myColor"] as SolidColorBrush);
//[2] 외부에 정의된 리소스를 사용
this.lblGreen.Foreground = this.Resources["myGreen"] as SolidColorBrush;
}
}
}
--> '새 항목 추가'로 "Silverlight User Control"형식의 파일인 "FrmMergedResourceDictionary.xaml"파일을 추가하고 아래와 같이 코딩한다.(여기서는 "FrmMergedResourceDictionary.xaml.cs"파일은 삭제하였다. 참고로, 아래그림과 같이 '새 항목 추가'로 "Silverlight Resource Dictionary"형식의 파일을 동일하게 만들 수 있다.)
[FrmMergedResourceDictionary.xaml]
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="Blue" x:Key="myBlue" />
<SolidColorBrush Color="Green" x:Key="myGreen" />
</ResourceDictionary>
[실행결과]
'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글
템플릿(Template) : 컨트롤 모양 미리 정의 (0) | 2009.12.03 |
---|---|
스타일(Style) : 재 사용 가능한 속성 집합 (0) | 2009.12.03 |
DatePicker 컨트롤 (0) | 2009.12.02 |
Calendar 컨트롤 (0) | 2009.12.02 |
TabControl 컨트롤 (0) | 2009.12.02 |