⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionmonitoradapter.cs

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 CS
字号:
//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Mobile.ConnectionMonitor;

namespace Microsoft.Practices.Mobile.DisconnectedAgent
{
	/// <summary>
	/// Adapter that implements the IConnectionMonitor interface required by the
	/// Disconnected Agent Block using the ConnectionMonitor block.
	/// </summary>
	public class ConnectionMonitorAdapter : IConnectionMonitor, IDisposable
	{
		bool disposed;
		ConnectionMonitor.ConnectionMonitor monitor;

		/// <summary>
		/// Fired when the ConnectionMonitor gets connection.
		/// </summary>
		public event EventHandler ConnectionStatusChanged;

		/// <summary>
		/// Constructor which creates a new adapter using the given ConnectionMonitor.
		/// </summary>
		/// <param name="monitor">ConnectionMonitor to be engaged.</param>
		public ConnectionMonitorAdapter(ConnectionMonitor.ConnectionMonitor monitor)
		{
			Guard.ArgumentNotNull(monitor, "monitor");
			this.monitor = monitor;
			monitor.ActiveNetworkChanged += ConnectionManager_ActiveNetworkChanged;
		}

		void ConnectionManager_ActiveNetworkChanged(object sender, ActiveNetworkChangedEventArgs args)
		{
			if (ConnectionStatusChanged != null && args.Active)
				ConnectionStatusChanged(sender, EventArgs.Empty);
		}

		/// <summary>
		/// This method gets the current connection price from the ConnectionMonitor.
		/// </summary>
		/// <exception cref="InvalidOperationException">
		/// If there is not active connection this method throws an InvalidOperationException.
		/// </exception>
		public uint CurrentConnectionPrice
		{
			get 
			{
				if (disposed) throw new ObjectDisposedException("ConnectionManagerAdapter");
				if (monitor.ActiveConnection == null)
					throw new InvalidOperationException(Properties.Resources.PriceNotConnection);
				return (uint) monitor.ActiveConnection.Price; 
			}
		}

		/// <summary>
		/// Gets the current network name.
		/// If there is not active network returns null.
		/// </summary>
		public string CurrentNetwork
		{
			get 
			{
				if (disposed) throw new ObjectDisposedException("ConnectionManagerAdapter");
				if (monitor.ActiveNetwork == null) return null;
				return monitor.ActiveNetwork.Name; 
			}
		}

		/// <summary>
		/// Gets the connection state.
		/// </summary>
		public bool IsConnected
		{
			get 
			{
				if (disposed) throw new ObjectDisposedException("ConnectionManagerAdapter");
				return monitor.IsConnected; 
			}
		}

		/// <summary>
		/// This method disposes adapter used resources.
		/// </summary>
		public void Dispose()
		{
			if (disposed) throw new ObjectDisposedException("ConnectionManagerAdapter");
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		private void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				monitor.ActiveNetworkChanged -= ConnectionManager_ActiveNetworkChanged;
			}
			disposed = true;
		}

		~ConnectionMonitorAdapter()
		{
			Dispose(false);
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -