日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WCF附加屬性技巧掌握

WCF作為一種比較新的技術(shù)工具,其中有許多東西值得我們?nèi)ド钊雽W(xué)習(xí)。比如WCF附加屬性。我們在這里就為大家介紹一下WCF附加屬性實現(xiàn)單實例的方法。#t#

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比渝北網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式渝北網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋渝北地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。

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. //根據(jù)附加屬性的返回值使能單實例模式
  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()
     //如果不適用附加屬性也可以直接使用此函數(shù)
  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("已經(jīng)有一個實例在運行,
    無法啟動第二個實例");
  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. }

WCF附加屬性代碼其實很簡單,前半部分是注冊依賴屬性,然后根據(jù)依賴屬性判斷是否啟用單實例模式;后半部分就是一個傳統(tǒng)的單實例模式的功能了。也就不介紹了。

使用這段WCF附加屬性代碼也很簡單:

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

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

傳統(tǒng)方式:直接使用代碼中后半部分,和winform下沒什么區(qū)別。在主窗口的構(gòu)造函數(shù)里面加入這句話。
SingletonWindow.Process();


文章題目:WCF附加屬性技巧掌握
鏈接分享:http://www.5511xx.com/article/dhsdcch.html