WCF附加属性技巧掌握

开发 开发工具
WCF附加属性的学习需要我们在实际开发中去不断的深入研究,这篇文章介绍的内容只是其中一个简单的开发实例,方便大家理解。

WCF作为一种比较新的技术工具,其中有许多东西值得我们去深入学习。比如WCF附加属性。我们在这里就为大家介绍一下WCF附加属性实现单实例的方法。#t#

WCF附加属性代码如下:

 

  1. class SingletonWindow  
  2. {  
  3. //注册附加属性  
  4. public static readonly DependencyProperty
     
    IsEnabledProperty =  
  5. DependencyProperty.RegisterAttached
    ("IsEnabled", typeof(bool), typeof
    (SingletonWindow), new Framework
    PropertyMetadata(OnIsEnabledChanged));  
  6. public static void SetIsEnabled
    (DependencyObject element, Boolean value)  
  7. {  
  8. element.SetValue(IsEnabledProperty, value);  
  9. }  
  10. public static Boolean GetIsEnabled
    (DependencyObject element)  
  11. {  
  12. return (Boolean)element.GetValue
    (IsEnabledProperty);  
  13. }  
  14. //根据附加属性的返回值使能单实例模式  
  15. public static void OnIsEnabledChanged
    (DependencyObject obj, Dependency
    PropertyChangedEventArgs args)  
  16. {  
  17. if ((bool)args.NewValue != true)  
  18. {  
  19. return;  
  20. }  
  21. Process();  
  22. return;  
  23. }  
  24. public static void Process()
     //如果不适用附加属性也可以直接使用此函数  
  25. {  
  26. //判断单实例的方式有很多,如mutex,process,
    文件锁等,这里用的是process方式  
  27. var process = GetRunningInstance();  
  28. if (process != null)  
  29. {  
  30. HandleRunningInstance(process);  
  31. Environment.Exit(0);  
  32. }  
  33. }  
  34. const int WS_SHOWNORMAL = 1;  
  35. [System.Runtime.InteropServices.
    DllImport("User32.dll")]  
  36. static extern bool ShowWindowAsync
    (IntPtr hWnd, int cmdShow);  
  37. [System.Runtime.InteropServices.
    DllImport("User32.dll")]  
  38. static extern bool SetForeground
    Window(IntPtr hWnd);  
  39. [System.Runtime.InteropServices.
    DllImport("user32.dll")]  
  40. static extern bool FlashWindow
    (IntPtr hWnd,bool bInvert);   
  41. static System.Diagnostics.Process 
    GetRunningInstance()  
  42. {  
  43. var current = System.Diagnostics.
    Process.GetCurrentProcess();  
  44. var processes = System.Diagnostics
    .Process.GetProcessesByName(current.
    ProcessName);  
  45. foreach (var process in processes)  
  46. {  
  47. if (process.Id != current.Id)  
  48. if (System.Reflection.Assembly.
    GetExecutingAssembly().Location.
    Replace("/", "\\") == current.
    MainModule.FileName)  
  49. return process;  
  50. }  
  51. return null;  
  52. }  
  53. static void HandleRunningInstance
    (System.Diagnostics.Process instance)  
  54. {  
  55. if (instance.MainWindowHandle!=IntPtr.Zero)  
  56. {  
  57. for (int i = 0; i < 2; i++)  
  58. {  
  59. FlashWindow(instance.MainWindowHandle, 500);  
  60. }  
  61. SetForegroundWindow(instance.
    MainWindowHandle);  
  62. ShowWindowAsync(instance.
    MainWindowHandle, WS_SHOWNORMAL);  
  63. }  
  64. else  
  65. {  
  66. //else 处理有点麻烦,简化如下  
  67. MessageBox.Show("已经有一个实例在运行,
    无法启动第二个实例");  
  68. }  
  69. }  
  70. static void FlashWindow(IntPtr hanlde, int interval)  
  71. {  
  72. FlashWindow(hanlde, true);  
  73. System.Threading.Thread.Sleep(interval);  
  74. FlashWindow(hanlde, false);  
  75. System.Threading.Thread.Sleep(interval);  
  76. }  
  77. }  

 

WCF附加属性代码其实很简单,前半部分是注册依赖属性,然后根据依赖属性判断是否启用单实例模式;后半部分就是一个传统的单实例模式的功能了。也就不介绍了。

使用这段WCF附加属性代码也很简单:

Xaml方式:在主窗口的xaml文件中加入附加属性即可

 

  1. < Window xmlns:src=
    "clr-namespace:WpfApplication1" 
    src:SingletonWindow.IsEnabled="true">  

传统方式:直接使用代码中后半部分,和winform下没什么区别。在主窗口的构造函数里面加入这句话。
SingletonWindow.Process();

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-11 14:21:57

PHP获取字段属性

2009-12-18 15:19:50

2022-11-01 15:57:44

2009-12-21 14:10:26

WCF异步调用

2009-12-14 18:10:21

Shell特性技巧

2009-12-21 15:53:56

WCF获取客户端IP

2010-03-30 09:04:26

Silverlight依赖属性附加属性

2009-11-09 15:06:34

WCF序列化

2009-11-06 11:07:52

WCF事务属性

2013-08-02 13:25:00

2009-12-29 10:22:34

WPF附加属性

2010-02-24 12:49:39

WCF枚举

2009-12-07 17:13:23

WCF技术

2010-02-23 09:44:12

WCF dataCon

2010-02-22 11:25:50

WCF DateSet

2009-11-09 15:28:04

WCF知识结构

2010-02-22 17:58:06

WCF异步上传

2010-03-01 13:06:49

WCF继承

2009-12-07 16:33:55

WCF 缓存

2009-11-05 13:16:59

WCF代理
点赞
收藏

51CTO技术栈公众号