Pro Silverlight 3 in C# - XAML Resources
1. The Resources Collection
<UserControl x:Class="EightBall.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <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>
<Grid x:Name="grid1" Background="{StaticResource BackgroundBrush}">
2.The Hierarchy of Resources
<UserControl x:Class="Resources.ResourceHierarchy"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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
<Application xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 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>
4.Accessing Resources in Code
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;
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow); this.Resources["ButtonFace"] = brush;
5. Organizing Resources with Resource Dictionaries
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <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>
<Application xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ElementBrushes.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="BasicBrushes.xaml" /> <ResourceDictionary Source="ButtonBrushes.xaml" /> </ResourceDictionary.MergedDictionaries> <LinearGradientBrush x:Key="GraphicalBrush1" ... ></LinearGradientBrush> <LinearGradientBrush x:Key="GraphicalBrush2" ... ></LinearGradientBrush> </ResourceDictionary> </Application.Resources>
Book Mark:93
                
            
        
                    
                
浙公网安备 33010602011771号