A:数据持久化内容获取

This commit is contained in:
jimmy committed 2025-08-09 10:17:08 +08:00
1 parent 2a3e2eba65
commit af24643b21
2 files changed
+86 -1

No files matched your search

@@ -7,12 +7,13 @@ using Common.Logger;
using Common.Utils;
using Utils.ETW.Services;
using Shell.Models;
using Shell.UserControls;
using Data;
using Utils.ETW.Models;
namespace Shell.ViewModel;
public class NetworkMonitorViewModel : INotifyPropertyChanged
public class NetworkMonitorViewModel : INotifyPropertyChanged, ISettingsAware
{
private readonly DispatcherTimer _updateTimer;
private List<NetworkMonitorHelper.NetworkInterface> _interfaces;
@@ -637,6 +638,33 @@ public class NetworkMonitorViewModel : INotifyPropertyChanged
#region IDisposable Implementation
/// <summary>
/// 实现ISettingsAware接口,应用设置到ViewModel
/// </summary>
/// <param name="settings">要应用的设置</param>
public void ApplySettings(NetViewSettings settings)
{
try
{
Log.Info("NetworkMonitorViewModel 开始应用设置");
// 这里可以根据设置更新ViewModel的行为
// 例如更新速度单位显示格式、Top榜显示数量等
// 触发属性更改通知,让UI重新渲染
OnPropertyChanged(nameof(DownloadSpeed));
OnPropertyChanged(nameof(UploadSpeed));
OnPropertyChanged(nameof(TotalSpeed));
OnPropertyChanged(nameof(TopProcesses));
Log.Info("NetworkMonitorViewModel 设置应用完成");
}
catch (Exception ex)
{
Log.Error4Ctx($"NetworkMonitorViewModel 应用设置失败: {ex.Message}");
}
}
public void Dispose()
{
if (_isDesignMode) return;
@@ -110,6 +110,9 @@ public partial class NetView : Window
this.Topmost = _appSettings.IsTopmost;
SetClickThrough(_appSettings.IsClickThrough);
// 同步数据库设置到NetViewSettings实例
SyncDatabaseSettingsToNetViewSettings();
// 更新菜单项的选中状态
if (LockPositionMenuItem != null)
LockPositionMenuItem.IsChecked = _appSettings.IsPositionLocked;
@@ -123,6 +126,60 @@ public partial class NetView : Window
DetailedInfoMenuItem.IsChecked = _appSettings.ShowDetailedInfo;
}
private void SyncDatabaseSettingsToNetViewSettings()
{
try
{
var netViewSettings = Shell.Models.NetViewSettings.Instance;
// 同步外观设置
netViewSettings.TextColor = _appSettings.TextColor;
netViewSettings.BackgroundColor = _appSettings.BackgroundColor;
netViewSettings.BackgroundOpacity = _appSettings.Opacity;
// 同步显示设置
netViewSettings.SpeedUnit = _appSettings.SpeedUnit switch
{
0 => Shell.Models.SpeedUnit.Bytes,
1 => Shell.Models.SpeedUnit.KB,
2 => Shell.Models.SpeedUnit.MB,
3 => Shell.Models.SpeedUnit.Auto,
_ => Shell.Models.SpeedUnit.Auto
};
netViewSettings.ShowUnit = _appSettings.ShowUnit;
netViewSettings.LayoutDirection = _appSettings.LayoutDirection == 0
? Shell.Models.LayoutDirection.Horizontal
: Shell.Models.LayoutDirection.Vertical;
// 同步网速Top榜设置
netViewSettings.ShowNetworkTopList = _appSettings.ShowNetworkTopList;
netViewSettings.NetworkTopListCount = _appSettings.NetworkTopListCount;
// 同步交互设置
netViewSettings.DoubleClickAction = _appSettings.DoubleClickAction switch
{
0 => Shell.Models.DoubleClickAction.None,
1 => Shell.Models.DoubleClickAction.TrafficAnalysis,
2 => Shell.Models.DoubleClickAction.Settings,
_ => Shell.Models.DoubleClickAction.None
};
// 同步窗口行为设置
netViewSettings.LockPosition = _appSettings.IsPositionLocked;
netViewSettings.ClickThrough = _appSettings.IsClickThrough;
netViewSettings.Topmost = _appSettings.IsTopmost;
netViewSettings.SnapToScreen = _appSettings.SnapToScreen;
netViewSettings.ShowDetailedInfo = _appSettings.ShowDetailedInfo;
System.Diagnostics.Debug.WriteLine("已将数据库设置同步到NetViewSettings实例");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"同步数据库设置到NetViewSettings失败: {ex.Message}");
}
}
private async Task SaveSettingsToDatabase()
{
try