From 569e93949274a245735282ff5b515e0cbe0be669 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 20 Jul 2025 14:16:19 +0800 Subject: [PATCH] Optimizing and improving code --- .../Common/{StringEx.cs => Extension.cs} | 7 +- v2rayN/ServiceLib/Common/Utils.cs | 8 ++ .../Views/AddServer2Window.axaml.cs | 8 +- .../Views/AddServerWindow.axaml.cs | 93 +++++---------- .../Views/DNSSettingWindow.axaml.cs | 20 +--- .../v2rayN.Desktop/Views/MainWindow.axaml.cs | 1 - .../Views/OptionSettingWindow.axaml.cs | 92 ++++---------- .../Views/RoutingRuleDetailsWindow.axaml.cs | 15 +-- .../Views/RoutingRuleSettingWindow.axaml.cs | 12 +- .../Views/RoutingSettingWindow.axaml.cs | 15 +-- .../Views/SubEditWindow.axaml.cs | 5 +- .../Views/ThemeSettingView.axaml.cs | 17 +-- v2rayN/v2rayN/Views/AddServer2Window.xaml.cs | 8 +- v2rayN/v2rayN/Views/AddServerWindow.xaml.cs | 93 +++++---------- v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs | 20 +--- v2rayN/v2rayN/Views/MsgView.xaml.cs | 7 +- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 112 +++++------------- .../Views/RoutingRuleDetailsWindow.xaml.cs | 22 +--- .../Views/RoutingRuleSettingWindow.xaml.cs | 12 +- .../v2rayN/Views/RoutingSettingWindow.xaml.cs | 15 +-- v2rayN/v2rayN/Views/SubEditWindow.xaml.cs | 5 +- v2rayN/v2rayN/Views/ThemeSettingView.xaml.cs | 18 +-- 22 files changed, 164 insertions(+), 441 deletions(-) rename v2rayN/ServiceLib/Common/{StringEx.cs => Extension.cs} (91%) diff --git a/v2rayN/ServiceLib/Common/StringEx.cs b/v2rayN/ServiceLib/Common/Extension.cs similarity index 91% rename from v2rayN/ServiceLib/Common/StringEx.cs rename to v2rayN/ServiceLib/Common/Extension.cs index 1b880b74..49d59534 100644 --- a/v2rayN/ServiceLib/Common/StringEx.cs +++ b/v2rayN/ServiceLib/Common/Extension.cs @@ -2,7 +2,7 @@ using System.Diagnostics.CodeAnalysis; namespace ServiceLib.Common; -public static class StringEx +public static class Extension { public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value) { @@ -79,4 +79,9 @@ public static class StringEx { return int.TryParse(value, out var result) ? result : defaultValue; } + + public static List AppendEmpty(this IEnumerable source) + { + return source.Concat(new[] { string.Empty }).ToList(); + } } diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 5d773a16..c5b670cf 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -323,6 +323,14 @@ public class Utils return text.Replace(",", ",").Replace(Environment.NewLine, ","); } + public static List GetEnumNames() where TEnum : Enum + { + return Enum.GetValues(typeof(TEnum)) + .Cast() + .Select(e => e.ToString()) + .ToList(); + } + #endregion 转换函数 #region 数据检查 diff --git a/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs b/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs index 3c96871f..4d378bf9 100644 --- a/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs @@ -21,13 +21,7 @@ public partial class AddServer2Window : WindowBase btnCancel.Click += (s, e) => this.Close(); ViewModel = new AddServer2ViewModel(profileItem, UpdateViewHandler); - foreach (ECoreType it in Enum.GetValues(typeof(ECoreType))) - { - if (it == ECoreType.v2rayN) - continue; - cmbCoreType.Items.Add(it.ToString()); - } - cmbCoreType.Items.Add(string.Empty); + cmbCoreType.ItemsSource = Utils.GetEnumNames().Where(t => t != ECoreType.v2rayN.ToString()).ToList().AppendEmpty(); this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs index 64c43f6c..5b86143b 100644 --- a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs @@ -26,41 +26,22 @@ public partial class AddServerWindow : WindowBase ViewModel = new AddServerViewModel(profileItem, UpdateViewHandler); - Global.CoreTypes.ForEach(it => - { - cmbCoreType.Items.Add(it); - }); - cmbCoreType.Items.Add(string.Empty); + cmbCoreType.ItemsSource = Global.CoreTypes.AppendEmpty(); + cmbNetwork.ItemsSource = Global.Networks; + cmbFingerprint.ItemsSource = Global.Fingerprints; + cmbFingerprint2.ItemsSource = Global.Fingerprints; + cmbAllowInsecure.ItemsSource = Global.AllowInsecure; + cmbAlpn.ItemsSource = Global.Alpns; - cmbStreamSecurity.Items.Add(string.Empty); - cmbStreamSecurity.Items.Add(Global.StreamSecurity); - - Global.Networks.ForEach(it => - { - cmbNetwork.Items.Add(it); - }); - Global.Fingerprints.ForEach(it => - { - cmbFingerprint.Items.Add(it); - cmbFingerprint2.Items.Add(it); - }); - Global.AllowInsecure.ForEach(it => - { - cmbAllowInsecure.Items.Add(it); - }); - Global.Alpns.ForEach(it => - { - cmbAlpn.Items.Add(it); - }); + var lstStreamSecurity = new List(); + lstStreamSecurity.Add(string.Empty); + lstStreamSecurity.Add(Global.StreamSecurity); switch (profileItem.ConfigType) { case EConfigType.VMess: gridVMess.IsVisible = true; - Global.VmessSecurities.ForEach(it => - { - cmbSecurity.Items.Add(it); - }); + cmbSecurity.ItemsSource = Global.VmessSecurities; if (profileItem.Security.IsNullOrEmpty()) { profileItem.Security = Global.DefaultSecurity; @@ -69,10 +50,7 @@ public partial class AddServerWindow : WindowBase case EConfigType.Shadowsocks: gridSs.IsVisible = true; - AppHandler.Instance.GetShadowsocksSecurities(profileItem).ForEach(it => - { - cmbSecurity3.Items.Add(it); - }); + cmbSecurity3.ItemsSource = AppHandler.Instance.GetShadowsocksSecurities(profileItem); break; case EConfigType.SOCKS: @@ -82,11 +60,8 @@ public partial class AddServerWindow : WindowBase case EConfigType.VLESS: gridVLESS.IsVisible = true; - cmbStreamSecurity.Items.Add(Global.StreamSecurityReality); - Global.Flows.ForEach(it => - { - cmbFlow5.Items.Add(it); - }); + lstStreamSecurity.Add(Global.StreamSecurityReality); + cmbFlow5.ItemsSource = Global.Flows; if (profileItem.Security.IsNullOrEmpty()) { profileItem.Security = Global.None; @@ -95,11 +70,8 @@ public partial class AddServerWindow : WindowBase case EConfigType.Trojan: gridTrojan.IsVisible = true; - cmbStreamSecurity.Items.Add(Global.StreamSecurityReality); - Global.Flows.ForEach(it => - { - cmbFlow6.Items.Add(it); - }); + lstStreamSecurity.Add(Global.StreamSecurityReality); + cmbFlow6.ItemsSource = Global.Flows; break; case EConfigType.Hysteria2: @@ -119,10 +91,7 @@ public partial class AddServerWindow : WindowBase cmbFingerprint.IsEnabled = false; cmbFingerprint.SelectedValue = string.Empty; - Global.TuicCongestionControls.ForEach(it => - { - cmbHeaderType8.Items.Add(it); - }); + cmbHeaderType8.ItemsSource = Global.TuicCongestionControls; break; case EConfigType.WireGuard: @@ -134,6 +103,7 @@ public partial class AddServerWindow : WindowBase break; } + cmbStreamSecurity.ItemsSource = lstStreamSecurity; gridTlsMore.IsVisible = false; @@ -272,44 +242,41 @@ public partial class AddServerWindow : WindowBase private void SetHeaderType() { - cmbHeaderType.Items.Clear(); + var lstHeaderType = new List(); var network = cmbNetwork.SelectedItem.ToString(); if (network.IsNullOrEmpty()) { - cmbHeaderType.Items.Add(Global.None); + lstHeaderType.Add(Global.None); + cmbHeaderType.ItemsSource = lstHeaderType; + cmbHeaderType.SelectedIndex = 0; return; } if (network == nameof(ETransport.tcp)) { - cmbHeaderType.Items.Add(Global.None); - cmbHeaderType.Items.Add(Global.TcpHeaderHttp); + lstHeaderType.Add(Global.None); + lstHeaderType.Add(Global.TcpHeaderHttp); } else if (network is nameof(ETransport.kcp) or nameof(ETransport.quic)) { - cmbHeaderType.Items.Add(Global.None); - Global.KcpHeaderTypes.ForEach(it => - { - cmbHeaderType.Items.Add(it); - }); + lstHeaderType.Add(Global.None); + lstHeaderType.AddRange(Global.KcpHeaderTypes); } else if (network is nameof(ETransport.xhttp)) { - Global.XhttpMode.ForEach(it => - { - cmbHeaderType.Items.Add(it); - }); + lstHeaderType.AddRange(Global.XhttpMode); } else if (network == nameof(ETransport.grpc)) { - cmbHeaderType.Items.Add(Global.GrpcGunMode); - cmbHeaderType.Items.Add(Global.GrpcMultiMode); + lstHeaderType.Add(Global.GrpcGunMode); + lstHeaderType.Add(Global.GrpcMultiMode); } else { - cmbHeaderType.Items.Add(Global.None); + lstHeaderType.Add(Global.None); } + cmbHeaderType.ItemsSource = lstHeaderType; cmbHeaderType.SelectedIndex = 0; } diff --git a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs index 347093f9..8f1a0f5a 100644 --- a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs @@ -17,22 +17,10 @@ public partial class DNSSettingWindow : WindowBase btnCancel.Click += (s, e) => this.Close(); ViewModel = new DNSSettingViewModel(UpdateViewHandler); - Global.DomainStrategy4Freedoms.ForEach(it => - { - cmbdomainStrategy4Freedom.Items.Add(it); - }); - Global.SingboxDomainStrategy4Out.ForEach(it => - { - cmbdomainStrategy4Out.Items.Add(it); - }); - Global.DomainDNSAddress.ForEach(it => - { - cmbdomainDNSAddress.Items.Add(it); - }); - Global.SingboxDomainDNSAddress.ForEach(it => - { - cmbdomainDNSAddress2.Items.Add(it); - }); + cmbdomainStrategy4Freedom.ItemsSource = Global.DomainStrategy4Freedoms; + cmbdomainStrategy4Out.ItemsSource = Global.SingboxDomainStrategy4Out; + cmbdomainDNSAddress.ItemsSource = Global.DomainDNSAddress; + cmbdomainDNSAddress2.ItemsSource = Global.SingboxDomainDNSAddress; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index c93151cf..ea95ef0e 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -134,7 +134,6 @@ public partial class MainWindow : WindowBase break; } }); - if (Utils.IsWindows()) { diff --git a/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs index f417e498..010488e5 100644 --- a/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs @@ -19,87 +19,39 @@ public partial class OptionSettingWindow : WindowBase ViewModel = new OptionSettingViewModel(UpdateViewHandler); clbdestOverride.SelectionChanged += ClbdestOverride_SelectionChanged; - Global.destOverrideProtocols.ForEach(it => - { - clbdestOverride.Items.Add(it); - }); + clbdestOverride.ItemsSource = Global.destOverrideProtocols; _config.Inbound.First().DestOverride?.ForEach(it => { clbdestOverride.SelectedItems.Add(it); }); - Global.IEProxyProtocols.ForEach(it => - { - cmbsystemProxyAdvancedProtocol.Items.Add(it); - }); - Global.LogLevels.ForEach(it => - { - cmbloglevel.Items.Add(it); - }); - Global.Fingerprints.ForEach(it => - { - cmbdefFingerprint.Items.Add(it); - }); - Global.UserAgent.ForEach(it => - { - cmbdefUserAgent.Items.Add(it); - }); - Global.SingboxMuxs.ForEach(it => - { - cmbmux4SboxProtocol.Items.Add(it); - }); - Global.TunMtus.ForEach(it => - { - cmbMtu.Items.Add(it); - }); - Global.TunStacks.ForEach(it => - { - cmbStack.Items.Add(it); - }); - Global.CoreTypes.ForEach(it => - { - cmbCoreType1.Items.Add(it); - cmbCoreType2.Items.Add(it); - cmbCoreType3.Items.Add(it); - cmbCoreType4.Items.Add(it); - cmbCoreType5.Items.Add(it); - cmbCoreType6.Items.Add(it); - cmbCoreType9.Items.Add(it); - }); + cmbsystemProxyAdvancedProtocol.ItemsSource = Global.IEProxyProtocols; + cmbloglevel.ItemsSource = Global.LogLevels; + cmbdefFingerprint.ItemsSource = Global.Fingerprints; + cmbdefUserAgent.ItemsSource = Global.UserAgent; + cmbmux4SboxProtocol.ItemsSource = Global.SingboxMuxs; + cmbMtu.ItemsSource = Global.TunMtus; + cmbStack.ItemsSource = Global.TunStacks; - for (var i = 2; i <= 8; i++) - { - cmbMixedConcurrencyCount.Items.Add(i); - } - for (var i = 2; i <= 6; i++) - { - cmbSpeedTestTimeout.Items.Add(i * 5); - } + cmbCoreType1.ItemsSource = Global.CoreTypes; + cmbCoreType2.ItemsSource = Global.CoreTypes; + cmbCoreType3.ItemsSource = Global.CoreTypes; + cmbCoreType4.ItemsSource = Global.CoreTypes; + cmbCoreType5.ItemsSource = Global.CoreTypes; + cmbCoreType6.ItemsSource = Global.CoreTypes; + cmbCoreType9.ItemsSource = Global.CoreTypes; + cmbMixedConcurrencyCount.ItemsSource = Enumerable.Range(2, 7).ToList(); + cmbSpeedTestTimeout.ItemsSource = Enumerable.Range(2, 5).Select(i => i * 5).ToList(); cmbSpeedTestUrl.ItemsSource = Global.SpeedTestUrls; cmbSpeedPingTestUrl.ItemsSource = Global.SpeedPingTestUrls; cmbSubConvertUrl.ItemsSource = Global.SubConvertUrls; + cmbGetFilesSourceUrl.ItemsSource = Global.GeoFilesSources; + cmbSrsFilesSourceUrl.ItemsSource = Global.SingboxRulesetSources; + cmbRoutingRulesSourceUrl.ItemsSource = Global.RoutingRulesSources; + cmbIPAPIUrl.ItemsSource = Global.IPAPIUrls; - Global.GeoFilesSources.ForEach(it => - { - cmbGetFilesSourceUrl.Items.Add(it); - }); - Global.SingboxRulesetSources.ForEach(it => - { - cmbSrsFilesSourceUrl.Items.Add(it); - }); - Global.RoutingRulesSources.ForEach(it => - { - cmbRoutingRulesSourceUrl.Items.Add(it); - }); - Global.IPAPIUrls.ForEach(it => - { - cmbIPAPIUrl.Items.Add(it); - }); - foreach (EGirdOrientation it in Enum.GetValues(typeof(EGirdOrientation))) - { - cmbMainGirdOrientation.Items.Add(it.ToString()); - } + cmbMainGirdOrientation.ItemsSource = Utils.GetEnumNames(); this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs index 35791788..05744799 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs @@ -25,18 +25,9 @@ public partial class RoutingRuleDetailsWindow : WindowBase - { - clbProtocol.Items.Add(it); - }); - Global.InboundTags.ForEach(it => - { - clbInboundTag.Items.Add(it); - }); - Global.RuleNetworks.ForEach(it => - { - cmbNetwork.Items.Add(it); - }); + clbProtocol.ItemsSource = Global.RuleProtocols; + clbInboundTag.ItemsSource = Global.InboundTags; + cmbNetwork.ItemsSource = Global.RuleNetworks; if (!rulesItem.Id.IsNullOrEmpty()) { diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs index 37b9a1a8..78ec8833 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs @@ -30,15 +30,9 @@ public partial class RoutingRuleSettingWindow : WindowBase - { - cmbdomainStrategy.Items.Add(it); - }); - cmbdomainStrategy.Items.Add(string.Empty); - Global.DomainStrategies4Singbox.ForEach(it => - { - cmbdomainStrategy4Singbox.Items.Add(it); - }); + + cmbdomainStrategy.ItemsSource = Global.DomainStrategies.AppendEmpty(); + cmbdomainStrategy4Singbox.ItemsSource = Global.DomainStrategies4Singbox; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs index 0e6f69cc..d9095135 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs @@ -26,18 +26,9 @@ public partial class RoutingSettingWindow : WindowBase ViewModel = new RoutingSettingViewModel(UpdateViewHandler); - Global.DomainStrategies.ForEach(it => - { - cmbdomainStrategy.Items.Add(it); - }); - Global.DomainMatchers.ForEach(it => - { - cmbdomainMatcher.Items.Add(it); - }); - Global.DomainStrategies4Singbox.ForEach(it => - { - cmbdomainStrategy4Singbox.Items.Add(it); - }); + cmbdomainStrategy.ItemsSource = Global.DomainStrategies; + cmbdomainMatcher.ItemsSource = Global.DomainMatchers; + cmbdomainStrategy4Singbox.ItemsSource = Global.DomainStrategies4Singbox; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs index b2fcf442..99049018 100644 --- a/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs @@ -22,10 +22,7 @@ public partial class SubEditWindow : WindowBase ViewModel = new SubEditViewModel(subItem, UpdateViewHandler); - Global.SubConvertTargets.ForEach(it => - { - cmbConvertTarget.Items.Add(it); - }); + cmbConvertTarget.ItemsSource = Global.SubConvertTargets; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN.Desktop/Views/ThemeSettingView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/ThemeSettingView.axaml.cs index e8d8d9bb..b1f16b7e 100644 --- a/v2rayN/v2rayN.Desktop/Views/ThemeSettingView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/ThemeSettingView.axaml.cs @@ -16,20 +16,9 @@ public partial class ThemeSettingView : ReactiveUserControl - { - cmbCurrentLanguage.Items.Add(it); - }); + cmbCurrentTheme.ItemsSource = Utils.GetEnumNames(); + cmbCurrentFontSize.ItemsSource = Enumerable.Range(Global.MinFontSize, 11).ToList(); + cmbCurrentLanguage.ItemsSource = Global.Languages; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs b/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs index 61a23c11..87a476f6 100644 --- a/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServer2Window.xaml.cs @@ -14,13 +14,7 @@ public partial class AddServer2Window this.Loaded += Window_Loaded; ViewModel = new AddServer2ViewModel(profileItem, UpdateViewHandler); - foreach (ECoreType it in Enum.GetValues(typeof(ECoreType))) - { - if (it == ECoreType.v2rayN) - continue; - cmbCoreType.Items.Add(it.ToString()); - } - cmbCoreType.Items.Add(string.Empty); + cmbCoreType.ItemsSource = Utils.GetEnumNames().Where(t => t != ECoreType.v2rayN.ToString()).ToList().AppendEmpty(); this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs index 2a25fa65..c2c13636 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs @@ -20,41 +20,22 @@ public partial class AddServerWindow ViewModel = new AddServerViewModel(profileItem, UpdateViewHandler); - Global.CoreTypes.ForEach(it => - { - cmbCoreType.Items.Add(it); - }); - cmbCoreType.Items.Add(string.Empty); + cmbCoreType.ItemsSource = Global.CoreTypes.AppendEmpty(); + cmbNetwork.ItemsSource = Global.Networks; + cmbFingerprint.ItemsSource = Global.Fingerprints; + cmbFingerprint2.ItemsSource = Global.Fingerprints; + cmbAllowInsecure.ItemsSource = Global.AllowInsecure; + cmbAlpn.ItemsSource = Global.Alpns; - cmbStreamSecurity.Items.Add(string.Empty); - cmbStreamSecurity.Items.Add(Global.StreamSecurity); - - Global.Networks.ForEach(it => - { - cmbNetwork.Items.Add(it); - }); - Global.Fingerprints.ForEach(it => - { - cmbFingerprint.Items.Add(it); - cmbFingerprint2.Items.Add(it); - }); - Global.AllowInsecure.ForEach(it => - { - cmbAllowInsecure.Items.Add(it); - }); - Global.Alpns.ForEach(it => - { - cmbAlpn.Items.Add(it); - }); + var lstStreamSecurity = new List(); + lstStreamSecurity.Add(string.Empty); + lstStreamSecurity.Add(Global.StreamSecurity); switch (profileItem.ConfigType) { case EConfigType.VMess: gridVMess.Visibility = Visibility.Visible; - Global.VmessSecurities.ForEach(it => - { - cmbSecurity.Items.Add(it); - }); + cmbSecurity.ItemsSource = Global.VmessSecurities; if (profileItem.Security.IsNullOrEmpty()) { profileItem.Security = Global.DefaultSecurity; @@ -63,10 +44,7 @@ public partial class AddServerWindow case EConfigType.Shadowsocks: gridSs.Visibility = Visibility.Visible; - AppHandler.Instance.GetShadowsocksSecurities(profileItem).ForEach(it => - { - cmbSecurity3.Items.Add(it); - }); + cmbSecurity3.ItemsSource = AppHandler.Instance.GetShadowsocksSecurities(profileItem); break; case EConfigType.SOCKS: @@ -76,11 +54,8 @@ public partial class AddServerWindow case EConfigType.VLESS: gridVLESS.Visibility = Visibility.Visible; - cmbStreamSecurity.Items.Add(Global.StreamSecurityReality); - Global.Flows.ForEach(it => - { - cmbFlow5.Items.Add(it); - }); + lstStreamSecurity.Add(Global.StreamSecurityReality); + cmbFlow5.ItemsSource = Global.Flows; if (profileItem.Security.IsNullOrEmpty()) { profileItem.Security = Global.None; @@ -89,11 +64,8 @@ public partial class AddServerWindow case EConfigType.Trojan: gridTrojan.Visibility = Visibility.Visible; - cmbStreamSecurity.Items.Add(Global.StreamSecurityReality); - Global.Flows.ForEach(it => - { - cmbFlow6.Items.Add(it); - }); + lstStreamSecurity.Add(Global.StreamSecurityReality); + cmbFlow6.ItemsSource = Global.Flows; break; case EConfigType.Hysteria2: @@ -113,10 +85,7 @@ public partial class AddServerWindow cmbFingerprint.IsEnabled = false; cmbFingerprint.Text = string.Empty; - Global.TuicCongestionControls.ForEach(it => - { - cmbHeaderType8.Items.Add(it); - }); + cmbHeaderType8.ItemsSource = Global.TuicCongestionControls; break; case EConfigType.WireGuard: @@ -128,6 +97,7 @@ public partial class AddServerWindow break; } + cmbStreamSecurity.ItemsSource = lstStreamSecurity; gridTlsMore.Visibility = Visibility.Hidden; @@ -267,44 +237,41 @@ public partial class AddServerWindow private void SetHeaderType() { - cmbHeaderType.Items.Clear(); + var lstHeaderType = new List(); var network = cmbNetwork.SelectedItem.ToString(); if (network.IsNullOrEmpty()) { - cmbHeaderType.Items.Add(Global.None); + lstHeaderType.Add(Global.None); + cmbHeaderType.ItemsSource = lstHeaderType; + cmbHeaderType.SelectedIndex = 0; return; } if (network == nameof(ETransport.tcp)) { - cmbHeaderType.Items.Add(Global.None); - cmbHeaderType.Items.Add(Global.TcpHeaderHttp); + lstHeaderType.Add(Global.None); + lstHeaderType.Add(Global.TcpHeaderHttp); } else if (network is nameof(ETransport.kcp) or nameof(ETransport.quic)) { - cmbHeaderType.Items.Add(Global.None); - Global.KcpHeaderTypes.ForEach(it => - { - cmbHeaderType.Items.Add(it); - }); + lstHeaderType.Add(Global.None); + lstHeaderType.AddRange(Global.KcpHeaderTypes); } else if (network is nameof(ETransport.xhttp)) { - Global.XhttpMode.ForEach(it => - { - cmbHeaderType.Items.Add(it); - }); + lstHeaderType.AddRange(Global.XhttpMode); } else if (network == nameof(ETransport.grpc)) { - cmbHeaderType.Items.Add(Global.GrpcGunMode); - cmbHeaderType.Items.Add(Global.GrpcMultiMode); + lstHeaderType.Add(Global.GrpcGunMode); + lstHeaderType.Add(Global.GrpcMultiMode); } else { - cmbHeaderType.Items.Add(Global.None); + lstHeaderType.Add(Global.None); } + cmbHeaderType.ItemsSource = lstHeaderType; cmbHeaderType.SelectedIndex = 0; } diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs index 0ef8bbf5..1f6b960b 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs @@ -17,22 +17,10 @@ public partial class DNSSettingWindow ViewModel = new DNSSettingViewModel(UpdateViewHandler); - Global.DomainStrategy4Freedoms.ForEach(it => - { - cmbdomainStrategy4Freedom.Items.Add(it); - }); - Global.SingboxDomainStrategy4Out.ForEach(it => - { - cmbdomainStrategy4Out.Items.Add(it); - }); - Global.DomainDNSAddress.ForEach(it => - { - cmbdomainDNSAddress.Items.Add(it); - }); - Global.SingboxDomainDNSAddress.ForEach(it => - { - cmbdomainDNSAddress2.Items.Add(it); - }); + cmbdomainStrategy4Freedom.ItemsSource = Global.DomainStrategy4Freedoms; + cmbdomainStrategy4Out.ItemsSource = Global.SingboxDomainStrategy4Out; + cmbdomainDNSAddress.ItemsSource = Global.DomainDNSAddress; + cmbdomainDNSAddress2.ItemsSource = Global.SingboxDomainDNSAddress; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/MsgView.xaml.cs b/v2rayN/v2rayN/Views/MsgView.xaml.cs index 1a04a9ed..96e67c16 100644 --- a/v2rayN/v2rayN/Views/MsgView.xaml.cs +++ b/v2rayN/v2rayN/Views/MsgView.xaml.cs @@ -25,11 +25,8 @@ public partial class MsgView menuMsgViewCopy.Click += menuMsgViewCopy_Click; menuMsgViewCopyAll.Click += menuMsgViewCopyAll_Click; menuMsgViewClear.Click += menuMsgViewClear_Click; - - Global.PresetMsgFilters.ForEach(it => - { - cmbMsgFilter.Items.Add(it); - }); + + cmbMsgFilter.ItemsSource = Global.PresetMsgFilters; } private async Task UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index ecdc39f0..c790e25d 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -21,94 +21,39 @@ public partial class OptionSettingWindow ViewModel = new OptionSettingViewModel(UpdateViewHandler); clbdestOverride.SelectionChanged += ClbdestOverride_SelectionChanged; - Global.destOverrideProtocols.ForEach(it => - { - clbdestOverride.Items.Add(it); - }); + clbdestOverride.ItemsSource = Global.destOverrideProtocols; _config.Inbound.First().DestOverride?.ForEach(it => { clbdestOverride.SelectedItems.Add(it); }); - Global.IEProxyProtocols.ForEach(it => - { - cmbsystemProxyAdvancedProtocol.Items.Add(it); - }); - Global.LogLevels.ForEach(it => - { - cmbloglevel.Items.Add(it); - }); - Global.Fingerprints.ForEach(it => - { - cmbdefFingerprint.Items.Add(it); - }); - Global.UserAgent.ForEach(it => - { - cmbdefUserAgent.Items.Add(it); - }); - Global.SingboxMuxs.ForEach(it => - { - cmbmux4SboxProtocol.Items.Add(it); - }); - Global.TunMtus.ForEach(it => - { - cmbMtu.Items.Add(it); - }); - Global.TunStacks.ForEach(it => - { - cmbStack.Items.Add(it); - }); - Global.CoreTypes.ForEach(it => - { - cmbCoreType1.Items.Add(it); - cmbCoreType2.Items.Add(it); - cmbCoreType3.Items.Add(it); - cmbCoreType4.Items.Add(it); - cmbCoreType5.Items.Add(it); - cmbCoreType6.Items.Add(it); - cmbCoreType9.Items.Add(it); - }); + cmbsystemProxyAdvancedProtocol.ItemsSource = Global.IEProxyProtocols; + cmbloglevel.ItemsSource = Global.LogLevels; + cmbdefFingerprint.ItemsSource = Global.Fingerprints; + cmbdefUserAgent.ItemsSource = Global.UserAgent; + cmbmux4SboxProtocol.ItemsSource = Global.SingboxMuxs; + cmbMtu.ItemsSource = Global.TunMtus; + cmbStack.ItemsSource = Global.TunStacks; - for (var i = 2; i <= 8; i++) - { - cmbMixedConcurrencyCount.Items.Add(i); - } - for (var i = 2; i <= 6; i++) - { - cmbSpeedTestTimeout.Items.Add(i * 5); - } - Global.SpeedTestUrls.ForEach(it => - { - cmbSpeedTestUrl.Items.Add(it); - }); - Global.SpeedPingTestUrls.ForEach(it => - { - cmbSpeedPingTestUrl.Items.Add(it); - }); - Global.SubConvertUrls.ForEach(it => - { - cmbSubConvertUrl.Items.Add(it); - }); - Global.GeoFilesSources.ForEach(it => - { - cmbGetFilesSourceUrl.Items.Add(it); - }); - Global.SingboxRulesetSources.ForEach(it => - { - cmbSrsFilesSourceUrl.Items.Add(it); - }); - Global.RoutingRulesSources.ForEach(it => - { - cmbRoutingRulesSourceUrl.Items.Add(it); - }); - Global.IPAPIUrls.ForEach(it => - { - cmbIPAPIUrl.Items.Add(it); - }); - foreach (EGirdOrientation it in Enum.GetValues(typeof(EGirdOrientation))) - { - cmbMainGirdOrientation.Items.Add(it.ToString()); - } + cmbCoreType1.ItemsSource = Global.CoreTypes; + cmbCoreType2.ItemsSource = Global.CoreTypes; + cmbCoreType3.ItemsSource = Global.CoreTypes; + cmbCoreType4.ItemsSource = Global.CoreTypes; + cmbCoreType5.ItemsSource = Global.CoreTypes; + cmbCoreType6.ItemsSource = Global.CoreTypes; + cmbCoreType9.ItemsSource = Global.CoreTypes; + + cmbMixedConcurrencyCount.ItemsSource = Enumerable.Range(2, 7).ToList(); + cmbSpeedTestTimeout.ItemsSource = Enumerable.Range(2, 5).Select(i => i * 5).ToList(); + cmbSpeedTestUrl.ItemsSource = Global.SpeedTestUrls; + cmbSpeedPingTestUrl.ItemsSource = Global.SpeedPingTestUrls; + cmbSubConvertUrl.ItemsSource = Global.SubConvertUrls; + cmbGetFilesSourceUrl.ItemsSource = Global.GeoFilesSources; + cmbSrsFilesSourceUrl.ItemsSource = Global.SingboxRulesetSources; + cmbRoutingRulesSourceUrl.ItemsSource = Global.RoutingRulesSources; + cmbIPAPIUrl.ItemsSource = Global.IPAPIUrls; + + cmbMainGirdOrientation.ItemsSource = Utils.GetEnumNames(); this.WhenActivated(disposables => { @@ -209,8 +154,7 @@ public partial class OptionSettingWindow private async Task InitSettingFont() { var lstFonts = await GetFonts(Utils.GetFontsPath()); - lstFonts.ForEach(it => { cmbcurrentFontFamily.Items.Add(it); }); - cmbcurrentFontFamily.Items.Add(string.Empty); + cmbcurrentFontFamily.ItemsSource = lstFonts.AppendEmpty(); } private async Task> GetFonts(string path) diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs index b04f9df2..9db3fb83 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs @@ -16,23 +16,11 @@ public partial class RoutingRuleDetailsWindow clbInboundTag.SelectionChanged += ClbInboundTag_SelectionChanged; ViewModel = new RoutingRuleDetailsViewModel(rulesItem, UpdateViewHandler); - - Global.OutboundTags.ForEach(it => - { - cmbOutboundTag.Items.Add(it); - }); - Global.RuleProtocols.ForEach(it => - { - clbProtocol.Items.Add(it); - }); - Global.InboundTags.ForEach(it => - { - clbInboundTag.Items.Add(it); - }); - Global.RuleNetworks.ForEach(it => - { - cmbNetwork.Items.Add(it); - }); + + cmbOutboundTag.ItemsSource = Global.OutboundTags; + clbProtocol.ItemsSource = Global.RuleProtocols; + clbInboundTag.ItemsSource = Global.InboundTags; + cmbNetwork.ItemsSource = Global.RuleNetworks; if (!rulesItem.Id.IsNullOrEmpty()) { diff --git a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs index 389c8183..bec5d1f7 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs @@ -21,15 +21,9 @@ public partial class RoutingRuleSettingWindow btnBrowseCustomRulesetPath4Singbox.Click += btnBrowseCustomRulesetPath4Singbox_Click; ViewModel = new RoutingRuleSettingViewModel(routingItem, UpdateViewHandler); - Global.DomainStrategies.ForEach(it => - { - cmbdomainStrategy.Items.Add(it); - }); - cmbdomainStrategy.Items.Add(string.Empty); - Global.DomainStrategies4Singbox.ForEach(it => - { - cmbdomainStrategy4Singbox.Items.Add(it); - }); + + cmbdomainStrategy.ItemsSource = Global.DomainStrategies.AppendEmpty(); + cmbdomainStrategy4Singbox.ItemsSource = Global.DomainStrategies4Singbox; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs index fcb1d630..5781dcba 100644 --- a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs @@ -21,18 +21,9 @@ public partial class RoutingSettingWindow ViewModel = new RoutingSettingViewModel(UpdateViewHandler); - Global.DomainStrategies.ForEach(it => - { - cmbdomainStrategy.Items.Add(it); - }); - Global.DomainMatchers.ForEach(it => - { - cmbdomainMatcher.Items.Add(it); - }); - Global.DomainStrategies4Singbox.ForEach(it => - { - cmbdomainStrategy4Singbox.Items.Add(it); - }); + cmbdomainStrategy.ItemsSource = Global.DomainStrategies; + cmbdomainMatcher.ItemsSource = Global.DomainMatchers; + cmbdomainStrategy4Singbox.ItemsSource = Global.DomainStrategies4Singbox; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs b/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs index 20a1263d..c9ab564b 100644 --- a/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs @@ -15,10 +15,7 @@ public partial class SubEditWindow ViewModel = new SubEditViewModel(subItem, UpdateViewHandler); - Global.SubConvertTargets.ForEach(it => - { - cmbConvertTarget.Items.Add(it); - }); + cmbConvertTarget.ItemsSource = Global.SubConvertTargets; this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/ThemeSettingView.xaml.cs b/v2rayN/v2rayN/Views/ThemeSettingView.xaml.cs index 9483ef5e..93d634de 100644 --- a/v2rayN/v2rayN/Views/ThemeSettingView.xaml.cs +++ b/v2rayN/v2rayN/Views/ThemeSettingView.xaml.cs @@ -13,22 +13,10 @@ public partial class ThemeSettingView { InitializeComponent(); ViewModel = new ThemeSettingViewModel(); - foreach (ETheme it in Enum.GetValues(typeof(ETheme))) - { - if ((int)it > 2) - continue; - cmbCurrentTheme.Items.Add(it.ToString()); - } - for (int i = Global.MinFontSize; i <= Global.MinFontSize + 10; i++) - { - cmbCurrentFontSize.Items.Add(i.ToString()); - } - - Global.Languages.ForEach(it => - { - cmbCurrentLanguage.Items.Add(it); - }); + cmbCurrentTheme.ItemsSource = Utils.GetEnumNames().Take(3).ToList(); + cmbCurrentFontSize.ItemsSource = Enumerable.Range(Global.MinFontSize, 11).ToList(); + cmbCurrentLanguage.ItemsSource = Global.Languages; this.WhenActivated(disposables => {