c#入门

.NET简介

The .NET Framework (pronounced dot net) is a software framework developed by Microsoft that runs primarily on Microsoft Windows. It includes a large library and provides language interoperability (each language can use code written in other languages) across several programming languages. Programs written for the .NET Framework execute in a software environment (as contrasted tohardware environment), known as the Common Language Runtime (CLR), an application virtual machine that provides services such as security, memory management, and exception handling. The class library and the CLR together constitute the .NET Framework.

From Wikipedia(http://en.wikipedia.org/wiki/.NET_Framework)

它包含一个大的类库,可以提供在不同语言之间的互通性,即一种语言编写的程序可以被其他不同类型的语言识别和使用。

由.NET编写的程序会在一个软件虚拟机中被执行,这个虚拟机就是CLR,它可以提供安全性、内存管理和异常处理等服务,因此运行在CLR上面的程序也被称为托管程序(Managed Program)。

.NET分为两部分:

       1: CLR,公共语言运行时
       2: .NET framework类库

SimpleCalculator项目源码分析

下载地址:http://code.msdn.microsoft.com/Simple-Calculator-d1d8cf4c

开发步骤:

   1: Blend画项目的UI,并且进行简单的交互接口的设置
   2: Visual Studio进行逻辑代码的开发

熟悉Blend界面

Blend界面与wxBuilder十分相似,首先选取控件,布局到画布上。

精简之后,可以保留以下几个窗口:

   1: Tools                //用于操作
   2: Assets               //用于添加控件
   3: Projects            //管理项目中用到的文件
   4: Objects&Timeline    //管理控件对象的层次
   5: Properties            //管理当前控件的属性

其中Properties页面中又包含两个Tag页面,分别是Properties和Event Handler。

控件对象的创建

可以看到控件对象的创建是由Blend生成的代码进行创建的,即我们不用操心控件对象的生存期管理。

 

控件对象的属性和方法

控件对象的属性分为读属性和写属性,具体在c#中是通过getter/setter实现的。

方法是系统提供的完成一种操作的接口。

 

Attribute的作用

attribute与property不同:

  • property是对象的属性,是对象的内部特征,是对象的组成部分;
  • attribute是对象的附属物(attachment),是对对象的外部描述。

attribute主要用来添加元数据,这些元数据可以被编译进行程序集Assembly.cs中,被编译器使用,或者在运行时被Reflection机制使用。

比如:

   1: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
   2: internal sealed class FindData
   3: {
   4:     public int fileAttributes;
   5:     public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
   6:     public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
   7:     public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
   8:     public uint nFileSizeHigh;
   9:     public uint nFileSizeLow;
  10:     public uint dwReserved0;
  11:     public uint dwReserved1;
  12:     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  13:     public string fileName;
  14:     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  15:     public string alternateFileName;
  16: }

其中的StructLayout就是提供给编译器使用的attribute,用来告诉编译器怎样布局数据结构。

 

partial class的作用

顾名思义,就是定义部分类,或者说一个类可以被分开在不同地方定义,每处定义该类的一部分,而最后编译时会被编译器编译成一个完整的类。

 

P/Invoke

   1: [DllImport("TestDll", EntryPoint = "GetStringReturn")]
   2:  5         [return:MarshalAs(UnmanagedType.LPStr)]
   3:  6         public static extern string GetStringReturnByMarshal();

通过这种方式提供语言之间的互通性,这里是调用C++编写的DLL中的函数。

 

WPF

MSDN上关于WPF的介绍(http://msdn.microsoft.com/zh-cn/library/aa970268(v=vs.110).aspx

 

代码隐藏

在WPF中,界面部分通过Blend生成,保存在XAML中,而且我们看到在程序的.cs代码中,通过partial class定义了C#部分实现的代码,那么另一部分partial class是在哪里定义的呢?答案就是在XAML中,WPF会将UI生成的代码隐藏类与程序中定义的那部分类进行合并。

 

数据绑定

通过Binding类实现,可以将控件对象与数据对象进行绑定,这样,两者中任何一者发生变化,另外一者都会自动地进行同步。

posted @ 2013-11-11 17:56  Daniel King  阅读(272)  评论(0编辑  收藏  举报