diff --git a/v2rayN/v2rayN/Handler/QRCodeHelper.cs b/v2rayN/v2rayN/Common/QRCodeHelper.cs similarity index 78% rename from v2rayN/v2rayN/Handler/QRCodeHelper.cs rename to v2rayN/v2rayN/Common/QRCodeHelper.cs index 07af8856..0c4fbdb2 100644 --- a/v2rayN/v2rayN/Handler/QRCodeHelper.cs +++ b/v2rayN/v2rayN/Common/QRCodeHelper.cs @@ -2,15 +2,19 @@ using QRCoder.Xaml; using System.Windows.Media; -namespace v2rayN.Handler +namespace v2rayN { /// /// 含有QR码的描述类和包装编码和渲染 /// public class QRCodeHelper { - public static DrawingImage? GetQRCode(string strContent) + public static DrawingImage? GetQRCode(string? strContent) { + if (strContent is null) + { + return null; + } try { QRCodeGenerator qrGenerator = new(); diff --git a/v2rayN/v2rayN/Common/UI.cs b/v2rayN/v2rayN/Common/UI.cs index dccbeee1..226757c4 100644 --- a/v2rayN/v2rayN/Common/UI.cs +++ b/v2rayN/v2rayN/Common/UI.cs @@ -1,4 +1,5 @@ -using System.Windows; +using Microsoft.Win32; +using System.Windows; namespace v2rayN { @@ -20,5 +21,24 @@ namespace v2rayN { return MessageBox.Show(msg, caption, MessageBoxButton.YesNo, MessageBoxImage.Question); } + + public static bool? OpenFileDialog(out string fileName, string filter) + { + fileName = string.Empty; + + var fileDialog = new OpenFileDialog + { + Multiselect = false, + Filter = filter + }; + + if (fileDialog.ShowDialog() != true) + { + return false; + } + fileName = fileDialog.FileName; + + return true; + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs b/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs index 34b3a93f..f4a5796c 100644 --- a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs @@ -1,4 +1,3 @@ -using Microsoft.Win32; using ReactiveUI; using ReactiveUI.Fody.Helpers; using ReactiveUI.Validation.Helpers; @@ -104,20 +103,16 @@ namespace v2rayN.ViewModels { UI.Show(ResUI.CustomServerTips); - OpenFileDialog fileDialog = new() - { - Multiselect = false, - Filter = "Config|*.json|YAML|*.yaml;*.yml|All|*.*" - }; - if (fileDialog.ShowDialog() != true) + if (UI.OpenFileDialog(out string fileName, + "Config|*.json|YAML|*.yaml;*.yml|All|*.*") != true) { return; } - string fileName = fileDialog.FileName; if (Utils.IsNullOrEmpty(fileName)) { return; } + var item = LazyConfig.Instance.GetProfileItem(SelectedSource.indexId); item ??= SelectedSource; item.address = fileName; diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index b4f3d234..52f581bc 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -3,7 +3,6 @@ using DynamicData.Binding; using MaterialDesignColors; using MaterialDesignColors.ColorManipulation; using MaterialDesignThemes.Wpf; -using Microsoft.Win32; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; @@ -1397,16 +1396,11 @@ namespace v2rayN.ViewModels private void ImportOldGuiConfig() { - OpenFileDialog fileDialog = new() - { - Multiselect = false, - Filter = "guiNConfig|*.json|All|*.*" - }; - if (fileDialog.ShowDialog() != true) + if (UI.OpenFileDialog(out string fileName, + "guiNConfig|*.json|All|*.*") != true) { return; } - string fileName = fileDialog.FileName; if (Utils.IsNullOrEmpty(fileName)) { return; diff --git a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs index 0fd7a0c5..52f08ba4 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs @@ -1,5 +1,4 @@ using DynamicData.Binding; -using Microsoft.Win32; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; @@ -264,20 +263,16 @@ namespace v2rayN.ViewModels private void ImportRulesFromFile() { - OpenFileDialog fileDialog = new OpenFileDialog - { - Multiselect = false, - Filter = "Rules|*.json|All|*.*" - }; - if (fileDialog.ShowDialog() != true) + if (UI.OpenFileDialog(out string fileName, + "Rules|*.json|All|*.*") != true) { return; } - string fileName = fileDialog.FileName; if (Utils.IsNullOrEmpty(fileName)) { return; } + string result = Utils.LoadResource(fileName); if (Utils.IsNullOrEmpty(result)) { diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index f431e4a0..c7f13a8c 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -1,5 +1,4 @@ -using Microsoft.Win32; -using ReactiveUI; +using ReactiveUI; using System.Globalization; using System.IO; using System.Reactive.Disposables; @@ -205,13 +204,5 @@ namespace v2rayN.Views this.Close(); } - private void btnBrowse_Click(object sender, System.Windows.RoutedEventArgs e) - { - var openFileDialog1 = new OpenFileDialog(); - openFileDialog1.Filter = "tunConfig|*.json|All|*.*"; - openFileDialog1.ShowDialog(); - - // txtCustomTemplate.Text = openFileDialog1.FileName; - } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs index b6ed8a8f..740231bd 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs @@ -1,5 +1,4 @@ -using Microsoft.Win32; -using ReactiveUI; +using ReactiveUI; using System.Reactive.Disposables; using System.Windows; using System.Windows.Input; @@ -131,11 +130,13 @@ namespace v2rayN.Views private void btnBrowse_Click(object sender, System.Windows.RoutedEventArgs e) { - var openFileDialog1 = new OpenFileDialog(); - openFileDialog1.Filter = "PNG,ICO|*.png;*.ico"; - openFileDialog1.ShowDialog(); + if (UI.OpenFileDialog(out string fileName, + "PNG,ICO|*.png;*.ico") != true) + { + return; + } - txtCustomIcon.Text = openFileDialog1.FileName; + txtCustomIcon.Text = fileName; } } } \ No newline at end of file