Thông tin tài liệu:
Essential Silverlight 3- P2: Khái quát Silverlight 3 không chỉ là lấp đầy với các chi tiết kỹ thuật, ví dụ rõ ràng,
và thực hiện lời khuyên rằng sẽ làm cho bất kỳ ứng dụng Silverlight tốt hơn, nhưng
Ashraf cũng bao gồm những hiểu biết rằng chỉ có thể đến từ một trong những nhà phát triển dẫn
của thời gian chạy Silverlight. Từ đồ họa, văn bản, để phương tiện truyền thông cuốn sách này-
có tất cả các thông tin cần thiết về thời gian chạy lõi 3 Silverlight....
Nội dung trích xuất từ tài liệu:
Essential Silverlight 3- P2
18 Chapter 2: Applications
// The RootVisual cannot be modified here since it will be
// used in the startup page animation. Defer initialization to
// the startup event after your application has loaded.
//
this.Startup += this.A pplication_Startup;
// You can also connect event handlers for shutdown and error
// handling
//
// this.Exit += this.A pplication_Exit;
// this.UnhandledException += this.A pplication_Unhandled;
}
private void A pplication_Startup(
object sender,
StartupEventA rgs e
)
{
// Create a hello world TextBlock
TextBlock textBlock = new TextBlock();
textBlock.Text = Hello World;
// Create a container canvas
Canvas canvas = new Canvas();
canvas.Children.A dd(textBlock);
// Set the application root to display the canvas
this.RootVisual = canvas;
}
}
}
In this example, the entry point is the HelloWorld.A pp constructor that
hooks up a Startup event handler that creates the application contents. You
need to create your application contents in the Startup event handler after
the startup screen loading animation. You can also hook up the Exit event
to save state when your application is going away, or hook up the
UnhandledException handler event to get information about exceptions
thrown during the execution of your application.
Although you can use the structure shown previously to construct the
application, you should construct your application objects through
XAML, the Silverlight declarative XML instantiation language. You can
conveniently edit XAML with WYSIWYG tools such as Expression Blend
and XAML will load faster than the equivalent initialization code.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Application Components 19
PERFORMANCE TIP
You may expect instantiating objects from code to be faster than
parsing XAML, but XAML is faster in most cases. The reason XAML
is faster is that the XAML parser does not create the API objects you
use to interact with elements, but instead only creates an internal
representation. After you start interacting with elements from code,
Silverlight creates API objects that will slow down your application.
To use XAML to build the previous example application, modify
HelloWorld.A pp to load a XAML file:
using System;
using System.Windows;
namespace HelloWorld
{
public partial class A pp : A pplication
{
public A pp()
{
System.Windows.A pplication.LoadComponent(
this,
new System.Uri(
/HelloWorld;component/A pp.xaml,
System.UriKind.Relative
)
);
}
}
}
Here is the XAML file that corresponds to the sample application in the
previous code example:
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
20 Chapter 2: Applications
A XAML file specifies which objects to create and what properties to set.
For example, the A pplication tag creates the HelloWorld.A pp class. The
A pplication.RootVisual tag specifies that the RootVisual property of the
A pplication object be set to the Canvas element contained within that tag.
Generally, setting an XML attribute specifies setting a property to the value
specified by the value of the attribute. You can also set a property by using
a special tag named to specify that property
MyProperty be set on class MyClass to the value of the element nested
within the scope of the XAML tag. This property syntax enables you to set
a property to a tree of objects rather than a simple string-based value.
The previous XAML is equivalent to
• Constructing the A pplication object. The x:Class property
indicates that this object is defined by the HelloWorld.A pp class. All
event handlers specified in the XAML file refer to methods on the
HelloWorld.A pp class
• Constructing the Canvas element
• Setting the A pplication.RootVisual property to the Canvas element
• Constructing the TextBlock element
• Setting the TextBlock element as a child of the Canvas element
• Setting the Text property on the TextBlock element to “Hello
World”
The structure of every Silverlight application follows the same pattern
shown in this section. In particular, the application consists of
• The HTML page that hosts the Silverlight plug-in
• A ZIP file (with a .XAP extension) that contains
• An a ...