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

创新互联-专业网站定制、快速模板网站建设、高性价比渝北网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式渝北网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖渝北地区。费用合理售后完善,十多年实体公司更值得信赖。
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);  
- } 
 
- //根据附加属性的返回值使能单实例模式 
 
- public static void OnIsEnabledChanged
 (DependencyObject obj, Dependency
 PropertyChangedEventArgs args)  
- { 
 
- if ((bool)args.NewValue != true) 
 
- { 
 
- return; 
 
- } 
 
- Process(); 
 
- return; 
 
- } 
 
- public static void Process()
  //如果不适用附加属性也可以直接使用此函数  
- { 
 
- //判断单实例的方式有很多,如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 处理有点麻烦,简化如下 
 
- MessageBox.Show("已经有一个实例在运行,
 无法启动第二个实例");  
- } 
 
- } 
 
- static void FlashWindow(IntPtr hanlde, int interval) 
 
- { 
 
- FlashWindow(hanlde, true); 
 
- System.Threading.Thread.Sleep(interval); 
 
- FlashWindow(hanlde, false); 
 
- System.Threading.Thread.Sleep(interval); 
 
- } 
 
- } 
 
WCF附加属性代码其实很简单,前半部分是注册依赖属性,然后根据依赖属性判断是否启用单实例模式;后半部分就是一个传统的单实例模式的功能了。也就不介绍了。
使用这段WCF附加属性代码也很简单:
Xaml方式:在主窗口的xaml文件中加入附加属性即可
 
 
 
  
  
  - < Window xmlns:src=
"clr-namespace:WpfApplication1" 
src:SingletonWindow.IsEnabled="true">  
 
 
 
传统方式:直接使用代码中后半部分,和winform下没什么区别。在主窗口的构造函数里面加入这句话。
 SingletonWindow.Process();
            
                网页名称:WCF附加属性技巧掌握
                
                URL标题:http://www.csdahua.cn/qtweb/news0/234500.html
            
            
                网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
                                
            
                广告
                
            
            
                声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源:
                快上网