posted @ 2012-11-25 16:30 from [FreedomShe]
1. 實現單例。
2. 加入MVVM支持庫Prism。(加速MVVM模式開發)
3. 定義符合Prism屬性格式的Code Snippet。(加速開發)
1. 實現單例。fromWPF程序單例實現
重載App的OnStartup方法:
1 public partial class App : Application 2 { 3 protected override void OnStartup(StartupEventArgs e) 4 { 5 Process process = Process.GetCurrentProcess(); 6 foreach (Process p in Process.GetProcessesByName(process.ProcessName)) 7 { 8 if (p.Id != process.Id) 9 { 10 //關閉第二個啟動的程序 11 MessageBox.Show("您的程序已經啟動!"); 12 Application.Current.Shutdown(); 13 return; 14 } 15 } 16 base.OnStartup(e); 17 } 18 }
2. 加入MVVM支持庫Prism。from《深入淺出WPF》系列視頻(特輯)——MVVM入門與提高(難度300+)
添加Microsoft.Practices.Prism.dll引用
工程->引用->右鍵添加引用->瀏覽->找到自己的Microsoft.Practices.Prism.dll(如果沒有安裝Prism需下載解壓,地址見下文)
加入Prism引用后,ViewModel就可以這么寫了:

class MainWindowViewModel : NotificationObject { //屬性 private String newContent; public String NewContent { get { return newContent; } set { newContent = value; RaisePropertyChanged("NewContent"); } } //命令 public DelegateCommand ConvertStringCommand { get; set; } private void ConvertStringExecute() { //TODO } //ViewModel的構造函數 public MainWindowViewModel() { ConvertStringCommand = new DelegateCommand(new Action(ConvertStringExecute)); } }
下載Prism:http://compositewpf.codeplex.com/releases/view/55576 Prism針對很多應用平台有不同的版本,我下的Prism for .NET 4.5——Microsoft.Practices.Prism.zip 可用於桌面應用開發。我用的這個版本里面只有4個dll,實際上我只需要用一個:Microsoft.Practices.Prism.dll,解壓到自己的文件夾即可,開發的時候引用進去。
Notice: Prism for .NET 4.5需要.NET 4.5框架支持,如果客戶機沒有安裝.NET 4.5 Framework會導致界面無法顯示。另外Visual studio 2012編譯的程序在Windows XP下無法運行,據說是VS2012加入了XP無法識別的頭信息。
3. 定義符合Prism屬性格式的Code Snippet。from 《深入淺出WPF》系列視頻(特輯)——MVVM入門與提高(難度300+)
VS2010菜單下:工具->代碼段管理器->Visual C#下拉菜單->Visual C#選項->propfull
在“位置”所示路徑下找到“propfull.snippet”所在文件夾,新建文件“propn.snippet”,粘貼如下代碼

1 <?xml version="1.0" encoding="utf-8"?> 2 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 3 <CodeSnippet Format="1.0.0"> 4 <Header> 5 <Title>propn</Title> 6 <Shortcut>propn</Shortcut> 7 <Description>屬性和支持字段的代碼段 For Prism</Description> 8 <Author>Microsoft Corporation</Author> 9 <SnippetTypes> 10 <SnippetType>Expansion</SnippetType> 11 </SnippetTypes> 12 </Header> 13 <Snippet> 14 <Declarations> 15 <Literal> 16 <ID>type</ID> 17 <ToolTip>屬性類型</ToolTip> 18 <Default>int</Default> 19 </Literal> 20 <Literal> 21 <ID>property</ID> 22 <ToolTip>屬性名</ToolTip> 23 <Default>MyProperty</Default> 24 </Literal> 25 <Literal> 26 <ID>field</ID> 27 <ToolTip>支持此屬性的變量</ToolTip> 28 <Default>myVar</Default> 29 </Literal> 30 </Declarations> 31 <Code Language="csharp"><![CDATA[private $type$ $field$; 32 33 public $type$ $property$ 34 { 35 get { return $field$;} 36 set 37 { 38 $field$ = value; 39 RaisePropertyChanged("$property$"); 40 } 41 } 42 $end$]]> 43 </Code> 44 </Snippet> 45 </CodeSnippet> 46 </CodeSnippets>
於是能看到:
於是項目中可以使用“propn" 字符串輸出代碼段了。
End,告別了MFC時代,跳過了Winform,對有WEB開發經驗的人來說WPF暴爽,此貼就用來記錄新機配置環境時的個人設置習慣吧。