# .NET 标准库
.NET Standard 类库 (opens new window)是平台特定的库和可移植库概念的替代。
# System
System (opens new window) 命名空间包含用于定义常用值和引用数据类型、事件和事件处理程序、接口、属性和处理异常的基本类和基类。
# 基本对象
# Object
Object (opens new window) 是所有 .NET 类的最终基类。
实例方法:
Object.GetHashCode (opens new window)()
用作默认的哈希函数。
重写
GetHashCode()(opens new window) 的派生类也必须重写Equals(Object)(opens new window),以确保两个被视为相等的对象具有相同的哈希代码。Equals(Object)方法用于确定两个对象是否相等,基于对象的引用相等性进行比较。GetHashCode()方法返回一个哈希码,用于在哈希表等数据结构中快速查找对象。
如果两个对象根据
Equals(Object)方法被认为是相等的,那么它们的哈希码也必须相同。这是哈希表工作的基础,否则,Hashtable类型可能无法正常工作。Object.Equals(Object) (opens new window)
确定指定对象是否等于当前对象。
Object类中的Equals方法区分两个对象的做法是比较地址值 (opens new window)。重写
Equals()方法的实现,也应当同时改写GetHashCode()方法的实现。否则GetHashCode()方法依然返回的是依据Object类中的地址值得到的哈希值。例如,
String类中,Equals()方法经过重写 (opens new window),String对象在调用Equals()方法比较另一个对象时,除了认定相同地址值的两个对象相等以外,还认定每个字符都相等的两个String对象也相等,即使这两个String对象的地址值不同。同时GetHashCode()(opens new window) 方法并不是基于对象的地址引用,而是基于字符串的内容计算的哈希码。
# Random
Random (opens new window) 是伪随机数生成器。
实例方法:
Random.Next(Int32, Int32) (opens new window)
返回在指定范围内的任意整数。
Random dice = new Random(); int roll = dice.Next(1, 7); // 该随机数大于等于 1,小于 7。
# String
String (opens new window) 将文本表示为 UTF-16 代码单元的序列。
静态方法:
String.IsNullOrWhiteSpace(String) (opens new window)
指示指定的字符串是
null、空还是仅由空白字符组成。
# System.Windows.Controls
System.Windows.Controls (opens new window) 命名空间提供一些类以创建称为控件的元素,从而使用户可使用这些元素与应用程序进行交互。
# 布局
# Grid
Grid (opens new window) 定义由列和行组成的灵活的网格区域。
属性:
Grid.RowDefinitions (opens new window)
获取在
Grid的实例上定义的RowDefinitionCollection(opens new window)。<Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> </Grid>Grid.ColumnDefinitions (opens new window)
获取在
Grid的实例上定义的ColumnDefinitionCollection(opens new window)。<Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> </Grid>
# StackPanel
StackPanel (opens new window) 将子元素排列成水平或垂直的一行。
<StackPanel Grid.Row="1" Grid.Column="1" Margin="5,0,0,0">
</StackPanel>
# 控件
# Control
Control (opens new window) 表示 UI 元素的基类,这些元素使用 ControlTemplate (opens new window) 来定义其外观。
属性:
Control.Template (opens new window)
获取或设置控件模板。
<Button Background="Green" Content="Add Name" Foreground="White"> <Button.Template> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1" CornerRadius="2"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Button.Template> </Button>
# ControlTemplate
ControlTemplate (opens new window) 指定 Control 的可在其多个实例之间共享的可视结构和行为方面。
属性:
ControlTemplate.Triggers (opens new window)
获取根据指定条件应用属性更改或执行操作的
TriggerBase(opens new window) 对象的集合。<ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="Black" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="border" Property="Background" Value="WhiteSmoke" /> </Trigger> </ControlTemplate.Triggers>
# Label
Label (opens new window) 表示控件的文本标签。
<Label Grid.Column="1">Names</Label>
# TextBox
TextBox (opens new window) 表示一个控件,该控件可用于显示或编辑无格式文本。
<TextBox x:Name="txtName" />
实例属性:
TextBox.Text (opens new window)
获取或设置文本框的文本内容。
实例方法:
TextBox.Clear (opens new window)()
清除文本框中的所有内容。
# Button
Button (opens new window) 表示 Windows 按钮控件,该按钮对 Click (opens new window) 事件做出反应。
<Button x:Name="btnAdd" Margin="0,5,0,0" Click="ButtonAddName_Click">Add Name</Button>
# ListBox
ListBox (opens new window) 包含可选项列表。
<!-- 列表框将位于第 1 行和第 0 列,并将此控件命名为 lstNames -->
<ListBox Grid.Row="1" x:Name="lstNames" />
实例属性:
ListBox.Items (opens new window)
获取用于生成
ItemsControl(opens new window) 的内容的集合。
# DataGrid
DataGrid (opens new window) 表示用于在可自定义的网格中显示数据的控件。
<DataGrid
x:Name="ReviewDataGrid"
AutoGenerateColumns="False"
ClipboardCopyMode="ExcludeHeader"
IsReadOnly="True"
ItemsSource="{Binding SalesOrderReviews}"
SelectionMode="Extended"
SelectionUnit="CellOrRowHeader">
<DataGrid.Columns>
<DataGridTextColumn
Width="85"
Binding="{Binding OrderDate, StringFormat={}{0:yyyy-MM-dd}}"
Header="订单日期" />
<DataGridTextColumn
Width="200"
Binding="{Binding CustomerName}"
Header="客户名称" />
<DataGridTextColumn
Width="85"
Binding="{Binding SalesOrderNumber}"
Header="销售单号" />
<DataGridTextColumn
Width="40"
Binding="{Binding SalesItemNumber}"
Header="项号" />
<DataGridTextColumn Binding="{Binding PartNumber}" Header="型号" />
<DataGridTextColumn
Width="60"
Binding="{Binding SalesPerson}"
Header="销售" />
<DataGridTextColumn
Width="60"
Binding="{Binding OrderQuantity, StringFormat={}{0:F2}}"
Header="订单数量" />
<DataGridTemplateColumn Width="110" Header="关联采购单">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding LinkedPurchaseOrders}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding PurchaseOrderNumber}" />
<Run Text=":" />
<Run Text="{Binding PurchaseItemNumber}" />
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn
Width="60"
Binding="{Binding SalesUnitPrice, StringFormat={}{0:F2}}"
Header="销售单价" />
<DataGridTextColumn
Width="60"
Binding="{Binding PurchaseUnitPrice, StringFormat={}{0:F2}}"
Header="采购单价" />
<DataGridTextColumn
Width="60"
Binding="{Binding SalesSubtotal}"
Header="销售小计" />
<DataGridTextColumn
Width="60"
Binding="{Binding PurchaseSubtotal}"
Header="采购小计" />
<DataGridTextColumn
Width="60"
Binding="{Binding GrossMargin, StringFormat={}{0:P2}}"
Header="毛利率" />
</DataGrid.Columns>
</DataGrid>
# System.Windows
System.Windows (opens new window) 命名空间提供了一些重要的 WPF 基元素类、各种支持 WPF 属性系统和事件逻辑的类以及由 WPF 核心和框架更加广泛使用的其他类型。
# 资源
# Application
Application (opens new window) 封装 WPF 应用程序。
属性:
Application.Resources (opens new window)
获取或设置应用程序范围资源的集合。
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/WPF_APP;component/Dictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
# ResourceDictionary
ResourceDictionary (opens new window) 提供一个哈希表实现,其中包含组件所使用的 WPF 资源以及 WPF 应用程序的其他元素。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
属性:
ResourceDictionary.MergedDictionaries (opens new window)
获取
ResourceDictionary字典的集合,这些字典构成了合并字典中的各种资源字典。<ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/WPF_APP;component/Dictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
# 元素
# FrameworkElement
FrameworkElement (opens new window) 为 WPF 元素提供一组 WPF 框架级属性、事件和方法。
实例属性:
FrameworkElement.Style (opens new window)
获取或设置此元素呈现时所使用的样式。
<Button x:Name="btnAdd" Style="{StaticResource Primary}" Content="Add Name" />FrameworkElement.Resources (opens new window)
获取或设置本地定义的资源字典。
<Window.Resources> <Style TargetType="Button"> <Setter Property="Background" Value="Red" /> </Style> </Window.Resources>
# Style
Style (opens new window) 在类型的不同实例之间共享属性、资源和事件处理程序。
<Style x:Key="Primary" TargetType="Button">
<Setter Property="Background" Value="Red" />
</Style>
实例属性:
Style.BasedOn (opens new window)
获取或设置一个作为当前样式的基准的已定义样式。
<Style x:Key="Primary" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="FontSize" Value="20" /> </Style>
# System.Collections.Generic
System.Collections.Generic (opens new window) 命名空间包含用于定义泛型集合的接口和类,可允许用户创建强类型集合,以提供比非泛型强类型集合更好的类型安全性和性能。
# 集合
# List<T>
List<T> (opens new window) 表示可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法。
实例方法:
List<T>.FindIndex(Predicate<T>) (opens new window)
搜索与指定条件函数所定义的条件相匹配的元素,并返回整个
List<T>中第一个匹配元素的从零开始的索引。List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int index = numbers.FindIndex(x => x == 3); Console.WriteLine("Index of 3: " + index);
# System.Linq
System.Linq (opens new window) 命名空间提供支持某些查询的类和接口,这些查询使用语言集成查询 LINQ。
# Enumerable
Enumerable (opens new window) 提供一组用于查询实现 IEnumerable<T> 的对象的静态方法。
静态方法:
Enumerable.Where (opens new window)<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>)
基于条件函数筛选值序列。将在条件函数的逻辑中使用每个元素的索引。
例如,根据
PartNumber的值,将salesOrderItems去重。var parts = salesOrderItems .Where((x, i) => salesOrderItems.FindIndex(z => z.PartNumber == x.PartNumber) == i) .ToList();