"Consistency in a user interface is an important trait; there are many facets of consistency,  
one of which is the consistent look and feel of controls. For example, all buttons should  
look roughly the same – similar colors, the same margins, and so on."

UI的风格一致性是应用程序应当关注的重要特性。

“Styles provide a convenient way to group a set of properties (and triggers) under a single
object, and then selectively (or automatically as we'll see later) apply it to elements.”

1.Creating and using styles

用一个Demo,来总结Style。

MainWindow.xaml如下:

<Window x:Class="CreatingAndUsingStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Styled Calculatot" Height="269" Width="180" ResizeMode="CanMinimize">
    <Window.Resources>
        <Style TargetType="Button" x:Key="numericStyle">
            <Setter Property="FontSize" Value="20" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="Padding" Value="6" />
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="Blue"/>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Button" x:Key="operatorStyle" BasedOn="{StaticResource numericStyle}">
            <Setter Property="FontWeight" Value="ExtraBold" />
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="Red" />
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
        <TextBox Background="Cyan" IsReadOnly="True"  Grid.ColumnSpan="4"/>
        <Button Content="7" Grid.Row="1" Style="{StaticResource numericStyle}"/>
        <Button Content="8" Grid.Row="1" Grid.Column="1" Style="{StaticResource numericStyle}"/>
        <Button Content="9" Grid.Row="1" Grid.Column="2" Style="{StaticResource numericStyle}"/>
        <Button Content="4" Grid.Row="2" Style="{StaticResource numericStyle}"/>
        <Button Content="5" Grid.Row="2" Grid.Column="1" Style="{StaticResource numericStyle}"/>
        <Button Content="6" Grid.Row="2" Grid.Column="2" Style="{StaticResource numericStyle}"/>
        <Button Content="1" Grid.Row="3" Style="{StaticResource numericStyle}"/>
        <Button Content="2" Grid.Row="3" Grid.Column="1" Style="{StaticResource numericStyle}"/>
        <Button Content="3" Grid.Row="3" Grid.Column="2" Style="{StaticResource numericStyle}"/>
        <Button Content="0" Grid.Row="4" Style="{StaticResource numericStyle}"/>
        <Button Content="=" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource operatorStyle}">
            <Button.Effect>
                <DropShadowEffect Color="Green" />
            </Button.Effect>
        </Button>
        <Button Content="+" Grid.Row="4" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
        <Button Content="-" Grid.Row="3" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
        <Button Content="X" Grid.Row="2" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
        <Button Content="/" Grid.Row="1" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
    </Grid>
</Window>

效果如下:

Point of Interest:


1.“A  Style is a container for a bunch of  Setter objects (and triggers, as we'll see later). Each
setter indicates which property should have which value; the property must be a dependency
property. The  FrameworkElement class exposes a Style property that can be set to such a
Style object. Styles are always built as resources.”

Setter的Property必须是依赖属性;

FrameworkStyle暴露了Style这个属性;

Style一般放在Resources中.


2."The  TargetType property of a  Style is typically set, which makes the  Style applicable to
that particular type (this can be any type, even a type of a custom control) and any derived
types."

TargetType="Button" 指定Style应用的类型。

一般,我们使用Style的时候,我们都会设置这个(In practice ,TargetType is always specified.),想想为什么?


3. x:Key="numericStyle" 可以根据需要进行有或无。

a.当其有的时候,我们需要对需要 Style="{StaticResource numericStyle}" .

b.其不设置的时候,默认所有TargetType使用此Style。  见2.


4. BasedOn="{StaticResource numericStyle}" 这是Style的继承。

Style继承时,可以修改BaseOn Style的Setter。如operatorStyle的          部分所示。

Style继承很好用,但是要注意:基Style的修改会影响子style.


5.“An element that uses a style can change a property that is set explicitly by a Style (a local
value), and this is stronger than a Style property setter.”

对于应用了Style的element,我们可以设置其属性值,且这个优先级更高(覆盖Style设置)。如"="Button的         部分所示。

  2.Applying a style automatically

 我们移除 x:Key="numericStyle"。让所有Button使用这个style.

MainWindow.xaml修改如下:

<Window x:Class="CreatingAndUsingStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Styled Calculatot" Height="269" Width="180" ResizeMode="CanMinimize">
    <Window.Resources>
        <Style  TargetType="Button">
            <Setter Property="FontSize" Value="20" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="Padding" Value="6" />
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="Blue"/>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Button" x:Key="operatorStyle" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="FontWeight" Value="ExtraBold" />
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="Red" />
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
        <TextBox Background="Cyan" IsReadOnly="True"  Grid.ColumnSpan="4"/>
        <Button Content="7" Grid.Row="1" />
        <Button Content="8" Grid.Row="1" Grid.Column="1" />
        <Button Content="9" Grid.Row="1" Grid.Column="2" />
        <Button Content="4" Grid.Row="2" />
        <Button Content="5" Grid.Row="2" Grid.Column="1" />
        <Button Content="6" Grid.Row="2" Grid.Column="2" />
        <Button Content="1" Grid.Row="3" />
        <Button Content="2" Grid.Row="3" Grid.Column="1" />
        <Button Content="3" Grid.Row="3" Grid.Column="2" />
        <Button Content="0" Grid.Row="4" Style="{x:Null}"/>
        <Button Content="=" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource operatorStyle}">
            <Button.Effect>
                <DropShadowEffect Color="Green" />
            </Button.Effect>
        </Button>
        <Button Content="+" Grid.Row="4" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
        <Button Content="-" Grid.Row="3" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
        <Button Content="X" Grid.Row="2" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
        <Button Content="/" Grid.Row="1" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
    </Grid>
</Window>

效果如下:

Point of Interest

1.“Automatic styles are created as resources without a key. This does not mean there is no key,
because it's still a dictionary. The key becomes the actual type to apply the style to defined by
the  TargetType property.”

不为Style指定x:key,这个Style将应用于所有的x:Tyle element;

没有指定x:key,不代表其没有key,其key由x:Tyle定义,就本例而言是{x:Type Button}},从Style继承中(xaml中绿色高亮代码),可见一斑。


2.如果,这样的Style被Set在Window的Resource,则影响这个Window上的所有x:Type类型的element;

如果被set在application's resources,则影响所有window。


3.“The Style is applied to all elements of the target type, but not derived types.
Any element that does not set its style explicitly obtains that style automatically. If an element
wishes to revert to its default style, it can set its Style property to null ( {x:Null}  in XAML)
or set its  Style to another named style.”

如果,我们希望某个element不应用这个style,我们可以设置它的Style属性为其他style;

如果想让它保持默认的样子,我们可以设置其Style="{x:Null}"如Button “0”。

 3.动态更换Style

“Automatic styles are a great way to create a consistent look and feel without burdening the
developer (or the designer) with the details of the various visual properties. It can also be
used as a quick way to implement skinning."

在Skin文件夹中添加两个Resource Dictionary,如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBox">
        <Setter Property="Background" Value="LightBlue" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="BorderBrush" Value="DarkBlue" />
        <Style.Triggers>
            <Trigger Property="IsReadOnly" Value="True">
                <Setter Property="Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="Button">
        <Setter Property="Background" Value="Cyan" />
    </Style>
</ResourceDictionary>
View Code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBox">
        <Setter Property="Background" Value="Violet" />
        <Setter Property="Foreground" Value="DarkViolet" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="BorderBrush" Value="LightPink" />

        <Style.Triggers>
            <Trigger Property="IsReadOnly" Value="True">
                <Setter Property="Background" Value="Pink" />
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="Button">
        <Setter Property="Background" Value="Red" />
    </Style>
</ResourceDictionary>
View Code

并设置其Property为:Content/Aways Copy.

“These settings prevent the default compilation to BAML and also provide the flexibility to
change the skins without recompilation. ”

请参考DebugLZQ前面的博文:WPF整理-二进制资源和内容

MainWindow.xaml,MainWindow.xaml.cs如下:

<Window x:Class="WPFStyleTriggerAndControlTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="303.719" Width="530.785">
    <Grid Margin="0,0,2,0">
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,35,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="156,35,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="293,35,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" RenderTransformOrigin="1.577,0.303"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="433,35,0,0" VerticalAlignment="Top" Width="75"/>
        <GroupBox FontSize="28" Header="Select Skin"  Margin="75,102,62,0" VerticalAlignment="Top" Height="158">
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                <RadioButton Content="Normal" Checked="AllRadioButtonChecked"/>
                <RadioButton Content="Blue Skin" Checked="AllRadioButtonChecked"/>
                <RadioButton Content="Red Skin" Checked="AllRadioButtonChecked"/>
            </StackPanel>            
        </GroupBox>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFStyleTriggerAndControlTemplate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void AllRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            switch (((RadioButton)sender).Content.ToString())
            { 
                case "Blue Skin":
                    ChangeSkin("Skins/BlueSkin.xaml");
                    break;
                case "Red Skin":
                    ChangeSkin("Skins/RedSkin.xaml");
                    break;
                case "Normal":
                    Application.Current.Resources.MergedDictionaries.Clear();
                    break;
            }
        }

        void ChangeSkin(string skinRelativeUri)
        {
            var si = Application.GetContentStream(new Uri(skinRelativeUri, UriKind.Relative));
            var rd = (ResourceDictionary)XamlReader.Load(si.Stream);
            Application.Current.Resources.MergedDictionaries.Clear();
            Application.Current.Resources.MergedDictionaries.Add(rd);
        }

    }
}

运行效果如下:

 4.Other places to set styles

"The example shows styles applied to the FrameworkElement.Style property, but this is
not the only property that can accept a Style.

Other examples include the following:"

  "FrameworkElement.FocusVisualStyle property: Accepts a Style that affects
  the way the focus indicator is rendered when that element has the keyboard focus."

  "ItemsControl.ItemContainerStyle property: Accepts a Style that affects the
  container element of each data item (for example, ListBoxItem for a ListBox)."

  "DataGrid.CellStyle: Accepts a Style that affects the way a cell is rendered.
  Similar properties exposed by the DataGrid include ColumnHeaderStyle,
  DragIndicatorStyle, DropLocationIndicatorStyle, RowHeaderStyle,
  and RowStyle."

 5.Style和ControlTemplate

ControlTemplate一般有如下2种使用方法

方法1:

<Window.Resources>
        <ControlTemplate x:Key="AnimatedExpanderButtonTemp" TargetType="{x:Type ToggleButton}">

Template="{StaticResource RevealExpanderTemp}"

--------------

方法2:

 <Style  TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid>

-----------------------------

关于DataTemplate请参考DebugLZQ前面的博文:WPF整理-使用 DataTemplate

6.Style & Trigger & ControlTemplate一个非常常见的例子

在实际应用中,这3个一般会同时用到。我们用一个自定义的最大化/最小化/关闭按钮等,如下:

一般有2种实现方法,第一种方法如下:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="362.767" Width="524.468" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
    <Window.Resources>
        <Style TargetType="Button" x:Key="btnStyle1">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="Button">
                        <Grid x:Name="grid1" Background="Transparent">
                            <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="grid1" Property="Grid.Background" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>                    
                </Setter.Value>
            </Setter>            
        </Style>

        <ControlTemplate  TargetType="Button" x:Key="btnTemplate1">
            <Grid x:Name="grid1" Background="Transparent" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="grid1" Property="Grid.Background" Value="LightBlue"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid >
        <Grid.Background>
            <ImageBrush ImageSource="Button_Icons/background_mainwnd.png"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="RightToLeft">
            <Button Style="{StaticResource btnStyle1}" Content="X" Foreground="White" FontSize="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="23" Height="23" Margin="5,0,5,0"/>
            <Button Template="{StaticResource btnTemplate1}" Content="口" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top"   Width="23" Height="23" Margin="5,0,5,0"/>
            <Button Template="{StaticResource btnTemplate1}" Content="一" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top"   Width="23" Height="23" Margin="5,0,5,0"/>

        </StackPanel>
    </Grid>
</Window>

效果如下:

这种方法的缺点是,我们设置的Content比较粗糙~

第二中方法,借助相关的图标的png图片及相应的效果图片,来设置Content,原理同上

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="362.767" Width="524.468" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
    <Window.Resources>
        <Style TargetType="Button" x:Key="btnStyle1">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="Button">
                        <Grid  Background="Transparent">
                            <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="cp1" Property="Content">
                                    <Setter.Value>
                                        <Image Source="Button_Icons/xm.png"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="Button" x:Key="btnStyle2">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="Button">
                        <Grid  Background="Transparent">
                            <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="cp1" Property="Content">
                                    <Setter.Value>
                                        <Image Source="Button_Icons/mxm.png"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="Button" x:Key="btnStyle3">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="Button">
                        <Grid  Background="Transparent">
                            <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="cp1" Property="Content">
                                    <Setter.Value>
                                        <Image Source="Button_Icons/mim.png"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <Grid >
        <Grid.Background>
            <ImageBrush ImageSource="Button_Icons/background_mainwnd.png"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="RightToLeft">
            <Button Style="{StaticResource btnStyle1}"  Foreground="White" FontSize="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="30" Height="30" >
                <Button.Content>
                    <Image Source="Button_Icons/xn.png" />
                </Button.Content>
            </Button>
            <Button Style="{StaticResource btnStyle2}"  Foreground="White" FontSize="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="30" Height="30" >
                <Button.Content>
                    <Image Source="Button_Icons/mxn.png" />
                </Button.Content>
            </Button>
            <Button Style="{StaticResource btnStyle3}"  Foreground="White" FontSize="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="30" Height="30" >
                <Button.Content>
                    <Image Source="Button_Icons/min.png" />
                </Button.Content>
            </Button>

        </StackPanel>
    </Grid>
</Window>

效果如下:

Style的另一个非常常用的场景是:视图切换。参考DebugLZQ前面总结DataTemplate的博文中的一个例子:6.DataTemplate的一个非常常见的应用

请关注后续整理~

Wish it helps~

posted @ 2013-07-11 14:22 DebugLZQ 阅读(9172) 评论(6) 推荐(8) 编辑
摘要: 在.NET中,我们使用try-catch-finally来处理异常。但,当一个Exception抛出,抛出Exception的代码又没有被try包围时,程序就崩溃了。这些异常往往是你没有注意到的。在WPF中,提供了一种处理这些个异常的方式。举例来说明。1.先抛出个异常,不用try包围它。在MainW... 阅读全文
posted @ 2013-07-08 09:01 DebugLZQ 阅读(11361) 评论(8) 推荐(9) 编辑
摘要: 有时我们不希望我们的WPF应用程序可以同时运行有多个实例,当我们试图运行第二个实例的时候,已经运行的实例也应该弹出来。我们可以用Mutex来实现打开App.xaml.cs,在App类中添加如下内容 public partial class App : Application { [DllImport("user32", CharSet = CharSet.Unicode)] static extern IntPtr FindWindow(string cls, string win); [DllImport("user32")] ... 阅读全文
posted @ 2013-07-04 22:09 DebugLZQ 阅读(3970) 评论(6) 推荐(5) 编辑
摘要: I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. 阅读全文
posted @ 2013-07-03 21:48 DebugLZQ 阅读(769) 评论(2) 推荐(0) 编辑
摘要: “Logical resources may be of various types, such as brushes, geometries, styles, and templates.Placing all those resources in a single file such as App.xaml hinders maintainability. A betterapproach would be to separate resources of different types (or based on some other criteria) totheir own files 阅读全文
posted @ 2013-07-02 22:03 DebugLZQ 阅读(14954) 评论(8) 推荐(3) 编辑
摘要: “Sometimes binary resources are defined in one assembly (typically a class library), but areneeded in another assembly (another class library or an executable). WPF provides a uniformand consistent way of accessing these resources using the pack URI scheme.”假如我们定义一个WPF User Control Library,并在其中添加一张图 阅读全文
posted @ 2013-07-02 13:15 DebugLZQ 阅读(2868) 评论(0) 推荐(1) 编辑
摘要: WPF中的Binary Resource(二进制资源)是相对于前面所说的Logical resource(逻辑资源)而说的,一般指Image、XML文件等。注意:这里说的是Resource"资源",和Content"内容"是不同的。1.Content VS Resource一般我们向工程中添加一个二进制的资源,如图片。我们设置它的属性,设置成资源和内容是不同的!简单的说下两者的区别:“When the Build Action is set to Content (as in the jellyfish example), the resource is 阅读全文
posted @ 2013-07-02 07:30 DebugLZQ 阅读(2019) 评论(2) 推荐(2) 编辑
摘要: 这篇博文分享下保龄球计分算法。计分规则描述如下:A game of tenpins bowling lasts ten frames, in each of which the bowler makes one or two attempts to knock down ten pins arran... 阅读全文
posted @ 2013-07-01 20:24 DebugLZQ 阅读(3298) 评论(1) 推荐(0) 编辑
摘要: “Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows Control Panel Personalization applet (or the older Display Settings in Windows XP),such as Window caption, Desktop color, and Selection color. Furthermore, an applicationmay want to react dynamica 阅读全文
posted @ 2013-06-29 23:12 DebugLZQ 阅读(3426) 评论(0) 推荐(1) 编辑
摘要: I haven't seen much information online comparing the similarities and differences between the Nunit and MSTest Frameworks. Here I will define the similarities and some of the differences. MSTest AttributeNUnit AttributePurpose[TestMethod][Test]Indentifies of an individual unit test[TestClass][Te 阅读全文
posted @ 2013-06-29 01:41 DebugLZQ 阅读(808) 评论(1) 推荐(0) 编辑
点击右上角即可分享
微信分享提示