Don't think you are, know you are

博客园 首页 新随笔 管理

我曾经在 Sun 的技术会议上见过这种技术,可惜 Sun 已不在了,当时那个主持人说起这个技术还一脸得意,现在在 Silverlight 下,实现起来竟是如此简单。

ok, Silverlight 必然要具备脱离浏览器和离线的能力,无论是 MS 的主观意愿还是客观的 WP7 的开发要求,这都是必须的。 不能不说,微软对浏览器是又爱又恨的,显然它意识到了浏览器的重要性,才不惜违法击垮Netscape,但现在还是在浏览器这个框框里落后了。So,如果有机会把战场跳出这个框框.....

 

言归正传,先说一下如何判断在线状态,显然你在做分别做在线和离线处理时,是需要先判断下的。

页面中添加一个

<Ellipse x:Name="ellipseIndicator" Height="82" HorizontalAlignment="Center" Stroke="Black" Fill="Gray" StrokeThickness="1" VerticalAlignment="Top" Width="86" /> 来指示状态

下面是cs(不能叫后台)代码

 public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            UpdateIndicator();
        }
        private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            UpdateIndicator();
        }
        private void UpdateIndicator()
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                this.ellipseIndicator.Fill = new SolidColorBrush(Colors.Green);
            }
            else
            {
                this.ellipseIndicator.Fill = new SolidColorBrush(Colors.Red);
            }
        }
    }

好了我知道E文不好的你有问题了,答案是NetworkAddressChanged这个事件会在运行端网络地址发生变化的时候触发。

 

下面另一个知识点,看一下怎么将程序脱离浏览器,和前面一个知识点显然是关联的,试想如果用户在离线状态下刷新浏览器,那你的Silverlight页面会变成什么样?(2种情况,你自己试吧),脱离浏览器是处理离线或不能总是在线这种状态的必要条件,如移动设备。显然这意味着将程序要安装到本地。

看见了,那个网络管理员举手了,不用问了,这就回答你的问题:Silverlight applications running out-of-browser are subject to the same
security restrictions as those running inside of a browser.

 

好了,具体做法是

右键点击 Project  ,点击 property

 更多内容敬请点击 out-of-browser settings

设定完就可以F5,然后点击右键试试就明白了。这里要提醒的是,从易用性来说最好给用户一个直观的按钮,让用户可以 安装 及 更新 程序。详细例子如下,包括自动更新的按钮:

<UserControl x:Class="OutOfBrowserTestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">

        <Button x:Name="btnInstallLocally" Content="Install Locally" Click="btnInstallLocally_Click" Height="23" Width="109" HorizontalAlignment="Left" Margin="12,30,0,0" VerticalAlignment="Top"/>
        <Button Height="23" Width="109" Content=" Check Update" Click="Button_Click"/>
        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="29,127,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="hahahehea" />
    </Grid>
</UserControl>

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OutOfBrowserTestApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnInstallLocally_Click(object sender,RoutedEventArgs e)
        {
            if (Application.Current.InstallState == InstallState.Installed)
            {
                MessageBox.Show("Application already installed");
            }
            else
            {
                Application.Current.Install();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(App_CheckAndDownloadUpdateCompleted);
            App.Current.CheckAndDownloadUpdateAsync();
        }

        private void App_CheckAndDownloadUpdateCompleted(object sender,CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
            {
                MessageBox.Show("An application update has been downloaded. Restart the application to run the new version.");
            }
            else if (e.Error != null && e.Error is PlatformNotSupportedException)
            {
                MessageBox.Show("An application update is available, but it requires a new version of Silverlight. Visit the application home page to upgrade.");
            }
            else
            {
                MessageBox.Show("There is no update available.");
            }
        }
    }
}

 

待续...

posted on 2010-12-02 17:20  炭炭  阅读(674)  评论(0编辑  收藏  举报