快速浏览Silverlight3 Beta:在线与离线状态检测
在Silverlight3中提供了NetworkChange,NetworkInterface这两个类(均为abstract型),
其中实现了NetworkAddressChanged的事件处理器用来检测当前在线状态,而NetworkInterface
类的 GetIsNetworkAvailable()方法(返回bool类型),用来判断当前是否在线。有了这两个类,
就可以很方便的实现动态检测当前应用是否连线了。
下面就是我写的一个DEMO,用于演示如何使用这两个方法,其中的XAML代码如下:
<UserControl x:Class="Off_Online.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="150" />
<RowDefinition Height="150" />
</Grid.RowDefinitions>
<Image Width="100" Height="100" x:Name="StateIcon" Grid.Row="0" HorizontalAlignment="Center" />
<TextBlock Name="NetWorkState" Foreground="Red" FontSize="20" Grid.Row="1" FontWeight="Bold" HorizontalAlignment="Center" >当前状态未知</TextBlock>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="150" />
<RowDefinition Height="150" />
</Grid.RowDefinitions>
<Image Width="100" Height="100" x:Name="StateIcon" Grid.Row="0" HorizontalAlignment="Center" />
<TextBlock Name="NetWorkState" Foreground="Red" FontSize="20" Grid.Row="1" FontWeight="Bold" HorizontalAlignment="Center" >当前状态未知</TextBlock>
</Grid>
</UserControl>
而相应的CS代码如下:
using System.Net.NetworkInformation;
namespace Off_Online
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
UpdateNetWorkState();
}
void UpdateNetWorkState()
{
string state = NetworkInterface.GetIsNetworkAvailable() ? "Online" : "Offline";
StateIcon.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/" + state + ".jpg", UriKind.Relative));
NetWorkState.Text = NetworkInterface.GetIsNetworkAvailable() ? "在线" : "离线";
}
void OnNetworkAddressChanged(object sender, EventArgs e)
{
UpdateNetWorkState();
}
}
}
namespace Off_Online
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
UpdateNetWorkState();
}
void UpdateNetWorkState()
{
string state = NetworkInterface.GetIsNetworkAvailable() ? "Online" : "Offline";
StateIcon.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/" + state + ".jpg", UriKind.Relative));
NetWorkState.Text = NetworkInterface.GetIsNetworkAvailable() ? "在线" : "离线";
}
void OnNetworkAddressChanged(object sender, EventArgs e)
{
UpdateNetWorkState();
}
}
}
下面就是其运行效果,首先是在线状态:
然后拔掉网线,就会显示离线状态了:
好了,今天的内容就先到这里了,源码下载,请点击这里:)
原文链接:http://www.cnblogs.com/daizhj/archive/2009/05/05/1450061.html
作者: daizhj, 代震军
Tags: silverlight,offline,online
网址: http://daizhj.cnblogs.com/
分类:
silverlight
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix