1, VS2008 itself provides:
2, A FREE tool, CodeRushX
software development and computer skills
场景介绍:
程序存储一些用户设定到一个文件中,这些设置被分为多个集合,分别包装成class,每个集合中的值项被定义为property。如果取property为null,则可以认为文件中不存在该值得记录。
示例代码:
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 时可以简化代码。
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:
Make your "Hello World" DocProject
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.