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

📄 connectionmonitorfactory.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 System.Xml;
using System.IO;
using Microsoft.Practices.Mobile.ConnectionMonitor.Configuration;
using Microsoft.Practices.Mobile.Configuration;
using Microsoft.Practices.Mobile.ConnectionMonitor.Implementations;

namespace Microsoft.Practices.Mobile.ConnectionMonitor
{
    public static class ConnectionMonitorFactory 
	{
		private static ConnectionMonitor monitor = new ConnectionMonitor();
		private static bool initialized;

        /// <summary>
		/// This factory method initializes the ConnectionMonitor single instance adding networks acording to the 
		/// native Connection Manager API and connections from the deafault configuration section
		/// "Connections".
        /// </summary>
		/// <returns>The ConnectionMonitor instance.</returns>
		public static ConnectionMonitor CreateFromConfiguration()
		{
			return CreateFromConfiguration("Connections");
		}

		/// <summary>
		///		This factory method initializes the ConnectionMonitor single instance adding networks acording to the 
		///		native Connection Manager API and connections from the supplied configuration section
		///		name.
		/// </summary>
		/// <param name="sectionName">
		///		Name of the section in the configuration file (App.config), wich contains the different
		///		types of connection
		/// </param>
		/// <returns>The ConnectionMonitor instance.</returns>
        public static ConnectionMonitor CreateFromConfiguration(string sectionName)
        {
			lock (monitor)
			{
				if (!initialized)
				{
					ConnectionSettingsSection configuration = ConfigurationManager.GetSection(sectionName) as ConnectionSettingsSection;

					List<string> networkNames = ConnectionMonitorNativeHelper.GetNetworkList();
					foreach (string name in networkNames)
					{
						monitor.Networks.Add(new Network(name));
					}

					if (configuration != null)
					{
						foreach (ConnectionItemElement itemConnection in configuration.ConnectionItems)
						{
							switch (itemConnection.Type)
							{
								case "NicConnection":
									monitor.Connections.Add(new NicConnection("NicConnection", itemConnection.Price));
									break;
								case "DesktopConnection":
									monitor.Connections.Add(new DesktopConnection("DesktopConnection", itemConnection.Price));
									break;
								case "CellConnection":
									monitor.Connections.Add(new CellConnection("CellConnection", itemConnection.Price));
									break;
								default:
									throw new ConnectionMonitorException("Unsupported Connection Type",0);
							}
						}
					}

					initialized = true;
				}
			}
			return monitor;
		}

		/// <summary>
		/// Provides access to the single instance of the ConnectionMonitor.
		/// </summary>
		public static ConnectionMonitor Instance
		{
			get 
			{
				lock (monitor)
				{
					if (!initialized)
						throw new InvalidOperationException(Properties.Resources.InstanceNotInitialized);
					return monitor;
				}
			}
		}
    }
}

⌨️ 快捷键说明

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