These out-of-browser applications allow you to bring the richness of Silverlight 3 applications directly to the desktop without the restriction of running within a browser.
All examples are included in this month's code download.
Eyes On TheTarget
Figure 1 shows the sample application running out of the browser.
Data Points : Building An Out-of-Browser Client With Silverlight 3
Figure 1 Silverlight Twitter Client at a Glance
Other alternatives are to use PopFly or Yahoo Pipes to relay the requests.
In that column, I discuss the file formats and how the policies work.
At that time the deferred message will be sent to Twitter.
This window is launched using the sllauncher.exe process and its host the Silverlight 3 out-of-browser application.
A Detach button also exists in the lower section of the application that will detach the application to run outside of the browser.
Install To Desktop
For example, since they are not hosted in the browser, communications with the browser's DOM are not possible like they are with Silverlight applications hosted in the browser.
The Application.Current.Detach method will raise an exception if it cannot detach, so some error handling is a good idea.
private void DetachButton_Click(object sender, RoutedEventArgs e)
{
    try {
        Application.Current.Detach(); // take the app out of browser
    }
    catch (InvalidOperationException opex)    {
        MessageBox.Show("Application is already detached" +
          " to the desktop.");
    }
    catch (Exception ex)    {
        MessageBox.Show("Application could not be detached" +
          " to the desktop."+ Environment.NewLine + 
          ex.Message);
    }
}
Data Points : Building An Out-of-Browser Client With Silverlight 3
Figure 3 Detaching a Silverlight Application to Run Outside the Browser
At this point, the browser can be closed and the out-of-browser application continues to run on its own.
It is worth noting that out-of-browser applications will not show up in the Control Panel's add/remove programs list.
Manifest Essentials
The AppManifest.xml is located in the Properties folder by default and it can be edited manually to include the out-of-browser capabilities.
The various icons are used for the Start menu shortcut, the desktop icon shortcut, the icon for the window in the out-of-browser application, and in the dialog that prompts users to decide whether they want to install the application outside the browser.
Running Offline
Therefore, before running this code, you could check the RunningOffline property.
The starting size and control of the window itself is not currently a feature in the Silverlight 3 Beta.
Figure 6, where the application is running inside the browser.
private void SetOfflineStatus()
{
    if (Application.Current.RunningOffline)
        stats.RunningModeMessage = "Running Out of Browser";
    else
        stats.RunningModeMessage = "Running In Browser";
}
Data Points : Building An Out-of-Browser Client With Silverlight 3
Figure 6 Running Online
There are five different states (found in the ExecutionStates enumerator) that can cause this to occur:
  • RunningOnline – The application is now running in the browser.
  • Detaching – The application is detaching from the browser.
  • Detached – The application is now detached from the browser and running outside the browser.
  • DetachedUpdatesAvailable – Same as detached, but it sees that updates are available.
  • DetachedFailed – A detach event failed.
As the events change, the message in the application automatically updates itself using data binding.
In that column, I discuss data binding and binding modes.
An event handler can be set up to listen for these states by handling the ExecutionStateChanged event, as shown below:

Application.Current.ExecutionStateChanged += new EventHandler(Network_ExecutionStateChanged);

Figure 7 shows that the message in the out-of-browser application was displayed when the ExecutionStateChanged event fired.
Data Points : Building An Out-of-Browser Client With Silverlight 3
Figure 7 Updates Are Available
Network Connectivity
Some workaround might include notifying the user of the problem or possibly storing the data locally until a network connection becomes available.
This event can be handled as shown below:

NetworkChange.NetworkAddressChanged += 
  new NetworkAddressChangedEventHandler(NetworkChange_  NetworkAddressChanged);

This will cause the NetworkAddressChanged event to fire and the UI to be updated through the data bindings.
private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
    stats.IsNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();

    if  (NetworkInterface.GetIsNetworkAvailable())
    {
        // get the stored tweet, try to post it
        if (IsolatedStorageSettings.ApplicationSettings.Contains(TWEET_STORAGE))
        {
            PostTweet(IsolatedStorageSettings.ApplicationSettings[TWEET_STORAGE].ToString());
        }
    }
}
Data Points : Building An Out-of-Browser Client With Silverlight 3
Figure 9 Network Address Unavailable
Figure 8 shows that when the network change is detected, if the network just became available and there is a message in Isolated Storage, the message will be posted to Twitter.
Storing Data Offline
If there is, the user is notified that only one message can be stored at a time.
private void PostTweet()
{
    string tweetText = TweetTextBox.Text;
    if (!NetworkInterface.GetIsNetworkAvailable())
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(TWEET_STORAGE))
        {
            MessageBox.Show(
                "Network connection is not available and only 1 tweet" + 
                " can be stored for delayed posting to Twitter.");
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings.Add(TWEET_STORAGE, tweetText);
            MessageBox.Show(
                "Network connection is not available. The post" +
                " will be stored until connectivity returns.");
        }
        return;
    }

    PostTweet(tweetText);
}
Additional storage can be requested through the IsolatedStorageFile.IncreaseQuoteTo API, which will prompt the user to allow or deny the quota increase.
When the network address is reestablished, the NetworkAddressChanged event handler will run and will pull the message out of Isolated Storage and submit it (as shown in Figure 8).
You're sure to come up with some great uses for these new features!


【reprinted from John Papa :http://msdn.microsoft.com/zh-cn/magazine/dd882515.aspx#id0400018】

相关文章: