wpf-资源相关

每个文件都有一个 build action 用来控制资源文件的使用

msdn

  • Resource Files: Data files that are compiled into either an executable or library WPF assembly.
  • Content Files: Standalone data files that have an explicit association with an executable WPF assembly.
  • Site of Origin Files: Standalone data files that have no association with an executable WPF assembly.

项目中新建文件夹 Images,资源管理器中把图片拷贝进去,项目中图片属性设置 build action 为 Resource do not copy 就可以引用了。

<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button ToolTip="打开测量界面" Name="tb_measurement" Click="tb_measurement_Click">
<StackPanel Orientation="Vertical">
<Image Source="/MRLGauger;component/Images/measure_40.png" />
<TextBlock Margin="0,2,0,0" HorizontalAlignment="Center">测量</TextBlock>
</StackPanel>
</Button>
<Button ToolTip="打开编程界面" Name="tb_program" Click="tb_program_Click">
<StackPanel Orientation="Vertical">
<Image Source="/MRLGauger;component/Images/measure_40.png" />
<TextBlock Margin="0,2,0,0" HorizontalAlignment="Center">编程</TextBlock>
</StackPanel>
</Button>
<Button Command="Save" Content="Save" />
</ToolBar>
</ToolBarTray>

Window.Resources

how-to-use-a-bitmapimage

<Image Width="200">
<Image.Source>
<!-- To save significant application memory, set the DecodePixelWidth or
DecodePixelHeight of the BitmapImage value of the image source to the desired
height and width of the rendered image. If you don't do this, the application will
cache the image as though it were rendered as its normal size rather then just
the size that is displayed. -->
<!-- Note: In order to preserve aspect ratio, only set either DecodePixelWidth
or DecodePixelHeight but not both. -->
<BitmapImage DecodePixelWidth="200"
UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg" />
</Image.Source>
</Image>

image vs BitmapImage

Image is a skinnable class that wraps BitmapImage. As such BitmapImage is more lightweight, but Image has more features. So the question when to use which boils down to this:
if the features of BitmapImage suffice, use that; otherwise use Image. Especially in mobile environments try to favor the use of BitmapImage.

Now what features does Image add exactly?

Image extends SkinnableComponent: this means you can assign your images a skin with a border or a dropshadow or whatever you like consistently throughout your application.
It also provides a progress indicator which can be displayed while the image is loading.
One more feature is the possibility to queue the loading of multiple images.

全局字体和尺寸

<Application.Resources>
<Style TargetType="{x:Type TextElement }">
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="30"/>
</Style>
<Style TargetType="{x:Type TextBlock }">
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="30"/>
</Style>
</Application.Resources>