|
This article builds upon the “Integrating an MDI application using C#” article using the code from that article as a basis for this one.
Web applications can be integrated into .Net applications in the same fashion as Windows applications.
The attached code adds the integration of two web applications to the toolbar. One is a sample web application hosted at the http://training.openspan.com website which allows a user to use a HTML menu to go to a search web page and look for a store nearest to an address. The other web application is the Google Map web application. Code is provided for doing the same search operation with Bing’s Map application.
The main points of using OpenSpan with .Net demonstrated here are:
- Starting and stopping an application using an adapter
- Dealing with multi-threading issues from application events
- How to wait for a web application’s controls to fully load before using them.
First we added two buttons and a text control to the toolbar so we could work with the web applications. One is for finding an address while the other is to use Google Maps to get directions. The toolbar now looks like:

Then we add two web application adapters. One is for http://training.openspan.com and the other is http://maps.google.com which we then interrogate. We’ll skip the interrogation since that is the same as the basic OpenSpan Studio product but you can open the adapters to see the controls that are being used.

You need to add an additional reference for the .Net project to work with the OpenSpan libraries. This is done by right-clicking References and selecting Add Reference and you add OpenSpan.Adapters.Web.dll.
Starting the applications
There is an important change in how the application is started compared to the earlier project.
Crm = new OpenSpanIntegration.CRM();
...
// Starts the external application Crm.Start( this ); }
The Start()method can take a parameter which provides a synchronization object. This object must implement the ISynchronizeInvoke interface. In practical terms, you pass it a Winform UI object such as a Form or a control. Doing this allows the OpenSpan adapters to automatically marshal their events to your UI so you don’t have to.
You only need to do this for adapters which you handle events from and that the event handlers modify UI items. If either of those conditions are not true, then you don’t need to pass an object to the Start() method.
Working with the Web Applications
One of the features of using OpenSpan is that it makes applications developed with different technologies appear the same to your code. For instance, working with lists, buttons and text boxes is largely the same for Win32, VB, .Net, Siebel, PowerBuilder, Web and other applications.
However, in the case of web applications, you do need to check if a web page or control has loaded in the browser before trying to use it. OpenSpan matches the control when it is fully loaded and then fires a Created event and sets a Boolean indicating the control is created. There are a couple of ways of checking for matched controls on a web page.
One is a simple check for the control being already matched called IsCreated() which returns true or false depending if the control has been matched (created in the web page).
Another way is to wait for the control or web form to be created (that is matched, yes the two terms get interchanged a bit). This is done with the WaitForCreate() method which optionally takes a timeout value.
Finally, you can also set up an event handler for the control being created called, yes, Created and then do what you want to do within the event handler.
While this isn’t used here, there is also a Destroyed event for indicating when a control is no longer matched.
We need to start the applications if they are not already running. Here we create new instances of the adapters and start them. We don’t need a synchronization object since we aren’t handling any events from these two web applications.
Immediately after starting the application, you can see where we wait for the Login screen to appear. It has a timeout in milliseconds so we can control how long to wait. In other circumstances, we could have used the Created event and whenever the Login screen appeared, we could just perform the login automatically.
protected OpenSpanIntegration.CrmWeb CrmWeb = null;
private void FindStore_Click( object sender, EventArgs e ) { // Start the web application if ( CrmWeb == null ) CrmWeb = new OpenSpanIntegration.CrmWeb();
if ( !CrmWeb.IsRunning ) { // No need for synch object since we aren't handling any adapter events. CrmWeb.Start(); if ( !CrmWeb.Login.WaitForCreate( 8000 ) ) { MessageBox.Show( "Login screen not found." ); } else { CrmWeb.Login.Enabled = true; CrmWeb.Login.PerformClick(); } }
After the Login screen click, we wait for the StoreLocatorLink to appear on the web page and then clicked thru it and waited for the next screen to appear. The next screen has a submit button on it so we waited for that control to be created.
if ( CrmWeb.StoreLocatorLink.WaitForCreate( 4000 ) ) { CrmWeb.StoreLocatorLink.PerformClick(); }
if ( CrmWeb.btnSubmit.WaitForCreate( 4000 ) ) { CrmWeb.txtZip.Text = Zipcode.Text;
CrmWeb.btnSubmit.PerformClick();
if ( CrmWeb.FullAddress.WaitForCreate( 5000 ) ) { NearestStore.Text = CrmWeb.FullAddress.Text; Update(); } } }
When a user presses the Get Directions button on the toolbar, we start the Google web application, which loads their map directions web page. The rest of this is just like what we’ve already done except in the sample below, you can see how to hide the web browser’s toolbar and address bar to make for a cleaner look to the resulting map.
The value OpenSpan.Adapters.Web.WebAdapter.WebBrowserShowToolbar is a bit wordy and you can simplify it with a C# using statement, but you should also notice that it’s not a Boolean true/false but instead its true/false/default where the default is whatever setting the browser is using as you can globally turn off these bars.
protected OpenSpanIntegration.GoogleWebMap GoogleWebMap = new OpenSpanIntegration.GoogleWebMap();
protected void GetGoogleDirections() { if ( GoogleWebMap == null ) { GoogleWebMap = new OpenSpanIntegration.GoogleWebMap();
// Hide the tool and address bars GoogleWebMap.AddressBarVisible = OpenSpan.Adapters.Web.WebAdapter.WebBrowserShowToolbar.False; GoogleWebMap.ToolBarVisible = OpenSpan.Adapters.Web.WebAdapter.WebBrowserShowToolbar.False; }
This shows a few techniques used when using OpenSpan’s integration within a .Net project. The attached solution can be used to experiment with this further.
|