Tải bản đầy đủ (.pdf) (8 trang)

Lập trình .net 4.0 và visual studio 2010 part 59 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (743.1 KB, 8 trang )

CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

387
• Many changes to API and refactoring of XamlSchemaContext for performance
improvements
• System.Xaml.dll no longer has a dependency on WindowsBase.dll
• The same XAML stack is utilized by WCF, WF, and WPF
• Performance optimizations in Baml2006Reader class
• New class XamlXmlReader
• Improved localization support
• Baml2006Writer class might be available with this release, which could potentially
allow the obfuscation of BAML
Silverlight 3.0

Silverlight developers are in for a treat with the latest version of Silverlight which offers the ability to run
your applications offline, deep linking for content and much more.

NOTE
This chapter assumes a basic knowledge of Silverlight and WPF. If you haven’t used Silverlight before
you might want to take a look at Chapter 14 where I introduce Silverlight.
Upgrading from Silverlight 2
Before you look at the new changes in Silverlight 3.0, note that upgrading can potentially break existing
applications. This URL lists breaking changes: />us/library/cc645049(VS.95).aspx.
And this URL provides guidance on how to upgrade your Silverlight 2.0 applications:

Offline Applications
Probably the best feature of Silverlight 3.0 is the ability it offers to run your applications offline. Offline
Silverlight applications run with the same permissions as their web counterparts, so they do not require
additional permissions to install. Offline applications also work on both PC and Mac platforms,
providing a very easy way to create cross-platform .NET applications.
Creating an Offline Application


To enable your Silverlight applications to run offline is very easy and involves making a simple change to
the AppManifest file. Try it now:
1. Create a new Silverlight application called Chapter15.Offline.
2. Add some content (e.g., an image).
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

388
3. Open the project properties (see Figure 15-22).

Figure 15-22. Enabling offline application in project properties
4. Check the box marked “Enable running application outside of browser”.
5. Click the Out-of-Browser Settings button and note that you can set properties such as images,
window size, and application title.
6. Press F5 to run your Silverlight project and then right-click the Silverlight app. The Silverlight
menu will open up.
7. Select the “Install Chapter15.Offline Application onto this computer” option (see Figure 15-23).

CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

389

Figure 15-23. Installing a Silverlight application onto the computer
8. Silverlight will then ask you to confirm where you want to place shortcuts to your application:
Start menu and/or Desktop (see Figure 15-24).

Figure 15-24. Offline Silverlight application
9. Check both the Start menu and Desktop options.
10. Close the browser.
11. Now click one of the short cuts that has been created, and your application will load up running
offline (see Figure 15-25):


CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

390

Figure 15-25. Running a silverlight app offline
Uninstalling Offline Silverlight Applications
If you want to uninstall an offline Silverlight application, simply right-click the window when it is
running locally and select the “Remove this application” option.
Detaching Manually
You probably don’t want to explain these steps to end users, so the Silverlight API contains an
Application.Current.Install()method that performs the same functionality.
Application.Current.Install() method returns a Boolean value indicating whether the detachment was
possible or not.
Retrieving Attachment State
To query whether your application is running online in a browser or detached, use the
Application.Current.IsRunningOutOfBrowser method.
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

391
Detecting Connection Status
Now that you have the ability to run applications offline, it is very useful to be able to determine whether
the user is connected to the Internet. This can be accomplished through the GetIsNetworkAvailable
method that returns true if the user is connected:

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

CAUTION
If the user has chosen to work in offline mode, this method will still return true.
You also have the ability to monitor network address changes through the NetworkAddressChanged

event:

System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged()
Autoupdate
When offline applications are run, Silverlight automatically checks to see whether a later version is
available. If it is, it is downloaded.
Deep Linking and Browser History
A major problem with Silverlight applications is that you cannot directly link to content within the
application in the same way you can with web pages. This makes it difficult for users to share or
bookmark content and makes search engine indexing impossible. Silverlight 3.0 attempts to solve this
issue by making use of HTML bookmark syntax in your application's URL.
Navigation Application
Navigation Application is a new type of project in Silverlight 3.0. If you say Navigation Application
quickly it sounds like gangsta rap, argued Silverlight MVP Chris Hay, and he was right. But that doesn’t
have much to do with anything, so let’s take a look at Navigation Application now:
1. Create a new Silverlight Navigation Application project called
Chapter15.NavigationApplication.
2. Press F5 to run the application and you will see the default Navigation Application project (see
Figure 15-26).
3. Click the about button and notice how the URL changes to something similar to this:
http://localhost:51951/Chapter15.NavigationApplicationTestPage.aspx#/About

You can use this URL to refer directly to the about pagetry navigating to a different page and then
pasting the URL into the address bar. This new URL format allows the browser to maintain a history of
pages the user navigated through, which means the back and forward browser buttons can be used to
move around your applications.
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

392



Figure 15-26. Default Navigation Application
Local Connections
Some page designs utilize two separate Silverlight controls and need a way to pass information between
them. Previously, this could only be accomplished using the HTML DOM methods. Silverlight 3.0 makes
this much easier with the local connection API. Let’s create a simple example and send some text from
one Silverlight control to another:
1. Open Visual Studio and create a new Silverlight application called Chapter15.Sender.
2. Add another Silverlight application to the solution called Chapter15.Receiver (opt not to create
another web hosting project).
3. Open Chapter15.SenderTestPage.aspx.
4. You want to display the Chapter15.Receiver project on the same page, so add another
Silverlight control beneath the existing one by copying the object block and modifying the
source parameter to display the Chapter15.Receiver project.xap file:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/Chapter15.Receiver.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

393
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<a href="
style="text-decoration:none">
<img " alt="Get Microsoft
Silverlight"
style="border-style:none"/>
</a>

</object>
5. Open MainPage.xaml in the sender project and add a button with the following XAML:
<Button x:Name="cmdSendMessage" Content="Send Message" Width="200"
Height="200"></Button>
6. Open MainPage.xaml.cs in the sender project and import the following namespace:
using System.Windows.Messaging;
7. Amend the code in MainMenu.xaml.cs to the following:
public partial class MainPage : UserControl
{
LocalMessageSender Channel1 = new LocalMessageSender("Channel1");

public MainPage()
{
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
Channel1.SendCompleted +=
new EventHandler<SendCompletedEventArgs>(Channel1_SendCompleted);
InitializeComponent();
}

void Channel1_SendCompleted(object sender, SendCompletedEventArgs e)
{
//Code to run after message is sent
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
this.cmdSendMessage.Click += new RoutedEventHandler(cmdSendMessage_Click);
}

void cmdSendMessage_Click(object sender, RoutedEventArgs e)

{
Channel1.SendAsync("Hello from sender project");
}
}
8. Open the receiver project MainPage.xaml and add a text box to display the received messages:
<TextBlock>Received messages:</TextBlock>
<TextBox x:Name="txtMessage"></TextBox>
CHAPTER 15  WPF 4.0 AND SILVERLIGHT 3.0

394
9. You now need to add code to receive the messages and update the text box with the messages
you have received. To receive messages, handle the MessageReceived event on an instance of
LocalMessageReceiver. Open MainPage.xaml.cs in the receiver project and import the following
namespace:
using System.Windows.Messaging;
10. Amend MainPage.xaml.cs to the following:
public partial class MainPage : UserControl
{
LocalMessageReceiver Channel1Receiver = new LocalMessageReceiver("Channel1");

public MainPage()
{

Channel1Receiver.MessageReceived +=
new
EventHandler<MessageReceivedEventArgs>(Channel1Receiver_MessageReceived);
Channel1Receiver.Listen();
InitializeComponent();
}


void Channel1Receiver_MessageReceived(object sender, MessageReceivedEventArgs e)
{
txtMessage.Text="" + e.Message.ToString();
}
}

NOTE
The LocalMessageReceiver constructor sets the parameter to Channel1 (the same channel that
messages are being sent on).
11. Press F5 to run the project.
12. Click the Send Message button. A message should then be sent using the local connection API
and the results displayed in the text box in the receive project.
Styles
Styles in Silverlight 3.0 can now be modified at runtime and support inheritance.
Applying Styles Dynamically
The following code shows how to apply a style to a button at runtime:

myButton.Style=(Style)Application.Current.Resources["MyHorridFuciaStyle"];

×