Iuis and web-style navigation in Windows Forms, Part 2

xiaoxiao2021-03-06  69

Download the winforms09212004_sample.msi file.

WHERE WERE WE?

In last month's installment, we saw how return on investment (ROI) is an important consideration to make with regard to an application's user experience. Higher ROI is generally achieved as a result of performing tasks more frequently and, consequently, being forced to relearn less the next time they are performed Lower ROI, on the other hand, results from less frequent tasks that require users to spend more time relearning them to demonstrate, we identified the Northwind database's Regions table as potentially supporting the following infrequently used tasks..:

Adding a new region renaming another moving territories from One Region To Another

We then discussed two types of UI designs-deductive and inductive. Generally speaking, the former puts the onus on the user to manage and learn a task, while the latter takes on the onus to guide the user through a task. The latter consequently turns out to be a great UI design choice for infrequently used tasks, particularly if they incorporate web-style navigation support. Jan Miksovsky's (Microsoft, Longhorn User Experience Team) work in the IUI space includes the development of a navigation library, Microsoft.Samples. Windows.Forms.Navigation, that allows Windows Forms developers to more easily develop web-style IUIs. We began using this library by creating a customized Northwind Region Manager explorer-style host simply by deriving from Microsoft.Samples.Windows.Forms.Navigation. Explorer. The Resulting RegionExplorer IS Shown in Figure 1.

Figure 1. Stylized RegionExplorer Browser Host

In this installment, we'll continue utilizing the Navigation library to convert RegionExplorer from an empty shell into a fully functional Northwind Region Manager, illustrated in Figure 2.Figure 2. Northwind Region Manager

As we saw in part 1, Explorer (and RegionExplorer, by way of derivation) is a lightweight UI built on top of a complete navigation framework. The real work is performed by the Frame type, which handles navigation to and between one or more pages (like a Web applications). The nature of the Navigation library's infrastructure is such that all we need to do to build the Northwind Region Manager is build the pages themselves and leave the underlying internals to take care of the rest. We'll need to Build One Page for Each Step in Each of The Designated Tasks, As Well as The Home Page.

Building the Home Page

Because the home page is the first page that will be navigated to, it seems a natural place to start. Whereas Windows Forms derive from System.Windows.Forms.Form, our IUIs pages will derive from Microsoft.Samples.Windows.Forms.Navigation .Page:

Public Class HomePage:

Microsoft.samples.windows.Forms.naVigation.page {...}

Because Page itself derives from System.Windows.Forms.UserControl, instead of manually creating and coding up a class, we can create a new page using the designer by following these steps (making sure the Navigation library is referenced by your project):

In Visual Studio .NET, open Solution Explorer Right-click on the appropriate project Click Add |.... Add Inherited Control In the Templates list, select Inherited User Control Enter a suitable name, like HomePage.cs Click Open, which opens the Inheritance Picker dialog. Select Page as the base implementation you'd like to inherit. Inheriting from UserControl also means that your page can be visually and programmatically designed, just like a form. Figure 3 shows the home page with appropriate controls.Figure 3. Homepage, Derived from Page, In The Windows Forms Designer

The Controls I'VE Used to build the page include:

A PictureBox for the enhanced region image A Label for the page title A ListBox to list currently active regions One LinkLabel to browse the Help page, and to browse to the first page of each of the identified tasks

The LinkLabel is used instead of traditional command buttons to keep the Web-style experience as familiar and consistent as possible with a Web application. The actual LinkLabel I've used is provided by the Navigation library, which derives from System.Windows.Forms. LinkLabel and is enhanced to ensure active link appearance (underlined and colored with the ActiveLinkColor) when the mouse both hovers over it and clicks it, the latter only being supported by the LinkLabel control.

Running the Home Page

Once created, you'll need to instruct your application on which page to load into RegionExplorer upon execution. This turns out to be a case of creating an instance of the RegionExplorer host and passing it an instance of the home page. The base Microsoft. Samples.windows.Forms.naVigation.Explorer Class Happens To Implement A Constructor Overload Designed to Assist US in this Very Endeavor: Namespace Microsoft.samples.windows.Forms.navigation {

Public class explorer: system.windows.forms.form {

Public explorer () {...}

Public explorer (page homepage): this () {...}

}

}

RegionExplorer Also Acquires The Same Capability Because, As a DeriVation, IT Must Re-Implement ITS Base Class's Constructors IF overloaded:

Namespace winforms08172004_regionManagersample {

Public Class RegionExplorer: Explorer {

Public regionExplorer (): base () {...}

Public RegionExplorer (Page Home): Base (HomePage) {...}

}

}

With this in place, Opening RegionExplorer with homepage WHEN The Application Starts is Simply a Matter of Taking Advantage Of The Constructor Overload from WtemPlication's Static Entry Point:

Public class entrypoint {

[Stathread]

Public static void main () {

// Create Home Page and Pass It To Explorer As The Home Page

Page homepage = new homepage ();

RegionExplorer EX = New RegionExplorer (HOMEPAGE);

// set regionExplorer Title Bar Text

EX.ApplicationName = "Northwind Region Manager";

// Run Application

Application.run (ex);

}

}

Figure 4 shows homepage running within a hidden frame, itself hosted by regionExplorer.

Figure 4. RegionExplorer Hosting HomePage (WITHIN A Hidden Frame)

Note All Pages Will Be Shown With Frame As Figure 4 Highlights.

Page Navigation

A home page is typically a base of either completed successfully or cancelled, users are typically navigated back operations from which the real tasks are initiated. Users initiate tasks by clicking the appropriate link label and being navigated to the appropriate page. When a task is to the home page (or whichever page they initiated a task from). Microsoft.Samples.Windows.Forms.Navigation.Page offers a several methods that, through your derived page classes, allow you to programmatically support forward, back, and home navigation.

Navigating to a static page

We'll Begin by looking at the MOST SIMPLE EXAMPLE OF Navigation, Which Is Navigating to Another Page Such As a Help Page, Show in Figure 5.

Figure 5. Static Help Page

Page Navigation Can Most Easily Be Implement by Call Page.go from your derived page. Homepage does this to navigate to the help page:

Public class homepage: Microsoft.Samples.windows.Forms.naVigation.page {

...

Void helplink_linkclicked

Object sender, LinkLabellinkClicKedEventArgs e) {

THIS.GO (TypeOf (Helppage));

}

...

}

You might by wondering why we are passing a type instead on an actual object instance to Go. This method call implicitly requests the Navigation library to create and treat the type being passed as a static page. Just as in Web applications, the Navigation library makes the distinction between static and dynamic pages. static pages, like our simple help page, look the same from one browse to the next. Because their content does not change, there's no need to repeatedly create new instances of them and sacrifice application performance. The Navigation library supports this by creating a single instance of the page type you specified and adding it to an internally-managed list of static pages. internally, Go creates a new instance of the type specified, adds it to the internal list, and browses To it. Subsequent calls to go open the existing instance held in the inload.navigating to a dynamic page

On The Other Hand, Some Pages Will Hand Content That Differs from One Navigation To The Next, Such As RenameRegionPage, Which Displays The Name of The Region Being Renamed, Shown in Figure 6.

Figure 6. Dynamic Rename Region Page

The Navigation Library Caters for Dynamic Pages with an offload of the Go Method That You Pass a page instance to:

Public class homepage: Microsoft.Samples.windows.Forms.naVigation.page {

...

Void RenameRegionLink_LinkClick (

Object sender, LinkLabellinkClicKedEventArgs e) {

THIS.GO (New RenameRegionPage (GetSelectedRegion),

GetSelectedRegionName ()));

}

...

}

In this sample, we are not only creating our own page instance, we are also passing it data that is unique to this instance. Just as with static pages, the Navigation library retains and shows this instance as users navigate back or forwards through history. And, Because Each Dynamic Page Instance Retains Appropriate State Data, You Can Be Assured That Performs The Same Task It Was Originally Instantiated to DO.NAVIGATING AWAY From A Task without ending

You might be wondering why the previous code sample does not retain a reference to the new RenameRegionPage instance. You'd see this in typical Windows Forms applications when main forms use dialogs to gather data that are actually processed by the main form when accepted by a user. RegionManager eschews this technique because the free-form navigation supported by Web-style applications leads to possibilities where, in fact, several instances of the same task page can be accessible through navigation history, as shown in Figure 7.

Figure 7. Multiple RenameRegionPage Instances in Navigation History

This is a cumulative result of various combinations of navigation, such as clicking links and navigating through history, without explicitly ending any tasks by clicking either the OK or Cancel buttons for example. Leaving a task incomplete is entirely valid and users should be able to navigate back to those pages to complete the desired tasks. After all, this is a great feature of Web-style navigation. However, in the face of multiple page instances, it quickly becomes difficult for the home page to remember which page instance the user is navigating back from. As such, it is easier to employ a fire-and-forget approach that relies on page instances to manage and respond to changes in their own state as appropriate, like renaming a region. This is necessarily different to the more traditional Main Form / Dialog Pattern Generally Found In Windows Forms Applications That Relies On Dialogs To Gather State Changes and The Main Form To Respond To Them.explicitly Ending A Task

When users do finally explicitly end a task, the responsibility for navigating them appropriately is up to you. If your IUI has only a single task, of which Wizards are great examples, it makes sense to completely finish the task. However, if your IUI is more like RegionExplorer and supports multiple tasks, navigating users back to the home page to begin other tasks is more suitable. As you might expect, Microsoft.Samples.Windows.Forms.Navigation.Page also offers the ability to navigate back to the home Page:

Public Class RenameRegionPage:

Microsoft.samples.windows.Forms.naVigation.page {

...

Void RenameRegionButton_Click (Object Sender, Eventargs E) {

...

// rename region

...

THIS.GOHOME ();

...

}

Void CancelButton_Click (Object Sender, Eventargs E) {this.gohome ();

}

...

}

GoHome instructs the host Frame to load the home page, the page instance stored in its HomePage property. Explorer also calls its Frame's GoHome method when the Home navigation toolbar button is clicked. Note that while the home page is a page instance you pass to Explorerà ,.............................

Navigation History

One of the benefits of combining Web-style navigation with the IUI concept is it allows users to navigate backwards and forwards through the current task until appropriately complete, or backwards and forwards through several tasks. Such functionality is natively supported by Frame and exposed using the Page type through the GoForward and GoBack methods. Calls to either of these functions instruct the host Frame instance to navigate to the next or previous Page in its backwards and forwards navigation stacks or histories. Explorer calls the Frame versions directly when either Back or Forwards navigation Toolbar Buttons Are Clicked:

Public class explorer: system.windows.forms.form {

...

Void Goback_Click (Object Sender, Eventargs E) {

Frame.goback ();

}

Void goforward_click (Object sender, eventargs e) {

Frame.goforward ();

}

...

}

Both GoBack and GoForward have an overload that allows a caller to specify a number of pages to move back or forwards through the navigation history. The benefits of navigation are plainly obvious to anyone who has used a Web browser, and the experience is comparable, as SHOWN IN Figure 8.

Figure 8. Navigating Back to the Home Page

While RegionExplorer Doesn't Need To Call Either, You May NEED TO Contemim from Scratch.removing a Task Page On Task End

While there is a benefit associated with back and forwards navigation support, there is also a potential issue that stems from the fact that you are navigating back to page instances that may have been built dynamically to reflect specific piece or set of data. For example, IF a User Deletes Region "1," THEN BROWS BACK TO A RENAME Region Page Created for region "1" and Tries To Rename IT, A SQL Server Exception Will Occur Since Region "1" Obviously Doesn't exist. THE Simplest Way To avoid confusing users and causing exceptions is to prevent them from performing a task they should not be able to perform. As it turns out, the Page type implements a CanEnter method that you can override to report whether or not this page instance can be re -BROWSED TO:

Public Class RenameRegionPage:

Microsoft.samples.windows.Forms.naVigation.page {

...

BOOL _COMPLETED = FALSE;

...

Public Override Bool Canenter (ViewState ViewState) {

Return! _completed;

}

Void RenameRegionButton_Click

Object sender,

System.Eventargs e) {

...

// prevent further renaming

_Completed = true;

...

}

...

}

When you browse back to this page, but before it is loaded into the host Frame, the host Frame itself calls CanEnter. If CanEnter returns a value of true, the host Frame completes the page navigation and displays it. If CanEnter returns false, however , The Host Frame Removes The page instance from the navigation to navigate the navigate back until it, one page instance what can be be loaded.page template

With great navigation support comes great responsibility. One such responsibility is to ensure a consistent user experience across your pages, which helps to provide a warm and happy context for your users. In Windows Forms, you can use visual inheritance to ensure all forms have the same general look and feel by creating a base form with the common visual elements and having subsequent forms derive from it to inherit them. Because Page derives from UserControl, we can take advantage visual inheritance by creating a page template with the elements that are common to the Northwind Region Manager IUI, including the region image, the page title and the help link. This involves creating PageTemplate in the same manner we used to create the HomePage and dropping on and configuring the appropriate controls to leave us with Figure 9.

Figure 9. Northwind Region Manager Page Template

It's pretty easy to determine that the only designable area, that is the only area where developers can actually place controls and configure them, is the area on the bottom right. We can enforce these rules with visual inheritance by setting the access modifiers on both the region image PictureBox and the Panel that contains both page title and help link to Private, thereby preventing derivations from altering them. Of course, developers should be able to actually change the page title Label's text from page to page. Allowing them to do so involves Creating An Override of the Text Property In PageTemplate: Public override string text {

SET {

Base.text = value;

IF (PageTitle == Null) {

PageTitle.hide ();

}

Else {

PageTitle.Text = Value;

PageTitle.show ();

}

}

}

PageTemplate Also Implements A Showhelp Property That Allows Developers to set WHETHER THE HELP LINK IS Shown or Not (It Wouldn't Make Sense To Show It On A Help Page, for Instance):

Bool showhelp {

Get {return this.helplink.visible;

Set {this.helplink.visible = value;

}

Subsequent Application Pages, Such as The HomePage, Should Now Derive from PageTemplate:

Public Class HomePage: PageTemplate {...}

The designer behaves a little differently when a visually inherited form is open, by using special icons to signify derived visual elements and preventing property changes of any controls set with Private modifiers, illustrated by Figure 10.

Figure 10. Visually Inherited HomePage

Each Page In The Northwind Region Manager Derives from PageTemplate and I'll Leave It Up You To Further Explore This Aspect of The Implementation.

WHERE area

In this installment, we utilized Jan Miksovsky's Navigation library to build the Northwind Region Manager as a Web-style inductive user interface. We saw how to derive from Page to build your custom pages, as well as using a page template to reduce development effort and ensure a consistent look and feel across the application. We then covered a variety of scenarios enabled by the Navigation library. Please take time to explore the Navigation library's sample, especially as it covers scenarios beyond the scope of this installment, including the use of view State.sample installation

If you do not have SQL Server or the SQL Server version of the Northwind database on which the sample is based, the sample includes a readme file (MSDE2000NorthwindDbInstallReadMe.html) that outlines how you can install the free Microsoft SQL Server Desktop Engine 2000 and Northwind database, as well as how updating the sample solution to accordingly. Note that the Northwind Region Manager does not support deleting the entire set of regions as doing so would introduce functionality beyond the scope of this article.

Acknowledgements

Thanks to Chris Sells for Introducing Me To Jan and His Work, for Asking Me To Write About It, And for Continuing To Not Let Me Offe Writing Hook.

转载请注明原文地址:https://www.9cbs.com/read-114529.html

New Post(0)