WCF附加屬性技巧掌握
WCF作為一種比較新的技術(shù)工具,其中有許多東西值得我們?nèi)ド钊雽W(xué)習(xí)。比如WCF附加屬性。我們在這里就為大家介紹一下WCF附加屬性實(shí)現(xiàn)單實(shí)例的方法。#t#
WCF附加屬性代碼如下:
- class SingletonWindow
 - {
 - //注冊附加屬性
 - public static readonly DependencyProperty
IsEnabledProperty = - DependencyProperty.RegisterAttached
("IsEnabled", typeof(bool), typeof
(SingletonWindow), new Framework
PropertyMetadata(OnIsEnabledChanged)); - public static void SetIsEnabled
(DependencyObject element, Boolean value) - {
 - element.SetValue(IsEnabledProperty, value);
 - }
 - public static Boolean GetIsEnabled
(DependencyObject element) - {
 - return (Boolean)element.GetValue
(IsEnabledProperty); - }
 - //根據(jù)附加屬性的返回值使能單實(shí)例模式
 - public static void OnIsEnabledChanged
(DependencyObject obj, Dependency
PropertyChangedEventArgs args) - {
 - if ((bool)args.NewValue != true)
 - {
 - return;
 - }
 - Process();
 - return;
 - }
 - public static void Process()
//如果不適用附加屬性也可以直接使用此函數(shù) - {
 - //判斷單實(shí)例的方式有很多,如mutex,process,
文件鎖等,這里用的是process方式 - var process = GetRunningInstance();
 - if (process != null)
 - {
 - HandleRunningInstance(process);
 - Environment.Exit(0);
 - }
 - }
 - const int WS_SHOWNORMAL = 1;
 - [System.Runtime.InteropServices.
DllImport("User32.dll")] - static extern bool ShowWindowAsync
(IntPtr hWnd, int cmdShow); - [System.Runtime.InteropServices.
DllImport("User32.dll")] - static extern bool SetForeground
Window(IntPtr hWnd); - [System.Runtime.InteropServices.
DllImport("user32.dll")] - static extern bool FlashWindow
(IntPtr hWnd,bool bInvert); - static System.Diagnostics.Process 
GetRunningInstance() - {
 - var current = System.Diagnostics.
Process.GetCurrentProcess(); - var processes = System.Diagnostics
.Process.GetProcessesByName(current.
ProcessName); - foreach (var process in processes)
 - {
 - if (process.Id != current.Id)
 - if (System.Reflection.Assembly.
GetExecutingAssembly().Location.
Replace("/", "\\") == current.
MainModule.FileName) - return process;
 - }
 - return null;
 - }
 - static void HandleRunningInstance
(System.Diagnostics.Process instance) - {
 - if (instance.MainWindowHandle!=IntPtr.Zero)
 - {
 - for (int i = 0; i < 2; i++)
 - {
 - FlashWindow(instance.MainWindowHandle, 500);
 - }
 - SetForegroundWindow(instance.
MainWindowHandle); - ShowWindowAsync(instance.
MainWindowHandle, WS_SHOWNORMAL); - }
 - else
 - {
 - //else 處理有點(diǎn)麻煩,簡化如下
 - MessageBox.Show("已經(jīng)有一個(gè)實(shí)例在運(yùn)行,
無法啟動(dòng)第二個(gè)實(shí)例"); - }
 - }
 - static void FlashWindow(IntPtr hanlde, int interval)
 - {
 - FlashWindow(hanlde, true);
 - System.Threading.Thread.Sleep(interval);
 - FlashWindow(hanlde, false);
 - System.Threading.Thread.Sleep(interval);
 - }
 - }
 
WCF附加屬性代碼其實(shí)很簡單,前半部分是注冊依賴屬性,然后根據(jù)依賴屬性判斷是否啟用單實(shí)例模式;后半部分就是一個(gè)傳統(tǒng)的單實(shí)例模式的功能了。也就不介紹了。
使用這段WCF附加屬性代碼也很簡單:
Xaml方式:在主窗口的xaml文件中加入附加屬性即可
 
- < Window xmlns:src=
 
"clr-namespace:WpfApplication1"
src:SingletonWindow.IsEnabled="true">
傳統(tǒng)方式:直接使用代碼中后半部分,和winform下沒什么區(qū)別。在主窗口的構(gòu)造函數(shù)里面加入這句話。
SingletonWindow.Process();















 
 
 
 
 
 
 