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

📄 addwebreferencedialog.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
	
		/// <summary>
		/// Gets the namespace to be used with the generated web reference code.
		/// </summary>
		string GetDefaultNamespace()
		{
			string defaultNamespace = String.Empty;
			
			if (project.StandardNamespace.Length > 0) {
				defaultNamespace = String.Concat(project.StandardNamespace, ".", WebReference.GetNamespaceFromUri(webBrowser.LocationURL));
			} else {
				defaultNamespace = WebReference.GetNamespaceFromUri(webBrowser.LocationURL);
			}

			return defaultNamespace;
		}
		
		/// <summary>
		/// Updates the web services tree view after we have navigated to a different
		/// url.
		/// </summary>
		void DisplayWebService(ServiceDescription desc)
		{
			webServicesTreeView.BeginUpdate();
			
			try {
				ClearWebService();
	
				ServiceDescription = desc;	
				
				if(desc == null) 
					return;
								
				TreeNode rootNode = new TreeNode(WebReference.GetNamespaceFromUri(webBrowser.LocationURL));			
				rootNode.Tag = desc;
				rootNode.ImageIndex = 6;
				rootNode.SelectedImageIndex = 6;
				webServicesTreeView.Nodes.Add(rootNode);
				
				foreach(Service svc in desc.Services) 
				{
					// add a Service node
					TreeNode serviceNode = new TreeNode(svc.Name);				
					serviceNode.Tag = svc;
					serviceNode.ImageIndex = 0;
					serviceNode.SelectedImageIndex = 1;
					rootNode.Nodes.Add(serviceNode);
					
					foreach(Port port in svc.Ports) 
					{					
						TreeNode portNode = new TreeNode(port.Name);
						portNode.Tag = port;
						portNode.ImageIndex = 2;
						portNode.SelectedImageIndex = 3;
						serviceNode.Nodes.Add(portNode);
									
						// get the operations
						System.Web.Services.Description.Binding binding = desc.Bindings[port.Binding.Name];
						if (binding != null) {
							PortType portType = desc.PortTypes[binding.Type.Name];
							if (portType != null) {
								foreach(Operation operation in portType.Operations) 
								{
									TreeNode operationNode = new TreeNode(operation.Name);
									operationNode.Tag = operation;
									operationNode.ImageIndex = 4;
									operationNode.SelectedImageIndex = 5;
									portNode.Nodes.Add(operationNode);
								}	
							}
						}			
					}															
				}
				webServicesTreeView.ExpandAll();	
								
			} finally {
				webServicesTreeView.EndUpdate();
			}
		}	
		
		bool IsValidNamespace {
			get {
				bool valid = false;
				
				if (namespaceTextBox.Text.Length > 0) {
					
					// Can only begin with a letter or '_'
					char ch = namespaceTextBox.Text[0];
					if (Char.IsLetter(ch) || (ch == '_')) {
						valid = true;
						for (int i = 1; i < namespaceTextBox.Text.Length; ++i) {
							ch = namespaceTextBox.Text[i];
							// Can only contain letters, digits or '_'
							if (!Char.IsLetterOrDigit(ch) && (ch != '.') && (ch != '_')) {
								valid = false;
								break;
							}
						}
					}					
				}
				
				return valid;
			}
		}
		
		bool IsValidReferenceName {
			get {
				bool valid = false;
				
				if (referenceNameTextBox.Text.Length > 0) {
					if (referenceNameTextBox.Text.IndexOf('\\') == -1) {
						if (!ContainsInvalidDirectoryChar(referenceNameTextBox.Text)) {
						    	valid = true;
						}
					}
				}
				
				return valid;
			}
		}
		
		bool ContainsInvalidDirectoryChar(string item)
		{
			bool hasInvalidChar = false;
			
			foreach (char ch in Path.InvalidPathChars) {
				if (item.IndexOf(ch) >= 0) {
					hasInvalidChar = true;
					break;
				}
			}
				
			return hasInvalidChar;
		}
		
		void ClearWebService()
		{
			webServicePropertiesListView.Items.Clear();
			webServicesTreeView.Nodes.Clear();
		}

		/// <summary>
		/// Starts the search for web services at the specified url.
		/// </summary>
		void StartDiscovery(string url)
		{
			// Abort previous discovery.
			StopDiscovery();
			
			// Start new discovery.
			DiscoverAnyAsync asyncDelegate = new DiscoverAnyAsync(discoveryClientProtocol.DiscoverAny);
			AsyncCallback callback = new AsyncCallback(DiscoveryCompleted);
			discoveryClientProtocol.Credentials = CredentialCache.DefaultCredentials;
			IAsyncResult result = asyncDelegate.BeginInvoke(url, callback, discoveryClientProtocol);
		}
		
		/// <summary>
		/// Called after an asynchronous web services search has
		/// completed.
		/// </summary>
		void DiscoveryCompleted(IAsyncResult result)
		{
			DiscoveryClientProtocol protocol = (DiscoveryClientProtocol)result.AsyncState;
					
			// Check that we are still waiting for this particular callback.
			bool wanted = false;
			lock (this) {
				wanted = Object.ReferenceEquals(discoveryClientProtocol, protocol);
			}
			
			if (wanted) {
				DisplayWebServiceHandler displayHandler = new DisplayWebServiceHandler(DisplayWebService);
				try {
					DiscoverAnyAsync asyncDelegate = (DiscoverAnyAsync)((AsyncResult)result).AsyncDelegate;
					DiscoveryDocument doc = asyncDelegate.EndInvoke(result);
					Invoke(displayHandler, new object[] {GetServiceDescription(protocol)});
				} catch (Exception) {
					Invoke(displayHandler, new object[] {null});
				}
			} 
		}
		
		/// <summary>
		/// Stops any outstanding asynchronous discovery requests.
		/// </summary>
		void StopDiscovery()
		{
			lock (this) {
				if (discoveryClientProtocol != null) {
					try {
						discoveryClientProtocol.Abort();
					} catch (NotImplementedException) {};
					discoveryClientProtocol.Dispose();
				}
				discoveryClientProtocol = new DiscoveryClientProtocol();
			}
		}
		
		void DialogClosing(object sender, CancelEventArgs e)
		{
			StopDiscovery();
		}
		
		ServiceDescription GetServiceDescription(DiscoveryClientProtocol protocol)
		{
			ServiceDescription desc = null;
			protocol.ResolveOneLevel();
			
			foreach (DictionaryEntry entry in protocol.References) {
				ContractReference contractRef = entry.Value as ContractReference;				
				if (contractRef != null) {
					desc = contractRef.Contract;
					break;
				}
			}
			
			return desc;
		}
		
		void UrlComboBoxKeyDown(object sender, KeyEventArgs e)
		{
			if((e.KeyValue == '\r') && (urlComboBox.Text != null) && (urlComboBox.Text != "")) {
				BrowseUrl(urlComboBox.Text);
			}
		}
		
		void UrlComboBoxSelectedIndexChanged(object sender, System.EventArgs e)
		{
			BrowseUrl(urlComboBox.Text);
		}		
		
		#region Url Autocompletion
		
		[Flags]
		enum AutoCompleteFlags : uint
		{
			Default             = 0x00000000,
			FileSystem          = 0x00000001,
			UrlHistory          = 0x00000002,
			UrlMenu             = 0x00000004,
			UseTab              = 0x00000008,
			FileSystemOnly      = 0x00000010,
			UrlAll              = UrlHistory|UrlMenu,
			FileSystemDirs      = 0x00000020,
			AutoSuggestForceOn  = 0x10000000,
			AutoSuggestForceOff = 0x20000000,
			AutoAppendForceOn   = 0x40000000,
			AutoAppendForceOff  = 0x80000000
		}
		
		[StructLayout(LayoutKind.Sequential)]
		struct COMBOBOXINFO {
			public Int32 cbSize;
			public RECT rcItem;
			public RECT rcButton;
			public ComboBoxButtonState buttonState;
			public IntPtr hwndCombo;
			public IntPtr hwndEdit;
			public IntPtr hwndList;
		}
		
		enum ComboBoxButtonState {
			STATE_SYSTEM_NONE = 0,
			STATE_SYSTEM_INVISIBLE = 0x00008000,
			STATE_SYSTEM_PRESSED = 0x00000008
		}
		
		[StructLayout(LayoutKind.Sequential)]
		struct RECT 
		{
		        public int left;
		        public int top;
		        public int right;
		        public int bottom;
		}
		
		[DllImport("user32.dll")]
		static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);
		
		[DllImport("shlwapi.dll")]
		static extern int SHAutoComplete(IntPtr Handle, uint Flags);
		
		/// <summary>
		/// Sets up autocompletion for the url combo box.
		/// </summary>
		void InitAutoCompletion()
		{
			COMBOBOXINFO cbi = new COMBOBOXINFO();
			cbi.cbSize = Marshal.SizeOf(cbi);
			if(GetComboBoxInfo(urlComboBox.Handle, ref cbi)) {
				if(cbi.hwndEdit != IntPtr.Zero) {
					SHAutoComplete(cbi.hwndEdit, (uint)AutoCompleteFlags.Default);
					AddMRUList();
			    }
			}
		}
		
		/// <summary>
		/// Reads MRU list from registry and adds it to the url combo box.
		/// </summary>
		void AddMRUList()
		{
			try {
				RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\TypedURLs");
				foreach (string name in key.GetValueNames()) {
					urlComboBox.Items.Add((string)key.GetValue(name));
				}
				
			} catch (Exception) { };
		}
		
		#endregion
	}
}

⌨️ 快捷键说明

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