202 lines
7.4 KiB
C#
202 lines
7.4 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using PortProxyTool.Wpf.Views.Windows;
|
|
|
|
namespace PortProxyTool.Wpf.Views;
|
|
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
private List<PortProxyItem> _portProxies = [];
|
|
public PortProxyItem? V4ToV4SelectedItem;
|
|
public PortProxyItem? V4ToV6SelectedItem;
|
|
public PortProxyItem? V6ToV4SelectedItem;
|
|
public PortProxyItem? V6ToV6SelectedItem;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
RefreshListBox();
|
|
}
|
|
|
|
private void RefreshButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is not Button) return;
|
|
RefreshListBox();
|
|
}
|
|
|
|
private void RefreshListBox()
|
|
{
|
|
_portProxies = PortProxy.GetPortProxies().ToList();
|
|
V4ToV4ListBox.ItemsSource = _portProxies.Where(x => x.Type == PortProxyType.V4ToV4);
|
|
V4ToV6ListBox.ItemsSource = _portProxies.Where(x => x.Type == PortProxyType.V4ToV6);
|
|
V6ToV6ListBox.ItemsSource = _portProxies.Where(x => x.Type == PortProxyType.V6ToV6);
|
|
V6ToV4ListBox.ItemsSource = _portProxies.Where(x => x.Type == PortProxyType.V6ToV4);
|
|
}
|
|
|
|
private void V4ToV4ListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (sender is ListBox listBox)
|
|
V4ToV4SelectedItem = listBox.SelectedItem as PortProxyItem;
|
|
}
|
|
|
|
private void V4ToV6ListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (sender is ListBox listBox)
|
|
V4ToV6SelectedItem = listBox.SelectedItem as PortProxyItem;
|
|
}
|
|
|
|
private void V6ToV6ListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (sender is ListBox listBox)
|
|
V6ToV6SelectedItem = listBox.SelectedItem as PortProxyItem;
|
|
}
|
|
|
|
private void V6ToV4ListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (sender is ListBox listBox)
|
|
V6ToV4SelectedItem = listBox.SelectedItem as PortProxyItem;
|
|
}
|
|
|
|
private void AddButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is not Button) return;
|
|
AddWindow addWindow = new();
|
|
addWindow.ShowDialog();
|
|
if (addWindow.DialogResult == true)
|
|
RefreshListBox();
|
|
}
|
|
|
|
private void RemoveButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is not Button button) return;
|
|
switch (button.Name)
|
|
{
|
|
case "V4ToV4RemoveButton":
|
|
if (V4ToV4SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要删除的条目", "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
var result = PortProxy.Remove(V4ToV4SelectedItem);
|
|
if (string.IsNullOrEmpty(result))
|
|
MessageBox.Show("删除成功", "删除成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
else
|
|
MessageBox.Show(result, "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
break;
|
|
case "V4ToV6RemoveButton":
|
|
if (V4ToV6SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要删除的条目", "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
var result = PortProxy.Remove(V4ToV6SelectedItem);
|
|
if (string.IsNullOrEmpty(result))
|
|
MessageBox.Show("删除成功", "删除成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
else
|
|
MessageBox.Show(result, "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
break;
|
|
case "V6ToV6RemoveButton":
|
|
if (V6ToV6SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要删除的条目", "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
var result = PortProxy.Remove(V6ToV6SelectedItem);
|
|
if (string.IsNullOrEmpty(result))
|
|
MessageBox.Show("删除成功", "删除成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
else
|
|
MessageBox.Show(result, "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
break;
|
|
case "V6ToV4RemoveButton":
|
|
if (V6ToV4SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要删除的条目", "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
var result = PortProxy.Remove(V6ToV4SelectedItem);
|
|
if (string.IsNullOrEmpty(result))
|
|
MessageBox.Show("删除成功", "删除成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
else
|
|
MessageBox.Show(result, "删除失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
RefreshListBox();
|
|
}
|
|
|
|
private void SetButton_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is not Button button) return;
|
|
SetWindow setWindow;
|
|
switch (button.Name)
|
|
{
|
|
case "V4ToV4SetButton":
|
|
if (V4ToV4SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要编辑的条目", "编辑失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
setWindow = new SetWindow(V4ToV4SelectedItem);
|
|
setWindow.ShowDialog();
|
|
RefreshListBox();
|
|
}
|
|
|
|
break;
|
|
case "V4ToV6SetButton":
|
|
if (V4ToV6SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要编辑的条目", "编辑失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
setWindow = new SetWindow(V4ToV6SelectedItem);
|
|
setWindow.ShowDialog();
|
|
RefreshListBox();
|
|
}
|
|
|
|
break;
|
|
case "V6ToV6SetButton":
|
|
if (V6ToV6SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要编辑的条目", "编辑失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
setWindow = new SetWindow(V6ToV6SelectedItem);
|
|
setWindow.ShowDialog();
|
|
RefreshListBox();
|
|
}
|
|
|
|
break;
|
|
case "V6ToV4SetButton":
|
|
if (V6ToV4SelectedItem is null)
|
|
{
|
|
MessageBox.Show("请选择要编辑的条目", "编辑失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
else
|
|
{
|
|
setWindow = new SetWindow(V6ToV4SelectedItem);
|
|
setWindow.ShowDialog();
|
|
RefreshListBox();
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
} |