2008年11月6日星期四

Refactor in C#

I am loving the productivity enhancements by using Refactor in VS2008.

1, VS2008 itself provides:


2, A FREE tool, CodeRushX



2008年10月31日星期五

2008年9月27日星期六

C# 中的 ?? 操作符有什么用?

场景介绍:

程序存储一些用户设定到一个文件中,这些设置被分为多个集合,分别包装成class,每个集合中的值项被定义为property。如果取propertynull,则可以认为文件中不存在该值得记录。

示例代码:

public class MySettings
{
public string NumOfPoints{ get; set; }
}

public class UserPreferences
{
public MySettings Settings{ get; set; }
}

class Program
{
static void Main(string[] args)
{
UserPreferences option = new UserPreferences();
string num = option.MySettings01 ?? 10; // only ONE line
}
}

class Program
{
static void Main(string[] args)
{
UserPreferences option = new UserPreferences();

// THREE lines!
string num = 10;
if( option.MySettings01 != null )
num = option.MySettings01.NumOfPoints;
}
}

总结:

C#中的操作符 ?? 在处理 property null 时可以简化代码。

2008年9月23日星期二

使用Google阅读器,共享你看到的好文章

Google Reader有个功能,就是能向你的Google好友共享你看到的好文章

 


 

2008年8月24日星期日

Introduce WPF UI Automation Test

video

The video above is a MSDN example of WPF UI automation for automated testing. You can get the article here, get the example here. To run the example, you need VS2008 installed.

2008年8月12日星期二

C# code snippets

I started to use WPF in our recent project. I need implement INotifyPropertyChanged interface for some classes. So I learnt to use code snippets. I find it is very good! Here is a brief tutorial that can help you get started quickly.

1, dowload snippets, here is some provided by MS, here is some provied by Dr.WPF;


2, use code snippets manager import them;


3, right click mouse where you want to insert codes;



4, select the snippet;


5, see, the codes added!

Now, you may curious about how to create your own snippets. Here can help you to get start.

Here is a good free tool which can help you create your snippets.

2008年8月11日星期一

Use "HP Quick Lanch Buttons"

This may help if you own a HP laptop, if you do not have one, you can skip it.


I have seen this small rocket for almost 2 years, but I learnt to use it only few days ago.


One function I like is "ZOOM". When you use 1680 * 1050 screen resolution, you may want to use large font. Here is the quicker way than change screen properties. I change this frequently because I am using dual monitors.


Another function I like is the quick buttons. There is button, a circle around a question mark, on the left top of your laptop keybord. Press the button, you get the button menus. Right click on the small rocket, you can "Adjust HP Quick Lanch Buttons Properties".

2008年7月30日星期三

Visula Studio Tips

Tip #4 : Conditional Compilation Symbols


[Conditional("condition1")]
public void Foo()
{
...
}

But I found some limitations for it:
1, can not apply to interface functions;
2, the return type of functions MUST be void.


Tip #3
: In MFC projects, use "Call Browser". You can see it in right click popup menu.




Tip #2 : enable mixed mode debug. When you call managed functions from un-managed world. You may found the managed codes sample can NOT be loaded. Here is the trick to enable the load.



Tip #1 : use "ctrl + i" to search. after press the two keys, you can see a telescope shown on the screen, then type the word you want to search. Some time this is more convenient than "ctrl + f".

2008年7月22日星期二

Expression Blend Tips


Tip #1:
Use "search" to find the property you want.
Tip #2:
Use "Color Eyedropper", you can pick any displayed color on your PC screen.

2008年7月9日星期三

Use Google Reader read RSS


在使用多种RSS阅读器之后,感觉还是 Google Reader 最好,推荐给大家。这是个 Web 应用程序,不需要安装,只要访问 www.google.com/reader 即可。当然你需要一个 google 帐号。注意上图中使用红色圆圈标出的部分,使用这些功能可以方便的帮助你管理的你 feeds 。都是很简单的东西,这里就不多说了,自己动手试试吧。

2008年7月7日星期一

Minimun Windows Version for VC2008

Our project team is moving codes from vs2005 to vs2008. Most legacy codes are MFC based. We saw one compile error after the migration. We dig out the reason in MSDN.

Origin : in Chinese, in English

---- from MSDN ---

Visual C++ 概念:移植和升级
如何:修改 WINVER 和 _WIN32_WINNT
从 Visual C++ 2008 开始,Visual C++ 不支持面向 Windows 95、Windows 98、Windows ME 或 Windows NT。如果您的 WINVER 或 _WIN32_WINNT 宏被指定到这些 Windows 版本之一,则需要修改宏。当升级从 Visual C++ 的以前版本创建的项目时,如果将 WINVER 或 _WIN32_WINNT 宏指定到不再受支持的 Windows 版本,可能会看到与这些宏相关的编译错误。

2008年7月6日星期日

New Updates to MFC in VS2008

Here is a 30 minutes vedio talk about new updates to MFC in vs2008. If you are working with MFC, this is what you should watch.

2008年7月4日星期五

WPF ?

So many ab. in this world. What's WPF? If C# == C++ , then WPF == MFC; WPF just another package in .NET framework.












To develop WPF application, you need these dev tools:
1, Visual Studio 2008 (or 2005 with some sp);
2, Microsoft Expression Blend, for the GUI programming, if you never heard it, google it.

2008年7月3日星期四

The power of UserControl

System.Windows.Controls.UserControl, here is some trick you can use it.

Senario 1:
You need display a image in your program. You can use xaml only, or you can use C# codes only. In some cases, combine xaml and Csharp is powerful. First you create a UserControl which only contain the picture, then you get a new class. In Cshap codes, you just "new" one instance.

If you create a image use codes only, you need there lines at least:
Image img = new Image();
BitmapFrame bmp = BitmapFrame.Create(new Uri(@"C:\myImage.jpg", UriKind.RelativeOrAbsolute));
img.Source = bmp;
img.Stretch = Stretch.Fill;

But, use expression blend, you can create a new user control just by several mouse click. Then in codes, just one line:
new MyImage();

Data Binding

Data Binding is a key concept in WPF.

Why I can NOT data bind my class ?

public class MyClass
{
public string MyPorperty { get; set; }
}
First time, I trid to bind MyProperty in MyClass to a TextBox, but I can NOT. Why? Because I did not derived my class from interface INotifyPropertyChanged.

public class MyClass : INotifyPropertyChanged
{...}


Yes, we have to implement the interface. Csharp is a great programming language, but not magician words. So when your data changed, you have to tell the framework the change happened. That's why you need the interface.

Same reason, when you implement you own collection class, you have implement the INotifyCollectionChanged. You may encounter some problem when you want to bind a List to ComboBox/ListBox/ListView. No, you are using the wrong class, you should use ObservableCollection instead.

Use ValidationRule to check your input






-

Let's use TextBox as a sample. You can set a validation rule to the binding. When the rule check failed, a red ring will around the text box.

Related classes: System.Windows.Data.Binding, System.Windows.Controls.ValidationRule

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

Some Other Good Readings:

  1. Binding menus using HeirarchicalDataTemplates

2008年7月2日星期三

Regular Expression

I started to play with regular expression since a few days ago. In .NET framework , there is a namespace System.Test.RegularExpressions where you can find helpful classes. Give you a simple sample, I wrote this for our project. Maybe this can help you start.

Senario:
We need user to input string in a text box, the string represent a frequency value, like "10.000 MHz".

Validation rule: (top down are the charaters can legally show in the string)
0 to N spaces
0 to N figure(0,1,2...9)
0 or 1 dot
1 to N figures
0 to N spaces
0 or 1 letter(the letter could be k, K, m, M, g, G)
0 or 1 letter(h or H)
0 or 1 letter(z or Z)
0 to N spaces.

C# Codes:
Regex r = new Regex(@"^\s*\d*[.]?\d+\s*[kKmMgG]?[hH]?[zZ]?\s*$");
Match m = r.Match(" 20000000000 ghz");
if (m.Success)
{
Console.WriteLine("Found match at position " + m.Index);
}


What simple! You can image how hard this could be when you just use CString, or string classes.

See detail about regular expression here.

DocProject, a document generate tool

To setup the environment, you need install these software
  1. VisualStudio 2005 or 2008;
  2. Sandcastle
  3. Help 1.x
  4. Help 2.x (optional)
  5. DocProject

Make your "Hello World" DocProject

  1. Create a C# project, do some comments in the codes, check the "XML Documentation File" in project properties "Build" tab.
  2. Create a DocProject project, just follow the wizard, you can easily to setup it;
  3. Compile the projects, this may take some time, just be patient.
  4. After compile finished, you get your document. Yeah~~

Others

After do these, you may get a compile warning 1591, because you are not comment all the classes, functions, etc. Your can suppress this warning in project properties "Build" tab.