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

📄 addwebreferencedialog.cs

📁 SharpDevelop2.0.0 c#开发免费工具
💻 CS
📖 第 1 页 / 共 2 页
字号:
				}
			} 
		}
		
		void ToolStripEnter(object sender, EventArgs e)
		{
			toolStrip.TabStop = false;
		}
		
		void ToolStripLeave(object sender, EventArgs e)
		{
			toolStrip.TabStop = true;
		}
		
		void BackButtonClick(object sender, EventArgs e)
		{
			try {
				webBrowser.GoBack();
			} catch (Exception) { }			
		}
		
		void ForwardButtonClick(object sender, System.EventArgs e)
		{
			try {
				webBrowser.GoForward();
			} catch (Exception) { }
		}
		
		void StopButtonClick(object sender, System.EventArgs e)
		{
			webBrowser.Stop();
			StopDiscovery();
			addButton.Enabled = false;		
		}
		
		void RefreshButtonClick(object sender, System.EventArgs e)
		{
			webBrowser.Refresh();
		}
		
		void GoButtonClick(object sender, System.EventArgs e)
		{
			BrowseUrl(urlComboBox.Text);
		}
		
		void BrowseUrl(string url)
		{
			webBrowser.Focus();
			webBrowser.Navigate(url);
		}
		
		void CancelButtonClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void WebBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
		{
			Cursor = Cursors.WaitCursor;
			stopButton.Enabled = true;
			webServicesView.Clear();
		}
		
		void WebBrowserNavigated(object sender, WebBrowserNavigatedEventArgs e)
		{
			Cursor = Cursors.Default;	
			stopButton.Enabled = false;
			urlComboBox.Text = webBrowser.Url.ToString();
			StartDiscovery(e.Url);
		}
		
		void WebBrowserCanGoForwardChanged(object sender, EventArgs e)
		{
			forwardButton.Enabled = webBrowser.CanGoForward;
		}
		
		void WebBrowserCanGoBackChanged(object sender, EventArgs e)
		{
			backButton.Enabled = webBrowser.CanGoBack;
		}
		
		/// <summary>
		/// Gets the namespace to be used with the generated web reference code.
		/// </summary>
		string GetDefaultNamespace()
		{
			if (namespacePrefix.Length > 0 && discoveryUri != null) {
				return String.Concat(namespacePrefix, ".", discoveryUri.Host);
			} else if (discoveryUri != null) {
				return discoveryUri.Host;
			}
			return String.Empty;
		}
		
		string GetReferenceName()
		{
			if (discoveryUri != null) {
				return discoveryUri.Host;
			} 
			return String.Empty;
		}

		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 {
				if (referenceNameTextBox.Text.Length > 0) {
					if (referenceNameTextBox.Text.IndexOf('\\') == -1) {
						if (!ContainsInvalidDirectoryChar(referenceNameTextBox.Text)) {
						    	return true;
						}
					}
				}
				return false;
			}
		}
		
		bool ContainsInvalidDirectoryChar(string item)
		{
			foreach (char ch in Path.GetInvalidPathChars()) {
				if (item.IndexOf(ch) >= 0) {
					return true;
				}
			}
			return false;
		}
		
		/// <summary>
		/// Starts the search for web services at the specified url.
		/// </summary>
		void StartDiscovery(Uri uri)
		{
			StartDiscovery(uri, new DiscoveryNetworkCredential(CredentialCache.DefaultNetworkCredentials, DiscoveryNetworkCredential.DefaultAuthenticationType));
		}
		
		void StartDiscovery(Uri uri, DiscoveryNetworkCredential credential)
		{
			// Abort previous discovery.
			StopDiscovery();
			
			// Start new discovery.
			discoveryUri = uri;
			DiscoverAnyAsync asyncDelegate = new DiscoverAnyAsync(discoveryClientProtocol.DiscoverAny);
			AsyncCallback callback = new AsyncCallback(DiscoveryCompleted);
			discoveryClientProtocol.Credentials = credential;
			IAsyncResult result = asyncDelegate.BeginInvoke(uri.AbsoluteUri, callback, new AsyncDiscoveryState(discoveryClientProtocol, uri, credential));
		}
		
		/// <summary>
		/// Called after an asynchronous web services search has
		/// completed.
		/// </summary>
		void DiscoveryCompleted(IAsyncResult result)
		{
			AsyncDiscoveryState state = (AsyncDiscoveryState)result.AsyncState;
			WebServiceDiscoveryClientProtocol protocol = state.Protocol;
					
			// Check that we are still waiting for this particular callback.
			bool wanted = false;
			lock (this) {
				wanted = Object.ReferenceEquals(discoveryClientProtocol, protocol);
			}
			
			if (wanted) {
				DiscoveredWebServicesHandler handler = new DiscoveredWebServicesHandler(DiscoveredWebServices);
				try {
					DiscoverAnyAsync asyncDelegate = (DiscoverAnyAsync)((AsyncResult)result).AsyncDelegate;
					DiscoveryDocument doc = asyncDelegate.EndInvoke(result);
					if (!state.Credential.IsDefaultAuthenticationType) {
						AddCredential(state.Uri, state.Credential);
					}
					Invoke(handler, new object[] {protocol});
				} catch (Exception ex) {
					if (protocol.IsAuthenticationRequired) {
						HttpAuthenticationHeader authHeader = protocol.GetAuthenticationHeader();
						AuthenticationHandler authHandler = new AuthenticationHandler(AuthenticateUser);
						Invoke(authHandler, new object[] {state.Uri, authHeader.AuthenticationType});
					} else {
						LoggingService.Error("DiscoveryCompleted", ex);
						Invoke(handler, new object[] {null});
					}
				}
			} 
		}
		
		/// <summary>
		/// Stops any outstanding asynchronous discovery requests.
		/// </summary>
		void StopDiscovery()
		{
			lock (this) {
				if (discoveryClientProtocol != null) {
					try {
						discoveryClientProtocol.Abort();
					} catch (NotImplementedException) {
					} catch (ObjectDisposedException) {
						// Receive this error if the url pointed to a file.
						// The discovery client will already have closed the file 
						// so the abort fails.
					}
					discoveryClientProtocol.Dispose();
				}
				discoveryClientProtocol = new WebServiceDiscoveryClientProtocol();
			}
		}

		void AddWebReferenceDialogFormClosing(object sender, FormClosingEventArgs e)
		{
			StopDiscovery();
		}
		
		protected override void OnShown(EventArgs e)
		{
			base.OnShown(e);
			urlComboBox.Focus();
		}
		
		ServiceDescriptionCollection GetServiceDescriptions(DiscoveryClientProtocol protocol)
		{
			ServiceDescriptionCollection services = new ServiceDescriptionCollection();
			protocol.ResolveOneLevel();
			
			foreach (DictionaryEntry entry in protocol.References) {
				ContractReference contractRef = entry.Value as ContractReference;				
				if (contractRef != null) {
					services.Add(contractRef.Contract);
				}
			}
			return services;
		}
		
		void DiscoveredWebServices(DiscoveryClientProtocol protocol)
		{			
			if (protocol != null) {
				addButton.Enabled = true;
				namespaceTextBox.Text = GetDefaultNamespace();
				referenceNameTextBox.Text = GetReferenceName();
				webServicesView.Add(GetServiceDescriptions(protocol));
				webReference = new WebReference(project, discoveryUri.AbsoluteUri, referenceNameTextBox.Text, namespaceTextBox.Text, protocol);
			} else {
				webReference = null;
				addButton.Enabled = false;
				webServicesView.Clear();
			}
		}
		
		void UrlComboBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			BrowseUrl(urlComboBox.Text);
		}
		
		void UrlComboBoxKeyDown(object sender, KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter && urlComboBox.Text.Length > 0) {
				BrowseUrl(urlComboBox.Text);
			}
		}
		
		void AddWebReferenceDialogResize(object sender, EventArgs e)
		{
			int widthChange = Width - initialFormWidth;
			urlComboBox.Width = initialUrlComboBoxWidth + widthChange;	
		}
		
		void AddButtonClick(object sender,EventArgs e)
		{
			try {
				if (!IsValidReferenceName) {
					MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.InvalidReferenceNameError}"));
					return;					
				}
				
				if (!IsValidNamespace) {
					MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.InvalidNamespaceError}"));
					return;										
				}
											
				webReference.Name = referenceNameTextBox.Text;
				webReference.ProxyNamespace = namespaceTextBox.Text;
				
				DialogResult = DialogResult.OK;
				Close();
			} catch (Exception ex) {
				MessageService.ShowError(ex);
			}
		}
		
		void AddImages()
		{
			goButton.Image = ResourceService.GetBitmap("Icons.16x16.RunProgramIcon");
			refreshButton.Image = ResourceService.GetBitmap("Icons.16x16.BrowserRefresh");
			backButton.Image = ResourceService.GetBitmap("Icons.16x16.BrowserBefore");
			forwardButton.Image = ResourceService.GetBitmap("Icons.16x16.BrowserAfter");
			stopButton.Image = ResourceService.GetBitmap("Icons.16x16.BrowserCancel");
			Icon = ResourceService.GetIcon("Icons.16x16.WebSearchIcon");
		}
		
		void AddStringResources()
		{
			Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.DialogTitle}");
			
			refreshButton.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.RefreshButtonTooltip}");
			refreshButton.ToolTipText = refreshButton.Text;
			
			backButton.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.BackButtonTooltip}");
			backButton.ToolTipText = backButton.Text;
			
			forwardButton.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.ForwardButtonTooltip}");
			forwardButton.ToolTipText = forwardButton.Text;

			referenceNameLabel.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.ReferenceNameLabel}");
			namespaceLabel.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.NamespaceLabel}");

			goButton.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.GoButtonTooltip}");
			goButton.ToolTipText = goButton.Text;
			
			addButton.Text = StringParser.Parse("${res:Global.AddButtonText}");
			cancelButton.Text = StringParser.Parse("${res:Global.CancelButtonText}");
			
			stopButton.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.StopButtonTooltip}");
			stopButton.ToolTipText = stopButton.Text;
				
			webServicesTabPage.Text = StringParser.Parse("${res:ICSharpCode.SharpDevelop.Gui.Dialogs.AddWebReferenceDialog.WebServicesTabPageTitle}");
			webServicesTabPage.ToolTipText = webServicesTabPage.Text;
		}
		
		void AuthenticateUser(Uri uri, string authenticationType)
		{
			DiscoveryNetworkCredential credential = (DiscoveryNetworkCredential)credentialCache.GetCredential(uri, authenticationType);
			if (credential != null) {
				StartDiscovery(uri, credential);
			} else {
				using (UserCredentialsDialog credentialsForm = new UserCredentialsDialog(uri.ToString(), authenticationType)) {
					if (DialogResult.OK == credentialsForm.ShowDialog()) {
						StartDiscovery(uri, credentialsForm.Credential);
					}
				}
			}
		}
		
		void AddCredential(Uri uri, DiscoveryNetworkCredential credential)
		{
			NetworkCredential matchedCredential = credentialCache.GetCredential(uri, credential.AuthenticationType);
			if (matchedCredential != null) {
				credentialCache.Remove(uri, credential.AuthenticationType);
			}
			credentialCache.Add(uri, credential.AuthenticationType, credential);
		}
	}
}

⌨️ 快捷键说明

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