The "longhorn" Application Model

xiaoxiao2021-03-06  54

Chapter 1: The "longhorn" Application Model

Brent Rectorwise OWL Consulting

October 2003

Contents

Features of the "longhorn" Application ModelThe Application Classthe NavigationApplication Classextensible Application Markup Language (XAML) Summary

Why Do We NEED A NEW Application Model? One Major Reason Is To Bridge The Gap Between Developing An Application for Microsoft® Windows® and Developing An Application for The Web.

Today when you write a Windows application, you can take advantage of Windows features. Your application can provide a rich, responsive user interface. You can install the application on the client computer, which allows the application to run offline, without a network connection. Windows Applications CAN Take Advantage of The Hardware Capabilities of The Client Computer.

However, traditional Windows applications also have a number of drawbacks. You typically must install a Windows application. This makes the application and any updates hard to deploy. Windows applications do not run in the browser. Therefore, familiar Web UI paradigms such as page -oriented applications, navigation directly from one page to another, page history, and more are not available to your application unless you create them from scratch. Windows applications also do not support text very well, especially when you try to mix text and graphics on the same page. Creating a Windows application that automatically flows text around graphics and that responds to user-initiated changes in the window size and user preferences for fonts and readability is a huge amount of work.

Web applications have their own distinct strengths as well. When you browse to a Web page, the browser downloads only that page and the components the page requires. When you navigate to a new page, the browser then downloads the new page's requirements. In other words, the browser progressively downloads the application as needed.Deployment of a Web application is trivial. What deployment? You place the necessary application components on a server, and the browser downloads them as needed. There's no deployment per se.

Creating the UI for a Web application is also quite easy. You declare your intentions using markup. For example, suppose I want a table in a specific position. I want an image to follow the table. I want some text to flow around the image TEXT, GRAPHICS, AND Media (Sound and Video) IN A Web Application IS StraightForward.

. Of course, Web applications also have their bad points You can not install a Web application on the desktop system;.. Therefore, the application can not run offline You must always have a connection to the server Certain application operations require roundtrips to the server , and that lessens performance. A Web application selection of controls is quite primitive in comparison to the available Windows controls. A Web application therefore typically has poor interactivity. It's also hard to develop an attractive UI for a Web application because you must express any non - Trivial Layout Using Tables.

Currently, developers designing new applications need to make an initial, huge, irreversible decision: should the application be a Web-style application or a classic Microsoft Win32® application Completely separate programming models (and skills!) Are needed depending on which application model? you choose. "Longhorn" allows you to develop applications using the best of both worlds. The "Longhorn" application model takes the best features of Web applications and the best features of Windows applications and combines them in a single unified programming model based on managed Code.

A second major reason for developing a new application model is to provide a single programming model that can create the wide variety of "applications" in use today. Look at one of your favorite Web sites, such as CNN or MSNBC. Is the Web site A Traditional Application? Is IT A Document? IS IT A MULTIMEDIA Presentation? In Many Cases, The Answer Is Yes To All Three Questions.

When a Web site includes UI elements such as list boxes, edit controls, and radio buttons, it looks like an application presenting a UI. However, when it displays images and text flowing around the images, the Web site is similar to a document. When IT Presents Flash Content, Graphics, Audio, Video, And Animation, The Web Site Seems To Be a MultiMedia Presentation.

Of course, such rich Web sites are difficult to develop. You need to patch together an HTML markup-based description of the page, with Microsoft ActiveX® controls for a rich UI, with embedded Flash animations, and possibly using Portable Document Format (PDF ) for document support. All these technologies use a different architecture, provide different performance characteristics, and require different programming models and tools.This typically means that you must hire multiple developers with different skill sets to develop each portion of the application. The developers must ..... debugging it is often a nightmare.. Debugging it............

"Longhorn" provides a unified architecture that supports these three tiers-documents, applications, and media. Do you want to create your UI declaratively using markup? Go for it. Do you need to use rich Windows controls? Then do so! Do you want to write event handlers in a strongly typed, managed language? you can do that as well. do you want to mix text, graphics, and video in a document with intelligent layout and presentation according to the user's preferences and optimized for best viewing and Reading on the client system? Guess what? you get what Too.

The "Longhorn" application model makes it possible to write an application using a single programming model that supports application-style UI functionality, document-style presentation of text and graphics, and integration of various media. In addition, you can create the UI using markup like a Web application. you also get the deployment (or lack of deployment) ease of a Web application. However, you still have the performance and ability to install the application for offline use like a Windows application. Your application can run as a stand-alone application or hosted in a Web browser by simply recompiling one source code base. in either case, your application can be form-based, like many traditional Windows applications, or page- based, like Web applications.Features of the "Longhorn Application Model

The "longhorn" Application Model Defines What An Application IS:

ITS Entry Points Its Flow of Control-How To Navigate from One Page To Another ITS Shared State and Resources Application-Wide Events ITS ISOLATION from Other Applications

The "longhorn" Application Model Defines How to Deploy and Maintain An Application:

Deployment As Single Or Multiple Files Update, Rollback, And Administration

The "longhorn" Application Model Defines The user's experience with the application:

ZERO-IMPACT Installation Stand-Alone (Windows-Style) or Integrated In Browser Runs Online or Offline Navigation Model

"Longhorn" Web Applications

The "Longhorn" application model allows you to write a rich application similarly to the way you write today's Web applications. This provides an easy migration path for Web developers, as the code they write is similar to the code for dynamic HTML (DHTML) Web pages. They can (shudder) place markup and script in same file. They can deploy the files for the application to a Web server. The application pages run in the Web browser.However, the object model for a "Longhorn" Web-style application is much simpler and far more powerful than DHTML. The application code can use the complete "Longhorn" presentation layer. Therefore, a "Longhorn" Web application can use rich client controls, support multimedia and graphics on the page, handle events locally- basically everything a normal client application might do In fact, a "Longhorn" Web application is not much different from a "Longhorn" desktop application other than that the files live on a server;. a browser typically, but not necessarily, Hosts The UI; and The Application Runs with Restricted Permissions Because The User Hasn't Installed It on The Client System.

"Longhorn" Desktop Applications

The "Longhorn" application model also defines how to write desktop applications. A "Longhorn" desktop application is an application that the user has installed locally. Such applications can run online or offline. Such applications can register with the shell, place icons on the Desktop, add shringcuts to the start menu, and more.

A desktop application can also run in the browser window or in a stand-alone window In fact, a desktop application can support many features traditionally associated with a Web application, including the following:. Explicitly define external entry points-that is, can start ON Any Page Share State Across Pages Handle Various Events, Including Page Navigation Events Control Application Flow Add / Remove Entries from a page history / travel navigation log launch coplication window

Building a "longhorn" Application

To build a "Longhorn" application, you define the object model for your application. You can define the model programmatically by writing code or declaratively by writing markup in a language called the Extensible Application Markup Language (XAML). You compile your code and / Or Markup Into One or More .NET Assembly, An Application Manifest File, And A Deployment Manifest File.

Optionally, you can package your application INTO A New File Format, Called a Container. The Application Files In a Conta Be Be Compressed, Encrypted, And Digitally Signed.

I discuss building a "Longhorn" application in detail in Chapter 2, but for now the main idea is that building a "Longhorn" application gives you the application's code, an application manifest that describes all the components that the application uses, and a deployment Manifest Tells the system how to install and maintain the application.

Deploying a "longhorn" Application

The "Longhorn" application model provides for easy, cost-effective deployment of your application. In the simplest case, you simply copy the application files to a server. Similarly, installing your application is straightforward and non-impactful.One option is not to install the application at all. The user can browse to the application manifest on a server and run it. "Longhorn" incrementally downloads your application and executes it. You get no confirmation demands, no reboot requirements, and no DLL hell. In fact, You do Not Even NEED Administrator Rights to Install or Run the Application.

Alternatively, the user can browse to the application's deployment manifest on the server and run it. "Longhorn" incrementally downloads your application, installs it, and executes it. By default, all "Longhorn" applications run in a limited permission environment called the Secure Execution Environment (See).

Applications running in the SEE receive a restricted permission set that is roughly equivalent to the permissions granted to today's applications associated with the Internet zone. An application that requires additional permissions than "Longhorn" provides by default must request those additional permissions in its application manifest.

The first time the user runs such an application, the "Longhorn" Trust Manager will evaluate the elevated permission request, notify the user of a suggested risk level associated with granting the application's permission request, and provide a suggested response for that risk level. When the user permits the Trust Manager to grant the application its requested permissions, the Trust Manager records this information. Subsequent executions of the installed application proceed without the security warning.Today, when you install an application locally, it receives the FullTrust permission set simply because it loads from the LocalComputer zone. Code Access Security (CAS) works differently for "Longhorn" applications. A local (or installed) application runs under the security policy of the site from which the user downloaded it instead of automatically receiving FullTrust simply because it IS installed locally.

When Loading An Application, ITS Components, And ITS Resources, "Longhorn" Province Evidence To The Common Language Runtime (CLR) Security System Such AS

Internet Zone and Site of Origin (from The Uniform Resource Identifier [URI]) Publisher and Module Name (from the deployment manifest)

CAS The Provides Security Policy-based Enforcement Over Access Privileges based on the application's evithne.

The deployment manifest for an application can specify the update interval that "Longhorn" should use when checking for a new version of the application. When "Longhorn" detects that a new version is available, it downloads and installs the new version in the background. The next Time The User Runs The Application, She Receives The New Version.

When installing an application, "Longhorn" preserves the previous version, if any. Should you need to, you can painlessly roll back to the previous version or even completely uninstall the application using Add / Remove Programs. IT departments can push the installation of an application to a client system for a hands-free deployment.You specify how to deploy the application when you compile the project, and you can change the deployment scenario by recompiling, typically with few or no changes to your source code.

A Developer's Program Initially Interacts with Much of The "Longhorn" Application Support Via An Instance of The Msavalon.Windows.Application Class, So Let's Look At That Class.

The Application Class

A "Longhorn" program always contains a single instance of an application object This object derives directly or indirectly from the MSAvalon.Windows.Application class and performs the following functions.:

Provides an entry point, encapsulation, and scope for the application Allows an application to share code and state across the pages that make up the application Provides application-level events Maintains a collection of the application's windows Provides a security model Defines any resources that the application Uses

The MSAvalon.Windows.Application class provides basic application support to an application. You typically use it when your application needs low overhead and does not use page navigation features. However, most "Longhorn" platform applications use the closely related MSAvalon.Windows.NavigationApplication class, which inherits from MSAvalon.Windows.Application and adds support for navigation. I'll discuss the NavigationApplication class in detail in the following section. You will typically define a class that inherits the appropriate base class, overrides base class methods as necessary, and then registers for events to provide custom startup or shutdown procedures.The SimpleApplication1.cs source file listing, shown here, demonstrates using the Application object. The EntryClass.Main method creates my specialized application object, MyApp, and calls its Run method to launch The application. The myapp Class Overrides the onstartingup method, Which Receives Control When the Applicatio N is starting up. When the system invokes the onstartingup method, i call a helper method That creates the application's main window, adds some text to the window, and displays the window.

SimpleApplication1.cs

Using system;

Using msavalon.windows;

Using msavalon.windows.Controls;

Using msavalon.windows.media;

Namespace Introlonghorn {

Public class myapp: msavaLon.windows.Application {

Msavalon.windows.controls.simpletext txtElement;

Msavalon.windows.window mainwindow;

Protected Override void onstartingup (StartingupCanceEventArgs E) {

Base.onstartingup (e);

CreateandShowMainWindow ();

}

Private void createandshowmainwindow () {

// Create the Application's Main Window

Mainwindow = new msavalon.windows.window (); // add a dark red, 14 point, "Hello World!" Text Element

TXTELEMENT = New msavaalon.windows.controls.simpletext ();

TXTELEMENT.TEXT = "Hello World!";

TXTELEMENT.FOREGROUND = New

Msavalon.windows.media.solidColorbrush (Colors.DarkRed);

TXTELEMENT.FONTSIZE = New FONTSIZE (14,

FONTSIZEPE.POINT);

Mainwindow.children.add (txtElement);

Mainwindow.show ();

}

}

INTERNAL Sealed Class entryclass {

[System.stathread]

Private static void main () {

Myapp app = new myapp ();

app.run ();

}

}

}

I used the follyapplication1.cs source code int to adjust the path to the reference asmble.

CSC /R: C :/Windows/MICROSOFT.NET/Windows/v6.0.4030/pesentationcore.dll

/r:c:/Windows / Microsoft.Net/Windows/v6.0.4030/pesentationframework.dll

/r:c:/Windows/MICROSoft.Net/Windows/v6.0.4030/windowsbase.dll

SimpleApplication1.cs

The Application class contains a number of other useful properties, methods, and events. For example, your application class can override the OnShuttingDown virtual method to provide custom shutdown behavior. The application class also provides the StartingUp and ShuttingDown events so that other classes can register .

You might want to reference your application object from multiple places in your source code. Therefore, the Application class provides the Current static property that returns a reference to your application object. The following code fragment uses the Current property to locate the application object and register For A Shutdown Event Notification: MyApp App = (MyApp) msavaalon.windows.Application.current;

app.shuttingdown = new

Application.shuttingDowneventhandler (ShutdownHandler);

§

Private static void

ShutdownHandler (Object sender, msavalon.windows e) {

§

}

The NavigationApplication Class

When you want navigation support for your application, you'll typically use the MSAvalon.Windows.Navigation.NavigationApplication class, which extends the MSAvalon.Windows.Application class. Although you can build a navigation-based application without using the NavigationApplication class, using The Class Provides The Following Additional Capabilities To your application:

Simplifies writing navigation-based applications; not usually necessary to subclass the class Determines when a connection is available Provides navigation events-such as Navigating, NavigationProcess, Navigated, NavigationError, LoadCompleted, and Stopped-which it fires when the appropriate event occurs in any of The Application's Windows SHARES State Across Pages Provides A Container for Property Values ​​Shared Across Pages Implements a Policy That Opens An Initial Window by Default

Externally, a navigation application's user can navigate only to well-defined entry points of the application. Internally, however, the developer controls navigation by hooking events. You can determine when a window or frame attempts to navigate to a new page and when the navigation is complete. You can cancel or redirect any navigation. You can find out the identity of the target page. You can handle navigation errors.The familiar navigation model makes an application easy to use. A navigation application provides behavior similar to the Web. Your application can use hyperlinks, provide Forward and Back buttons, display a Favorites list, and maintain a page History. The "Longhorn" NavigationApplication class and related classes provide all the support for such features.

A navigation application works whether online or offline, and it works the same whether a browser hosts the application or the application runs as a stand-alone. In addition, you have complete control over this Weblike behavior. You can customize the user experience as required ............................. ..

A navigation application typically creates one or more instances of the MSAvalon.Windows.Navigation.NavigationWindow class. The SimpleApplication2.cs listing, shown here, demonstrates a use of these classes. This listing is the same as SimpleApplication1.cs except that it uses the NavigationApplication and NavigationWindow Classes.

SimpleApplication2.cs

Using system;

Using msavalon.windows;

Using msavalon.windows.Controls;

Using msavalon.windows.media;

Using msavalon.windows.navigation;

Namespace Introlonghorn {

Public class myapp: msavalon.windows.navigation.naVigationApplication {

Protected Override void onstartingup (StartingupCanceEventArgs E) {

Base.onstartingup (e);

CreateandShowMainWindow ();

}

Private void createandshowmainwindow () {

// Create the Application's Main Window

Mainwindow = new msavalon.windows.navigation.navigationwindow ();

// Fill Window with ApproPriate Controls

§

// Show the window

Mainwindow.show ();

}

}

INTERNAL Sealed Class entryclass {

[System.stathread]

Private static void main () {

Myapp app = new myapp ();

app.run ();

}

}

}

The code you've seen up to now is just another variation on traditional programming models. The only new aspect is the actual classes I've used. Most of the time, however, you will not actually write much of this code. Let's Take a Slight Detour and Learn About a New Programming Language That Allows You To Write this Same Code In a Much More Compact and-To Me, At Least-More Understandable Manner.

Extensible Application Markup Language (XAML)

In many applications, much of the code you write pertains to creating and updating the UI of the application. In fact, in the previous examples, there was no code other than that required to create the UI. In the last few years, many developers have learned to write, and even to prefer to define, an application's UI by using one of a number of available markup languages ​​The "Longhorn" platform defines a new markup language named the Extensible Application Markup Language (XAML;. pronounced "Zamel," Which rhymes with "camel").

Using a markup language to define a UI has a number of advantages over using a procedural programming language These advantages include the following:. More apparent control hierarchies More apparent property inheritance Easier processing and interpretation of markup language by tools Potential separation of UI and procedural code

I like xaml, and i prefer to use it to define my uis rather Than Using the procedural-type coding i've shown you so far in this chapter. Ho's you'll becomi to do everything you ' Ll nesed to by using nothing but xaml.

Consider this statement from the documentation: "Documents can often be written entirely in XAML and displayed in the browser." I hastily point out that this sentence uses the word documents, not applications, and it qualifies the statement with the term often When you. 're writing a document that displays static content, you can create it in pure XAML. you can even write a document that uses data binding to display and update content from a data source by using nothing but XAML. you can define animations and mouse- OVER Effects by Using Nothing But Xaml. You Can Do A Heck of a Lot Using Nothing But Xaml. (in Fact, i try to do as much as possible in xaml and as little as possible in code. My Applications Seem to be Less Buggy and work more quickly the less code I write!) nevertheless, to write a production application, you'll typically need to react to events, provide custom decision logic, or include many other non-UI operations, so you'll need to mix Xaml and code. Fortunately, this is IS Extremely easy to do.

I'll Describe Xaml Files in More Depth in Chapter 3; For Now, Let's Look at a primer for xaml:

A XAML element name is a .NET Framework class name. When you define a XAML element, you are effectively creating an instance of the .NET Framework class with the same name as the XAML element. A XAML attribute name maps to the property or field With The Same Name, Typically In The Class Instance.in The SimpleApplication1.cs Program

// Create the Application's Main Window

Mainwindow = new msavaLon.windows.window ();

// Add A Dark Red, 14 Point, "Hello World!" Text Element

TXTELEMENT = New msavaalon.windows.controls.simpletext ();

TXTELEMENT.TEXT = "Hello World!";

TXTELEMENT.FOREGROUND = New

Msavalon.windows.media.solidColorbrush (Colors.DarkRed);

TXTELEMENT.FONTSIZE = New FONTSIZE (14, FontSizType.point);

Mainwindow.children.add (txtElement);

Mainwindow.show ();

The Following Xaml Document Produces Exactly This Same UI.

HelloWorld.xaml

Hello World!

The root Window element creates an instance of a class named MSAvalon.Windows.Window. Somehow, the build system needs to know that the XAML element named Window refers to an instance of the class named MSAvalon.Windows.Window. The xmlns attribute value provides This mapping.

XML parsers interpret unqualified element names relative to the namespace specified in the most-recent, in-scope, default namespace attribute, xmlns. When you specify an xmlns value of "http://schemas.microsoft.com/2003/xaml", the build system interprets an unqualified element name on the defining element, or one of its subordinate elements, as the name of a class in a predefined set of namespaces.Let me restate that in more concrete terms, using C # as an example. The xmlns declaration effectively adds a number of using statements to your code. The build system then interprets each unqualified XAML element name as a class name with the using declarations providing the context for the possible namespaces. Although the list might change, at the time of this writing , Specifying The Standard Value for the deflusion of the folloading sale meansments:

Using msavalon.windows;

Using msavalon.windows.Controls;

Using msavalon.windows.controls.primitives;

Using msavalon.windows.data;

Using msavalon.windows.documents;

Using msavalon.windows.shapes;

Using msavalon.windows.media;

Using msavalon.windows.media.Animation;

Using msavalon.windows.navigation;

..................................

I set the visible attribute of the window element to true original code what displays the window by calling its show method.

I've nested a SimpleText element within the Window element definition. This tells the system to instantiate an MSAvalon.Windows.Controls.SimpleText object, make it a child of the Window object, and set the value of the simple text object to the " Hello World! "String.save the preceding xaml code in a file named helloworld.xaml, and run the file. The Browser Will Interpret The Ui, AS Shown In Figure 1-1.

Figure 1-1. The Browser Displaying The Xaml Version of Hello World (Click Picture To See Larger Image)

You might want to use a .NET class that is not defined in one of the default namespaces listed previously. A typical example is using a class from an assembly that you create. The build system needs to be able to map the element name that You Specify In The Xaml Source File to the appropriate .NET Class in The Correct assembly. XAML Defines An XML Processing Instruction (PI) Named? mapping "You Use to make this association.

The? Mapping PI allows you to define an XML namespace prefix that maps to a CLR namespace and assembly. When you qualify an XAML element name with this namespace prefix, you tell the build system, in effect, to take the element name, add the CLR Prefix to the name, and create an instance of the class with the resulting name. The Compiler Will Reference The Specified Assembly So That It can find the definition of the class.

The Following Example Creates An Instance of the Wiseowl.statistics.poissondeviate Class, The Definition of Which Resides in The Wiseowl.statistics.Library Assembly:

AskMBLY = "WISEOWL.STATITISTICS.LIBRARY"?>

Hello World!

I can not emphasize enough that XAML is simply another way to produce code that uses the .NET Framework UI classes. In fact, you could have a tool that displays a XAML UI specification graphically using a visual designer. Another tool might do the reverse and allow you to design the UI graphically and save it as a XAML file. Yet another tool might save the UI design as procedural code, which is similar to how the WinForms designer works. All these approaches are just different methods of specifying the same information.

Earlier in this chapter, I mentioned that the browser could render a XAML file in its window. The browser can do this only when the XAML file contains nothing but markup, like the simple example just shown. As your UI becomes more complicated, you ' ll typically have to use event handlers and other nonmarkup source code in addition to the XAML that describes the UI. Any time you have a mixed source code base-that is, markup and nonmarkup source code-you must compile the markup and source code using ................... ..

Summary

All right! You now understand the basics of the new application model. You've learned how to use markup to create a UI declaratively, albeit a very simple UI. You could write the equivalent of Web pages using XAML files and deploy those files to .. a server for a user to browse However, scenarios that are more interesting will typically require you to compile the application before deploying it So let's jump right in and learn how to build and deploy a "Longhorn" application.Continue to Chapter 2: Building a "longhorn" Application.

© 2003 Microsoft Corporation. All Rights Reserved.

IntelliSense, Microsoft, MSDN, MS-DOS, Visual Basic .NET, and Visual Studio .NET are either registered trademarks or trademarks of Microsoft Corporation in the United States and / or other countries. Other product and company names mentioned herein may be the trademarks Of their respective ows.

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

New Post(0)