内容调整为 VM 数据绑定
This commit is contained in:
1 parent
86190a5f3c
commit
b196e511b6
3 files changed
+692
-292
No files matched your search
@@ -1,66 +1,99 @@
|
||||
<UserControl x:Class="Shell.UserControls.NetworkMonitorView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:NetVizor.Views"
|
||||
xmlns:converters="clr-namespace:NetVizor.Views.Converters"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Background="#F0F2F5">
|
||||
<Grid Margin="10">
|
||||
<UserControl
|
||||
x:Class="Shell.UserControls.NetworkMonitorView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:viewModel="clr-namespace:Shell.ViewModel"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
Background="#F0F2F5">
|
||||
|
||||
<d:UserControl.DataContext>
|
||||
<viewModel:NetworkMonitorViewModel />
|
||||
</d:UserControl.DataContext>
|
||||
<Grid
|
||||
Margin="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="20" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition
|
||||
Height="Auto" />
|
||||
<RowDefinition
|
||||
Height="20" />
|
||||
<RowDefinition
|
||||
Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 网络接口选择 -->
|
||||
<Grid Grid.Row="0">
|
||||
<Grid
|
||||
Grid.Row="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition
|
||||
Width="Auto" />
|
||||
<ColumnDefinition
|
||||
Width="*" />
|
||||
<ColumnDefinition
|
||||
Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Label Grid.Column="0" Content="选择网络接口:" VerticalAlignment="Center" />
|
||||
<ComboBox Grid.Column="1"
|
||||
Name="cmbInterfaces"
|
||||
Margin="5,0"
|
||||
VerticalAlignment="Center"
|
||||
SelectionChanged="cmbInterfaces_SelectionChanged">
|
||||
<Label
|
||||
Grid.Column="0"
|
||||
Content="选择网络接口:"
|
||||
VerticalAlignment="Center" />
|
||||
<ComboBox
|
||||
Grid.Column="1"
|
||||
Margin="5,0"
|
||||
VerticalAlignment="Center"
|
||||
ItemsSource="{Binding NetworkInterfaces}"
|
||||
SelectedItem="{Binding SelectedInterface}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition
|
||||
Width="Auto" />
|
||||
<ColumnDefinition
|
||||
Width="*" />
|
||||
<ColumnDefinition
|
||||
Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Grid.Column="0" Margin="0,0,10,0">
|
||||
<TextBlock.Text>
|
||||
<Binding Path="Name" />
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock
|
||||
Grid.Column="0"
|
||||
Text="{Binding Name}"
|
||||
Margin="0,0,10,0" />
|
||||
|
||||
<TextBlock Grid.Column="1" Foreground="Gray">
|
||||
<TextBlock.Text>
|
||||
<Binding Path="Description" />
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock
|
||||
Grid.Column="1"
|
||||
Text="{Binding Description}"
|
||||
Foreground="Gray" />
|
||||
|
||||
<TextBlock Grid.Column="2" Margin="10,0,0,0">
|
||||
<TextBlock
|
||||
Grid.Column="2"
|
||||
Margin="10,0,0,0">
|
||||
<TextBlock.Style>
|
||||
<Style TargetType="TextBlock">
|
||||
<Style
|
||||
TargetType="TextBlock">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsConnected}" Value="True">
|
||||
<Setter Property="Text" Value="[已连接]" />
|
||||
<Setter Property="Foreground" Value="Green" />
|
||||
<DataTrigger
|
||||
Binding="{Binding IsConnected}"
|
||||
Value="True">
|
||||
<Setter
|
||||
Property="Text"
|
||||
Value="[已连接]" />
|
||||
<Setter
|
||||
Property="Foreground"
|
||||
Value="Green" />
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding IsConnected}" Value="False">
|
||||
<Setter Property="Text" Value="[未连接]" />
|
||||
<Setter Property="Foreground" Value="Red" />
|
||||
<DataTrigger
|
||||
Binding="{Binding IsConnected}"
|
||||
Value="False">
|
||||
<Setter
|
||||
Property="Text"
|
||||
Value="[未连接]" />
|
||||
<Setter
|
||||
Property="Foreground"
|
||||
Value="Red" />
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
@@ -70,102 +103,149 @@
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
<Button Grid.Column="2"
|
||||
Name="btnRefresh"
|
||||
Content="刷新"
|
||||
Padding="10,5"
|
||||
Click="btnRefresh_Click" />
|
||||
<Button
|
||||
Grid.Column="2"
|
||||
Content="刷新"
|
||||
Padding="10,5"
|
||||
Command="{Binding RefreshCommand}" />
|
||||
</Grid>
|
||||
|
||||
<!-- 网速显示区域 -->
|
||||
<Border Grid.Row="2"
|
||||
BorderBrush="#CCCCCC"
|
||||
BorderThickness="1"
|
||||
CornerRadius="5"
|
||||
Background="#F9F9F9">
|
||||
<Grid Margin="20">
|
||||
<Border
|
||||
Grid.Row="2"
|
||||
BorderBrush="#CCCCCC"
|
||||
BorderThickness="1"
|
||||
CornerRadius="5"
|
||||
Background="#F9F9F9">
|
||||
<Grid
|
||||
Margin="20">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="20" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="20" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition
|
||||
Height="Auto" />
|
||||
<RowDefinition
|
||||
Height="20" />
|
||||
<RowDefinition
|
||||
Height="Auto" />
|
||||
<RowDefinition
|
||||
Height="Auto" />
|
||||
<RowDefinition
|
||||
Height="Auto" />
|
||||
<RowDefinition
|
||||
Height="20" />
|
||||
<RowDefinition
|
||||
Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0"
|
||||
Text="实时网速监控"
|
||||
FontSize="18"
|
||||
FontWeight="Bold"
|
||||
HorizontalAlignment="Center" />
|
||||
<TextBlock
|
||||
Grid.Row="0"
|
||||
Text="实时网速监控"
|
||||
FontSize="18"
|
||||
FontWeight="Bold"
|
||||
HorizontalAlignment="Center" />
|
||||
|
||||
<!-- 下载速度 -->
|
||||
<Grid Grid.Row="2">
|
||||
<Grid
|
||||
Grid.Row="2">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition
|
||||
Width="120" />
|
||||
<ColumnDefinition
|
||||
Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0" Orientation="Horizontal">
|
||||
<TextBlock Text="▼" Foreground="Green" FontWeight="Bold" Margin="0,0,5,0" />
|
||||
<TextBlock Text="下载速度:" FontSize="14" VerticalAlignment="Center" />
|
||||
<StackPanel
|
||||
Grid.Column="0"
|
||||
Orientation="Horizontal">
|
||||
<TextBlock
|
||||
Text="▼"
|
||||
Foreground="Green"
|
||||
FontWeight="Bold"
|
||||
Margin="0,0,5,0" />
|
||||
<TextBlock
|
||||
Text="下载速度:"
|
||||
FontSize="14"
|
||||
VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
<TextBlock Grid.Column="1"
|
||||
Name="txtDownloadSpeed"
|
||||
Text="0.00 B/s"
|
||||
FontSize="16"
|
||||
FontWeight="Bold"
|
||||
Foreground="Green"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock
|
||||
Grid.Column="1"
|
||||
Text="{Binding DownloadSpeed}"
|
||||
FontSize="16"
|
||||
FontWeight="Bold"
|
||||
Foreground="Green"
|
||||
VerticalAlignment="Center" />
|
||||
</Grid>
|
||||
|
||||
<!-- 上传速度 -->
|
||||
<Grid Grid.Row="3" Margin="0,10">
|
||||
<Grid
|
||||
Grid.Row="3"
|
||||
Margin="0,10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition
|
||||
Width="120" />
|
||||
<ColumnDefinition
|
||||
Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0" Orientation="Horizontal">
|
||||
<TextBlock Text="▲" Foreground="Blue" FontWeight="Bold" Margin="0,0,5,0" />
|
||||
<TextBlock Text="上传速度:" FontSize="14" VerticalAlignment="Center" />
|
||||
<StackPanel
|
||||
Grid.Column="0"
|
||||
Orientation="Horizontal">
|
||||
<TextBlock
|
||||
Text="▲"
|
||||
Foreground="Blue"
|
||||
FontWeight="Bold"
|
||||
Margin="0,0,5,0" />
|
||||
<TextBlock
|
||||
Text="上传速度:"
|
||||
FontSize="14"
|
||||
VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
<TextBlock Grid.Column="1"
|
||||
Name="txtUploadSpeed"
|
||||
Text="0.00 B/s"
|
||||
FontSize="16"
|
||||
FontWeight="Bold"
|
||||
Foreground="Blue"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock
|
||||
Grid.Column="1"
|
||||
Text="{Binding UploadSpeed}"
|
||||
FontSize="16"
|
||||
FontWeight="Bold"
|
||||
Foreground="Blue"
|
||||
VerticalAlignment="Center" />
|
||||
</Grid>
|
||||
|
||||
<!-- 总速度 -->
|
||||
<Grid Grid.Row="4">
|
||||
<Grid
|
||||
Grid.Row="4">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition
|
||||
Width="120" />
|
||||
<ColumnDefinition
|
||||
Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Grid.Column="0" Orientation="Horizontal">
|
||||
<TextBlock Text="Σ" Foreground="DarkOrange" FontWeight="Bold" FontSize="16" Margin="0,0,5,0" />
|
||||
<TextBlock Text="总速度:" FontSize="14" VerticalAlignment="Center" />
|
||||
<StackPanel
|
||||
Grid.Column="0"
|
||||
Orientation="Horizontal">
|
||||
<TextBlock
|
||||
Text="Σ"
|
||||
Foreground="DarkOrange"
|
||||
FontWeight="Bold"
|
||||
FontSize="16"
|
||||
Margin="0,0,5,0" />
|
||||
<TextBlock
|
||||
Text="总速度:"
|
||||
FontSize="14"
|
||||
VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
<TextBlock Grid.Column="1"
|
||||
Name="txtTotalSpeed"
|
||||
Text="0.00 B/s"
|
||||
FontSize="16"
|
||||
FontWeight="Bold"
|
||||
Foreground="DarkOrange"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock
|
||||
Grid.Column="1"
|
||||
Text="{Binding TotalSpeed}"
|
||||
FontSize="16"
|
||||
FontWeight="Bold"
|
||||
Foreground="DarkOrange"
|
||||
VerticalAlignment="Center" />
|
||||
</Grid>
|
||||
|
||||
<!-- 状态信息 -->
|
||||
<TextBlock Grid.Row="6"
|
||||
Name="txtStatus"
|
||||
Text="请选择一个网络接口"
|
||||
FontSize="12"
|
||||
Foreground="Gray"
|
||||
HorizontalAlignment="Center" />
|
||||
<TextBlock
|
||||
Grid.Row="6"
|
||||
Text="{Binding StatusText}"
|
||||
FontSize="12"
|
||||
Foreground="Gray"
|
||||
HorizontalAlignment="Center" />
|
||||
</Grid>
|
||||
</Border>
|
||||
</Grid>
|
||||
|
||||
</UserControl>
|
||||
@@ -1,8 +1,7 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Threading;
|
||||
using Common.Logger;
|
||||
using Common.Utils;
|
||||
using Shell.ViewModel;
|
||||
|
||||
namespace Shell.UserControls
|
||||
{
|
||||
@@ -11,194 +10,32 @@ namespace Shell.UserControls
|
||||
/// </summary>
|
||||
public partial class NetworkMonitorView : UserControl
|
||||
{
|
||||
private DispatcherTimer updateTimer;
|
||||
private List<NetworkMonitorHelper1.NetworkInterface> interfaces;
|
||||
private const string SELECT_ALL_TEXT = "选择全部";
|
||||
private NetworkMonitorViewModel _viewModel;
|
||||
|
||||
public NetworkMonitorView()
|
||||
{
|
||||
InitializeComponent();
|
||||
Log.Info("NetworkMonitorControl 初始化");
|
||||
Log.Info("NetworkMonitorView 初始化");
|
||||
|
||||
// 初始化定时器
|
||||
updateTimer = new DispatcherTimer();
|
||||
updateTimer.Interval = TimeSpan.FromSeconds(1); // 每秒更新一次
|
||||
updateTimer.Tick += UpdateTimer_Tick;
|
||||
// 创建并设置ViewModel
|
||||
_viewModel = new NetworkMonitorViewModel();
|
||||
DataContext = _viewModel;
|
||||
|
||||
// 控件加载完成后初始化
|
||||
Loaded += NetworkMonitorControl_Loaded;
|
||||
Unloaded += NetworkMonitorControl_Unloaded;
|
||||
|
||||
// NewMethod();
|
||||
// 订阅控件生命周期事件
|
||||
Loaded += NetworkMonitorView_Loaded;
|
||||
Unloaded += NetworkMonitorView_Unloaded;
|
||||
}
|
||||
|
||||
private static async Task NewMethod()
|
||||
private void NetworkMonitorView_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// 获取详细信息
|
||||
var interfaces = EnhancedNetworkMonitor.GetDetailedNetworkInterfaces();
|
||||
var json = JsonHelper.ToJson(interfaces);
|
||||
Console.WriteLine(json);
|
||||
// 获取完整报告
|
||||
var report = await EnhancedNetworkMonitor.GetNetworkStatusReportAsync();
|
||||
Console.WriteLine(report);
|
||||
|
||||
// 获取外网IP
|
||||
var externalIP = await EnhancedNetworkMonitor.GetExternalIPAsync();
|
||||
Console.WriteLine(externalIP);
|
||||
Log.Info("NetworkMonitorView 加载完成");
|
||||
}
|
||||
|
||||
private void NetworkMonitorControl_Loaded(object sender, RoutedEventArgs e)
|
||||
private void NetworkMonitorView_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Log.Info("NetworkMonitorControl 加载完成");
|
||||
LoadNetworkInterfaces();
|
||||
updateTimer.Start();
|
||||
}
|
||||
|
||||
private void NetworkMonitorControl_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Log.Info("NetworkMonitorControl 卸载");
|
||||
updateTimer.Stop();
|
||||
}
|
||||
|
||||
private void LoadNetworkInterfaces()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取所有网络接口
|
||||
interfaces = NetworkMonitorHelper1.GetNetworkInterfaces();
|
||||
|
||||
// 创建下拉框项目列表
|
||||
var comboItems = new List<object>();
|
||||
|
||||
// 添加"选择全部"选项
|
||||
comboItems.Add(new
|
||||
{
|
||||
Name = SELECT_ALL_TEXT,
|
||||
Description = "显示所有网络接口的总网速",
|
||||
TypeDescription = "",
|
||||
IsConnected = true
|
||||
});
|
||||
|
||||
// 添加所有非环回接口
|
||||
foreach (var iface in interfaces)
|
||||
{
|
||||
// 添加所有接口到下拉框(环回接口已经在工具类中过滤)
|
||||
comboItems.Add(iface);
|
||||
}
|
||||
|
||||
// 更新下拉框
|
||||
cmbInterfaces.ItemsSource = comboItems;
|
||||
|
||||
// 默认选择第一项(选择全部)
|
||||
if (comboItems.Count > 0)
|
||||
{
|
||||
cmbInterfaces.SelectedIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
txtStatus.Text = "未找到可用的网络接口";
|
||||
Log.Warning("未找到任何网络接口");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error4Ctx($"加载网络接口时发生错误: {ex.Message}");
|
||||
txtStatus.Text = $"加载失败: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (cmbInterfaces.SelectedItem == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
dynamic selectedItem = cmbInterfaces.SelectedItem;
|
||||
NetworkMonitorHelper1.NetworkSpeed speed;
|
||||
|
||||
if (selectedItem.Name == SELECT_ALL_TEXT)
|
||||
{
|
||||
// 计算所有接口的总网速
|
||||
speed = NetworkMonitorHelper1.CalculateTotalSpeed();
|
||||
txtStatus.Text = "显示所有网络接口的总网速";
|
||||
}
|
||||
else
|
||||
{
|
||||
// 计算指定接口的网速
|
||||
var networkInterface = selectedItem as NetworkMonitorHelper1.NetworkInterface;
|
||||
if (networkInterface != null)
|
||||
{
|
||||
speed = NetworkMonitorHelper1.CalculateSpeedByName(networkInterface.Name);
|
||||
|
||||
// 显示接口状态
|
||||
string statusText = $"正在监控: {networkInterface.Name} ({networkInterface.TypeDescription})";
|
||||
if (!networkInterface.IsConnected)
|
||||
{
|
||||
statusText += " - 未连接";
|
||||
}
|
||||
|
||||
txtStatus.Text = statusText;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新界面显示
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
txtDownloadSpeed.Text = speed.DownloadSpeedText;
|
||||
txtUploadSpeed.Text = speed.UploadSpeedText;
|
||||
txtTotalSpeed.Text = speed.TotalSpeedText;
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error4Ctx($"更新网速时发生错误: {ex.Message}");
|
||||
txtStatus.Text = $"更新失败: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
private void cmbInterfaces_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
Log.Info("用户切换了网络接口选择");
|
||||
|
||||
// 重置网速显示
|
||||
txtDownloadSpeed.Text = "0.00 B/s";
|
||||
txtUploadSpeed.Text = "0.00 B/s";
|
||||
txtTotalSpeed.Text = "0.00 B/s";
|
||||
|
||||
// 重置统计数据,以便立即开始新的统计
|
||||
NetworkMonitorHelper1.ResetStatistics();
|
||||
|
||||
if (cmbInterfaces.SelectedItem != null)
|
||||
{
|
||||
dynamic selectedItem = cmbInterfaces.SelectedItem;
|
||||
Log.Info($"选择了: {selectedItem.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
private void btnRefresh_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Log.Info("用户点击了刷新按钮");
|
||||
|
||||
// 停止定时器
|
||||
updateTimer.Stop();
|
||||
|
||||
// 重置统计数据
|
||||
NetworkMonitorHelper1.ResetStatistics();
|
||||
|
||||
// 重新加载接口
|
||||
LoadNetworkInterfaces();
|
||||
|
||||
// 重启定时器
|
||||
updateTimer.Start();
|
||||
|
||||
txtStatus.Text = "接口列表已刷新";
|
||||
Log.Info("NetworkMonitorView 卸载");
|
||||
// 释放ViewModel资源
|
||||
_viewModel?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,483 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using Common.Logger;
|
||||
using Common.Utils;
|
||||
|
||||
namespace Shell.ViewModel;
|
||||
|
||||
public class NetworkMonitorViewModel : INotifyPropertyChanged
|
||||
{
|
||||
private readonly DispatcherTimer _updateTimer;
|
||||
private List<NetworkMonitorHelper1.NetworkInterface> _interfaces;
|
||||
private const string SELECT_ALL_TEXT = "选择全部";
|
||||
private readonly bool _isDesignMode;
|
||||
|
||||
#region Properties
|
||||
|
||||
private ObservableCollection<NetworkInterfaceItem> _networkInterfaces;
|
||||
|
||||
public ObservableCollection<NetworkInterfaceItem> NetworkInterfaces
|
||||
{
|
||||
get => _networkInterfaces;
|
||||
set
|
||||
{
|
||||
_networkInterfaces = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private NetworkInterfaceItem _selectedInterface;
|
||||
|
||||
public NetworkInterfaceItem SelectedInterface
|
||||
{
|
||||
get => _selectedInterface;
|
||||
set
|
||||
{
|
||||
_selectedInterface = value;
|
||||
OnPropertyChanged();
|
||||
OnSelectedInterfaceChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string _downloadSpeed = "0.00 B/s";
|
||||
|
||||
public string DownloadSpeed
|
||||
{
|
||||
get => _downloadSpeed;
|
||||
set
|
||||
{
|
||||
_downloadSpeed = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string _uploadSpeed = "0.00 B/s";
|
||||
|
||||
public string UploadSpeed
|
||||
{
|
||||
get => _uploadSpeed;
|
||||
set
|
||||
{
|
||||
_uploadSpeed = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string _totalSpeed = "0.00 B/s";
|
||||
|
||||
public string TotalSpeed
|
||||
{
|
||||
get => _totalSpeed;
|
||||
set
|
||||
{
|
||||
_totalSpeed = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string _statusText = "请选择一个网络接口";
|
||||
|
||||
public string StatusText
|
||||
{
|
||||
get => _statusText;
|
||||
set
|
||||
{
|
||||
_statusText = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
|
||||
public ICommand RefreshCommand { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public NetworkMonitorViewModel()
|
||||
{
|
||||
// 检查是否在设计模式
|
||||
_isDesignMode =
|
||||
System.ComponentModel.DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject());
|
||||
|
||||
NetworkInterfaces = new ObservableCollection<NetworkInterfaceItem>();
|
||||
RefreshCommand = new RelayCommand(RefreshNetworkInterfaces);
|
||||
|
||||
if (_isDesignMode)
|
||||
{
|
||||
// 设计时数据
|
||||
LoadDesignTimeData();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 运行时数据
|
||||
Log.Info("NetworkMonitorViewModel 初始化");
|
||||
|
||||
// 初始化定时器
|
||||
_updateTimer = new DispatcherTimer();
|
||||
_updateTimer.Interval = TimeSpan.FromSeconds(1);
|
||||
_updateTimer.Tick += UpdateTimer_Tick;
|
||||
|
||||
// 初始化数据
|
||||
LoadNetworkInterfaces();
|
||||
_updateTimer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private void LoadDesignTimeData()
|
||||
{
|
||||
// 设计时假数据
|
||||
NetworkInterfaces.Clear();
|
||||
|
||||
// 添加"选择全部"选项
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = SELECT_ALL_TEXT,
|
||||
Description = "显示所有网络接口的总网速",
|
||||
TypeDescription = "",
|
||||
IsConnected = true,
|
||||
IsSelectAll = true
|
||||
});
|
||||
|
||||
// 添加假的网络接口
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = "以太网",
|
||||
Description = "Realtek PCIe GBE Family Controller",
|
||||
TypeDescription = "以太网",
|
||||
IsConnected = true,
|
||||
IsSelectAll = false
|
||||
});
|
||||
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = "Wi-Fi",
|
||||
Description = "Intel(R) Wi-Fi 6 AX201 160MHz",
|
||||
TypeDescription = "无线网络",
|
||||
IsConnected = true,
|
||||
IsSelectAll = false
|
||||
});
|
||||
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = "蓝牙网络连接",
|
||||
Description = "Bluetooth Device (Personal Area Network)",
|
||||
TypeDescription = "蓝牙",
|
||||
IsConnected = false,
|
||||
IsSelectAll = false
|
||||
});
|
||||
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = "VMware Network Adapter",
|
||||
Description = "VMware Virtual Ethernet Adapter",
|
||||
TypeDescription = "虚拟网卡",
|
||||
IsConnected = true,
|
||||
IsSelectAll = false
|
||||
});
|
||||
|
||||
// 设置默认选中项
|
||||
SelectedInterface = NetworkInterfaces[1]; // 选中以太网
|
||||
|
||||
// 设置假的网速数据
|
||||
DownloadSpeed = "15.67 MB/s";
|
||||
UploadSpeed = "2.34 MB/s";
|
||||
TotalSpeed = "18.01 MB/s";
|
||||
StatusText = "正在监控: 以太网 (以太网) - 已连接";
|
||||
}
|
||||
|
||||
private void LoadNetworkInterfaces()
|
||||
{
|
||||
if (_isDesignMode)
|
||||
{
|
||||
LoadDesignTimeData();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 获取所有网络接口
|
||||
_interfaces = NetworkMonitorHelper1.GetNetworkInterfaces();
|
||||
|
||||
// 清除现有项目
|
||||
NetworkInterfaces.Clear();
|
||||
|
||||
// 添加"选择全部"选项
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = SELECT_ALL_TEXT,
|
||||
Description = "显示所有网络接口的总网速",
|
||||
TypeDescription = "",
|
||||
IsConnected = true,
|
||||
IsSelectAll = true
|
||||
});
|
||||
|
||||
// 添加所有非环回接口
|
||||
foreach (var iface in _interfaces)
|
||||
{
|
||||
NetworkInterfaces.Add(new NetworkInterfaceItem
|
||||
{
|
||||
Name = iface.Name,
|
||||
Description = iface.Description,
|
||||
TypeDescription = iface.TypeDescription,
|
||||
IsConnected = iface.IsConnected,
|
||||
IsSelectAll = false,
|
||||
NetworkInterface = iface
|
||||
});
|
||||
}
|
||||
|
||||
// 默认选择第一项(选择全部)
|
||||
if (NetworkInterfaces.Count > 0)
|
||||
{
|
||||
SelectedInterface = NetworkInterfaces[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusText = "未找到可用的网络接口";
|
||||
Log.Warning("未找到任何网络接口");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error4Ctx($"加载网络接口时发生错误: {ex.Message}");
|
||||
StatusText = $"加载失败: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSelectedInterfaceChanged()
|
||||
{
|
||||
if (SelectedInterface == null) return;
|
||||
|
||||
if (_isDesignMode)
|
||||
{
|
||||
// 设计时假数据更新
|
||||
UpdateDesignTimeSpeed();
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Info($"用户切换了网络接口选择: {SelectedInterface.Name}");
|
||||
|
||||
// 重置网速显示
|
||||
DownloadSpeed = "0.00 B/s";
|
||||
UploadSpeed = "0.00 B/s";
|
||||
TotalSpeed = "0.00 B/s";
|
||||
|
||||
// 重置统计数据,以便立即开始新的统计
|
||||
NetworkMonitorHelper1.ResetStatistics();
|
||||
}
|
||||
|
||||
private void UpdateDesignTimeSpeed()
|
||||
{
|
||||
if (SelectedInterface == null) return;
|
||||
|
||||
// 根据选择的接口显示不同的假数据
|
||||
if (SelectedInterface.IsSelectAll)
|
||||
{
|
||||
DownloadSpeed = "25.89 MB/s";
|
||||
UploadSpeed = "3.45 MB/s";
|
||||
TotalSpeed = "29.34 MB/s";
|
||||
StatusText = "显示所有网络接口的总网速";
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (SelectedInterface.Name)
|
||||
{
|
||||
case "以太网":
|
||||
DownloadSpeed = "15.67 MB/s";
|
||||
UploadSpeed = "2.34 MB/s";
|
||||
TotalSpeed = "18.01 MB/s";
|
||||
StatusText = "正在监控: 以太网 (以太网) - 已连接";
|
||||
break;
|
||||
case "Wi-Fi":
|
||||
DownloadSpeed = "8.45 MB/s";
|
||||
UploadSpeed = "1.23 MB/s";
|
||||
TotalSpeed = "9.68 MB/s";
|
||||
StatusText = "正在监控: Wi-Fi (无线网络) - 已连接";
|
||||
break;
|
||||
case "蓝牙网络连接":
|
||||
DownloadSpeed = "0.00 B/s";
|
||||
UploadSpeed = "0.00 B/s";
|
||||
TotalSpeed = "0.00 B/s";
|
||||
StatusText = "正在监控: 蓝牙网络连接 (蓝牙) - 未连接";
|
||||
break;
|
||||
case "VMware Network Adapter":
|
||||
DownloadSpeed = "1.77 MB/s";
|
||||
UploadSpeed = "0.88 KB/s";
|
||||
TotalSpeed = "1.78 MB/s";
|
||||
StatusText = "正在监控: VMware Network Adapter (虚拟网卡) - 已连接";
|
||||
break;
|
||||
default:
|
||||
DownloadSpeed = "0.00 B/s";
|
||||
UploadSpeed = "0.00 B/s";
|
||||
TotalSpeed = "0.00 B/s";
|
||||
StatusText = $"正在监控: {SelectedInterface.Name}";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (_isDesignMode) return;
|
||||
|
||||
if (SelectedInterface == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
NetworkMonitorHelper1.NetworkSpeed speed;
|
||||
|
||||
if (SelectedInterface.IsSelectAll)
|
||||
{
|
||||
// 计算所有接口的总网速
|
||||
speed = NetworkMonitorHelper1.CalculateTotalSpeed();
|
||||
StatusText = "显示所有网络接口的总网速";
|
||||
}
|
||||
else
|
||||
{
|
||||
// 计算指定接口的网速
|
||||
var networkInterface = SelectedInterface.NetworkInterface;
|
||||
if (networkInterface != null)
|
||||
{
|
||||
speed = NetworkMonitorHelper1.CalculateSpeedByName(networkInterface.Name);
|
||||
|
||||
// 显示接口状态
|
||||
string statusText = $"正在监控: {networkInterface.Name} ({networkInterface.TypeDescription})";
|
||||
if (!networkInterface.IsConnected)
|
||||
{
|
||||
statusText += " - 未连接";
|
||||
}
|
||||
|
||||
StatusText = statusText;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新界面显示
|
||||
DownloadSpeed = speed.DownloadSpeedText;
|
||||
UploadSpeed = speed.UploadSpeedText;
|
||||
TotalSpeed = speed.TotalSpeedText;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error4Ctx($"更新网速时发生错误: {ex.Message}");
|
||||
StatusText = $"更新失败: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshNetworkInterfaces()
|
||||
{
|
||||
if (_isDesignMode)
|
||||
{
|
||||
LoadDesignTimeData();
|
||||
StatusText = "接口列表已刷新 (设计时数据)";
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Info("用户点击了刷新按钮");
|
||||
|
||||
// 停止定时器
|
||||
_updateTimer.Stop();
|
||||
|
||||
// 重置统计数据
|
||||
NetworkMonitorHelper1.ResetStatistics();
|
||||
|
||||
// 重新加载接口
|
||||
LoadNetworkInterfaces();
|
||||
|
||||
// 重启定时器
|
||||
_updateTimer.Start();
|
||||
|
||||
StatusText = "接口列表已刷新";
|
||||
}
|
||||
|
||||
// 测试用的异步方法
|
||||
private static async Task TestNetworkMethods()
|
||||
{
|
||||
// 获取详细信息
|
||||
var interfaces = EnhancedNetworkMonitor.GetDetailedNetworkInterfaces();
|
||||
var json = JsonHelper.ToJson(interfaces);
|
||||
Console.WriteLine(json);
|
||||
|
||||
// 获取完整报告
|
||||
var report = await EnhancedNetworkMonitor.GetNetworkStatusReportAsync();
|
||||
Console.WriteLine(report);
|
||||
|
||||
// 获取外网IP
|
||||
var externalIP = await EnhancedNetworkMonitor.GetExternalIPAsync();
|
||||
Console.WriteLine(externalIP);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region INotifyPropertyChanged Implementation
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Implementation
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isDesignMode) return;
|
||||
|
||||
Log.Info("NetworkMonitorViewModel 销毁");
|
||||
_updateTimer?.Stop();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region Helper Classes
|
||||
|
||||
public class NetworkInterfaceItem
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string TypeDescription { get; set; }
|
||||
public bool IsConnected { get; set; }
|
||||
public bool IsSelectAll { get; set; }
|
||||
public NetworkMonitorHelper1.NetworkInterface NetworkInterface { get; set; }
|
||||
}
|
||||
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
private readonly Action _execute;
|
||||
private readonly Func<bool> _canExecute;
|
||||
|
||||
public RelayCommand(Action execute, Func<bool> canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add => CommandManager.RequerySuggested += value;
|
||||
remove => CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
|
||||
|
||||
public void Execute(object parameter) => _execute();
|
||||
}
|
||||
|
||||
#endregion
|
||||
Reference in new issue
Block a user