Pro Silverlight 3 in C# - XAML Resources

1. The Resources Collection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<UserControl x:Class="EightBall.MainPage"
<UserControl.Resources>
<LinearGradientBrush x:Key="BackgroundBrush">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.00" Color="Yellow" />
<GradientStop Offset="0.50" Color="White" />
<GradientStop Offset="1.00" Color="Purple" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</UserControl.Resources>
...
</UserControl>

 

1
<Grid x:Name="grid1" Background="{StaticResource BackgroundBrush}">

 

2.The Hierarchy of Resources

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<UserControl x:Class="Resources.ResourceHierarchy"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel><StackPanel.Resources>
<LinearGradientBrush x:Key="ButtonFace">
<GradientStop Offset="0.00" Color="Yellow" />
<GradientStop Offset="0.50" Color="White" />
<GradientStop Offset="1.00" Color="Purple" />
</LinearGradientBrush>
</StackPanel.Resources>
<Button Content="Click Me First" Margin="5"
Background="{StaticResource ButtonFace}"></Button>
<Button Content="Click Me Next" Margin="5"
Background="{StaticResource ButtonFace}"></Button>
</StackPanel>
</Grid>
</UserControl>

3. Application.Resources

1
2
3
4
5
6
7
8
9
10
11
12
x:Class="SilverlightApplication1.App">
<Application.Resources>
<LinearGradientBrush x:Key="ButtonFace">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.00" Color="Yellow" /><GradientStop Offset="0.50" Color="White" />
<GradientStop Offset="1.00" Color="Purple" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Application.Resources>
</Application>
1
  
4.Accessing Resources in Code
1
2
3
4
5
LinearGradientBrush brush = (LinearGradientBrush)this.Resources["ButtonFace"];
// Swap the color order.
Color color = brush.GradientStops[0].Color;
brush.GradientStops[0].Color = brush.GradientStops[2].Color;
brush.GradientStops[2].Color = color;
1
  
1
2
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
this.Resources["ButtonFace"] = brush;

 

5. Organizing Resources with Resource Dictionaries

1
2
3
4
5
6
7
8
9
10
<ResourceDictionary
<LinearGradientBrush x:Key="ButtonFace">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.00" Color="Yellow" /><GradientStop Offset="0.50" Color="White" />
<GradientStop Offset="1.00" Color="Purple" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</ResourceDictionary>

 

1
2
3
4
5
6
7
8
9
10
11
x:Class="SilverlightApplication1.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary <strong>Source="ElementBrushes.xaml"</strong> />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

 

1
2
3
4
5
6
7
8
9
10
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary <strong>Source="BasicBrushes.xaml"</strong> />
<ResourceDictionary <strong>Source="ButtonBrushes.xaml"</strong> />
</ResourceDictionary.MergedDictionaries>
<LinearGradientBrush<strong> x:Key="GraphicalBrush1"</strong> ... ></LinearGradientBrush>
<LinearGradientBrush <strong>x:Key="GraphicalBrush2"</strong> ... ></LinearGradientBrush>
</ResourceDictionary>
</Application.Resources>

 

Book Mark:93

编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 2025成都.NET开发者Connect圆满结束
点击右上角即可分享
微信分享提示