饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 73 页

作者:Nick Randolph/等 当前章节:15372 字 更新时间:2026-6-18 14:51

must be user initiated (so random applications can’t install themselves on users’ machines without

their approval).

By default, your Silverlight application will

not be configured for Out-Of-Browser mode,

and you must explicitly enable this in your

application for the feature to be available.

The easiest way to enable this is in the project

properties for the Silverlight application, as

was shown in Figure 22-4. When you put a

check in the Enable Running the Application

Out of the Browser option the Out-of-Browser

Settings button becomes enabled, and clicking

this button pops up the window shown in

Figure 22-9.

This window enables you to configure various

options for when the application is running

in Out-Of-Browser mode. Most of the options

are fairly self-explanatory. You can set the

window title and its starting dimensions

(the window is resizable). You can also

configure the start menu/desktop shortcuts,

set the text for the shortcut (the shortcut

name), set the text that will appear when the

mouse hovers over the icon (the application

description), and set the various-sized icons to

use for the shortcut. These icons must be PNG

files that have already been added as files in your Silverlight project. Select the appropriate image

for each icon size. If you leave any of these icons blank, it simply uses the default Out-Of-Browser

icon for that icon size instead. The two checkboxes at the bottom enable you to set whether

Out-Of-Browser mode should use GPU acceleration (for Silverlight applications running inside

the browser this setting is set on the Silverlight plug-in itself), and the Show install menu checkbox

specifies whether the user should have the option to install the application via the right-click menu

(otherwise, the install process must be initiated from code).

fiGure 22-9

482 . chaPter 22 SilVerlighT

Once you ’ ve confi gured the Out - Of - Browser settings

you can now run the project and try it out. When your

application is running, right - click anywhere on your

application and select the Install XXXX onto your

computer option as shown in Figure 22 - 10 to

initiate the install process (where XXXX is the

name of the application).

The window shown in Figure 22 - 11 appears

with options for the user to select which types of

shortcuts to the application should be set up.

This installs the application locally (under the

user ’ s profi le), confi gures the selected desktop/

start menu shortcuts, and automatically starts the

application in Out - Of - Browser mode.

Note that your Silverlight application is still sandboxed when running outside

the browser and will have no more access to the user ’ s computer than it did

while running inside the browser. So although it may appear to be running as

if it were a standard application, it ’ s still restricted by the same security model

as when it ’ s running inside the browser. However, Silverlight 4 introduced the

ability for Out - Of - Browser applications to obtain elevated trust privileges,

including COM automation and local fi le access.

fiGure 22 - 10

fiGure 22 - 11

To uninstall the application, simply right - click it and select the Remove this

Application option.

Of course, you will need to update your application at some point in time and have the existing

instances that were installed updated accordingly. Luckily, this is very easy to do, but does require some

code. This code could be used anywhere in your application, but you ’ ll put it in the code - behind for the

App.xaml fi le, and start the update available check as soon as the application has started as follows:

Vb

Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) _

Handles Me.Startup

Me.RootVisual = New MainPage()

If Application.Current.IsRunningOutOfBrowser Then

enabling running out of Browser .

483

Application.Current.CheckAndDownloadUpdateAsync()

End If

End Sub

Private Sub App_CheckAndDownloadUpdateCompleted(ByVal sender As Object, _

ByVal e As _

System.Windows.CheckAndDownloadUpdateCompletedEventArgs) _

Handles Me.CheckAndDownloadUpdateCompleted

If e.UpdateAvailable Then

MessageBox.Show("A new version of this application is available and " &

"has been downloaded. Please close the application and " &

"restart it to use the new version.",

"Application Update Found", MessageBoxButton.OK)

End If

End Sub

c#

private void Application_Startup(object sender, StartupEventArgs e)

{

this.RootVisual = new Page();

if (Application.Current.IsRunningOutOfBrowser)

{

Application.Current.CheckAndDownloadUpdateCompleted +=

Current_CheckAndDownloadUpdateCompleted;

Application.Current.CheckAndDownloadUpdateAsync();

}

}

private void Current_CheckAndDownloadUpdateCompleted(object sender,

CheckAndDownloadUpdateCompletedEventArgs e)

{

if (e.UpdateAvailable)

{

MessageBox.Show("A new version of this application is available and " +

"has been downloaded. Please close the application and restart " +

"it to use the new version.", "Application Update Found",

MessageBoxButton.OK);

}

}

As you can see, if the application is running in Out-Of-Browser mode you check to see if there

are any updates. This asynchronously goes back to the URL that the application was installed

from and checks if there is a new version (during which the application continues to load and

run). If so it automatically downloads it. Whether or not an update was found, it raises the

CheckAndDownloadUpdateCompleted event once the check (and potential download of a new

version) is complete. Then you just need to see if an update had been found, and notify the user if

so. The update is automatically installed the next time the application is run, so in order to start

using the new version the user will need to close the application and reopen it again.

To test the update process, start by including the update check code in your application. Run

the application and install it using the method described earlier. Close both it and the instance

484 .

chaPter 22 SilVerlighT

that was running in the browser and return to Visual Studio. Make a change to the application

(one that allows you spot the difference if it is updated correctly) and recompile it. Now run the

previously installed version (from the Start menu or desktop icon). The application starts, and

shortly afterwards the message box appears stating that the new version has been downloaded and

to restart the application. When you reopen the application again you should see that you are indeed

now running the new version.

suMMary

In this chapter you have seen how you can work with Visual Studio 2010 to build applications with

Silverlight, and run them both within and outside the browser. To learn about one of the many

means of communicating between the client and the server and transferring data see Chapter 35,

“WCF RIA Services.”

23

Dynamic Data

what’s in this chaPter?

.

Creating a data-driven web application without writing any code

using Dynamic Data’s scaffolding functionality

.

Customizing the data model and presentation layer of a Dynamic

Data application

.

Adding Dynamic Data features to an existing web application

Most developers spend an inordinately large amount of their time writing code that deals with

data. In fact, this is so fundamental to what many of us do on a daily basis that an acronym

has appeared to describe this type of code — CRUD. CRUD stands for Create, Read, Update,

Delete , which are the four basic functions that can be performed on data.

For example, consider a simple application to maintain a Tasks or To Do list. At the very least

the application must provide the following functionality:

.

Create: Create a new task and save it in the database.

.

Read: Retrieve a list of tasks from the database and display them to the user. Retrieve

and display all the properties of an individual task.

.

Update: Modify the properties of an existing task and save the changes to the

database.

.

Delete: Delete a task from the database that is no longer required.

ASP.NET Dynamic Data is a framework that takes away the need to write much of this low-

level CRUD code. Dynamic Data can discover the data model and automatically generate a

fully functioning, data-driven web site at run time. This allows developers to focus instead on

writing rock-solid business logic, enhancing the user experience, or performing some other

high-value programming task.

486 .

chaPter 23 dynAmic dATA

less is More: scaffoldinG and conVention oVer confiGuration

Scaffolding is the name for the mechanism that ASP.NET Dynamic Data uses to

dynamically generate web pages based on the underlying database. The generated pages

include all of the functionality you would expect in any decent data - driven application

including paging and sorting. In addition to the benefits of freeing developers from

writing low - level data access code, scaffolding provides built - in data validation based on

the database schema and full support for foreign keys and relationships between tables.

Scaffolding was popularized by the Ruby on Rails web development framework.

Along with scaffolding, ASP.NET Dynamic Data includes several other principles

and practices that are clearly inspired by Ruby on Rails. One such principle is

Convention over Configuration, which means that certain things are implicitly

assumed through a standard convention. For example, at run time, Dynamic Data

will detect the file List.aspx under the folder called Products and use it to render

a custom web page for the Product database table. Because the folder name is the

same (pluralized) name as the database table, there is no need to explicitly tell

Dynamic Data that this file exists, or that it is associated with the Product table.

Less code means fewer places for mistakes.

This chapter demonstrates how to use Dynamic Data scaffolding to create a data-driven web

application with little or no code. You also learn how flexible Dynamic Data is by customizing

the data model and web pages.

Although Dynamic Data is somewhat synonymous with scaffolding and building a data-driven

web application from scratch, at the end of this chapter you will see that you can get a number of

benefits by adding Dynamic Data functionality to your existing web application.

creatinG a dynaMic data web aPPlication

Before you can create and run a Dynamic Data web application you will need a database. The

examples in this chapter use the SQL Server 2008 AdventureWorksLT database, which you can

download from the CodePlex web site at http://msftdbprodsamples.codeplex.com/.

Once you ’ ve downloaded your database, open Visual Studio and select File . New . Project. In the

Web project category of both Visual Basic and C# you will see two project templates for Dynamic

Data that refl ect the two major data access options supported by Microsoft. The fi rst, LINQ to SQL,

is provided by the aptly named Dynamic Data Linq to SQL Web Application template. The second

template, Dynamic Data Entities Web Application, supports the ADO.NET Entity Framework.

If you prefer working with Web Site projects instead of Web Application projects

you can still use Dynamic Data. Under the New Web Site dialog you will find

two equivalent templates for creating a new LINQ to SQL or Entities Dynamic

Data Web Site project.

Creating a Dynamic Data Web application .

487

linq to sql Versus the ado net entity fraMework

LINQ to SQL and the ADO.NET Entity Framework are the two main data access

options that are currently being promoted by Microsoft. Both have their pros

and cons, and both work perfectly well for many of the more common scenarios.

LINQ to SQL works only with Microsoft SQL Server database, and only supports

a direct mapping of a single database table to a single .NET class. Because LINQ

to SQL is so tightly coupled to SQL Server, it is known to generate very efficient

T - SQL code.

On the other hand, the ADO.NET Entity Framework allows for a data model that

is different from the underlying database schema. You can map multiple database

tables to a single .NET class, or a single database table to multiple .NET classes.

The Entity Framework also supports a number of different databases including

Oracle, MySQL, and DB2.

You can find out more about LINQ to SQL in Chapter 28 and the ADO.NET

Entity Framework in Chapter 29.

Select the Dynamic Data Linq to SQL Web Application

project and click OK. When the new project is created it will

generate a large number of files and folders, as shown in

Figure 23 - 1. Most of these files are templates that can be

modified to customize the user interface. These are located

under the DynamicData root folder and are discussed later

in this chapter.

The project template will also create a standard web form,

Default.aspx, as the start page for the web application.

As with the standard ASP.NET Web Application project,

the application encourages best practices by making use

of the master page feature and an external CSS file, and

includes the JQuery JavaScript library. See Chapter 20 for

further information on any of these features.

adding a data Model

Once you have created your new project you will need to

specify the database and create a new data model. Right-

click the App_Data folder and select Add Existing Item,

then browse to the AdventureWorksLT2008_Data.mdf file

you downloaded earlier and click Add.

fiGure 23-1

488 .

chaPter 23 dynAmic dATA

The next step is to create the data model. Right-click the project in the Solution Explorer and

select Add . New Item. Select the LINQ to SQL Classes item from the Data category and name it

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页