feat: 算法配置页面,改为json编辑

master
Tongg 8 months ago
parent cba4f6fce0
commit 52f8513ce9
  1. 2
      Language/zh_CN.xaml
  2. 4
      MainWindow.xaml
  3. 72
      Resource/Other/Json-Mode-Default.xshd
  4. 3
      SparkClient.csproj
  5. 56
      ViewModel/Configuration/AlgorithmConfigVM.cs
  6. 39
      Views/Configuration/AlgorithmConfigPage.xaml
  7. 44
      Views/Configuration/AlgorithmConfigPage.xaml.cs
  8. 2
      Views/Configuration/ConfigMenuPage.xaml.cs

@ -23,6 +23,8 @@
<!-- <sys:String x:Key="LevelConfig">定级配置</sys:String> -->
<sys:String x:Key="DeleteConfig">删除配置</sys:String>
<sys:String x:Key="ImportConfig">导入配置</sys:String>
<sys:String x:Key="BeautifyJson">美化Json</sys:String>
<sys:String x:Key="UglifyJson">压缩Json</sys:String>
<sys:String x:Key="ExitAsk">是否退出程序?</sys:String>

@ -100,12 +100,12 @@
<!-- 中文 -->
<Button Grid.Column="0" Width="50" Background="Transparent" BorderBrush="Transparent" Command="{Binding SetZhcnLanguage}" >
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
Text="中" Foreground="Azure" FontSize="24" />
Text="中" Foreground="Azure" FontSize="18" />
</Button>
<!-- 英文 -->
<Button Grid.Column="1" Width="50" Background="Transparent" BorderBrush="Transparent" Command="{Binding SetEnLanguage}" >
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
Text="EN" Foreground="Azure" FontSize="24"/>
Text="EN" Foreground="Azure" FontSize="18"/>
</Button>

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- syntaxdefinition for Json by alek kowalczyk -->
<!-- update by zuijin in 2019.12.20 -->
<SyntaxDefinition name="Json" extensions=".json" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Bool" foreground="Blue" exampleText="true | false" />
<Color name="Number" foreground="Red" exampleText="3.14" />
<Color name="String" foreground="Green" exampleText="" />
<Color name="Null" foreground="Olive" exampleText="" />
<Color name="FieldName" foreground="DarkMagenta" />
<Color name="Punctuation" foreground="Black" />
<RuleSet name="String">
<Span begin="\\" end="."/>
</RuleSet>
<RuleSet name="Object">
<Span color="FieldName" ruleSet="String">
<Begin>"</Begin>
<End>"</End>
</Span>
<Span color="FieldName" ruleSet="String">
<Begin>'</Begin>
<End>'</End>
</Span>
<Span color="Punctuation" ruleSet="Expression">
<Begin>:</Begin>
</Span>
<Span color="Punctuation">
<Begin>,</Begin>
</Span>
</RuleSet>
<RuleSet name="Array">
<Import ruleSet="Expression"/>
<Span color="Punctuation">
<Begin>,</Begin>
</Span>
</RuleSet>
<RuleSet name="Expression">
<Keywords color="Bool" >
<Word>true</Word>
<Word>false</Word>
</Keywords>
<Keywords color="Null" >
<Word>null</Word>
</Keywords>
<Span color="String" ruleSet="String">
<Begin>"</Begin>
<End>"</End>
</Span>
<Span color="String" ruleSet="String">
<Begin>'</Begin>
<End>'</End>
</Span>
<Span color="Punctuation" ruleSet="Object" multiline="true">
<Begin>\{</Begin>
<End>\}</End>
</Span>
<Span color="Punctuation" ruleSet="Array" multiline="true">
<Begin>\[</Begin>
<End>\]</End>
</Span>
<Rule color="Number">
\b0[xX][0-9a-fA-F]+|(\b\d+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?
</Rule>
</RuleSet>
<RuleSet>
<Import ruleSet="Expression"/>
</RuleSet>
</SyntaxDefinition>

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AvalonEdit" Version="6.3.0.90" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="HandyControl" Version="3.5.1" />
<PackageReference Include="HandyControl.Lang.en" Version="3.5.1" />
@ -54,6 +55,8 @@
<Content Include="Resource\Document\Helper.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="Resource\Other\Json-Mode-Default.xshd" />
<EmbeddedResource Include="Resource\Other\Json-Mode-Default.xshd" />
</ItemGroup>
<ItemGroup>

@ -1,13 +1,25 @@
namespace SparkClient.ViewModel.Configuration;
using System.Windows.Input;
using HandyControl.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace SparkClient.ViewModel.Configuration;
public class AlgorithmConfigVM : BaseViewModel
{
public ICommand SaveAlgorithmDataCommand { get; }
public ICommand BeautifyJsonCommand { get; }
public ICommand UglifyJsonCommand { get; }
private string _AlgorithmConfigJson;
public string AlgorithmConfigJson { get { return _AlgorithmConfigJson; } set { _AlgorithmConfigJson = value; OnPropertyChanged("AlgorithmConfigJson"); } }
public AlgorithmConfigVM()
{
// 初始化VIEW层参数
// 初始化VIEW层Command
this.InitAlgorithmData(null);
SaveAlgorithmDataCommand = new RelayCommand(SaveAlgorithmData);
BeautifyJsonCommand = new RelayCommand(BeautifyJson);
UglifyJsonCommand = new RelayCommand(UglifyJson);
InitAlgorithmData(null);
}
/// <summary>
@ -16,33 +28,45 @@ public class AlgorithmConfigVM : BaseViewModel
/// <param name="param"></param>
public void InitAlgorithmData(object param)
{
AlgorithmConfigJson = "{}";
}
/// <summary>
/// 添加行
/// 保存数据
/// </summary>
/// <param name="row">行数</param>
public void AddRows(object row)
/// <param name="param"></param>
public void SaveAlgorithmData(object param)
{
Growl.Info("Saving Algorithm Data");
}
/// <summary>
/// 保存数据
/// 梅花JSON
/// </summary>
/// <param name="param"></param>
public void SaveAlgorithmData(object param)
public void BeautifyJson(object param)
{
try
{
AlgorithmConfigJson = JToken.Parse(AlgorithmConfigJson).ToString();
}
catch (Exception ex)
{
Growl.Error(ex.Message);
}
}
/// <summary>
/// 删除一行数据
/// 压缩JSON
/// </summary>
/// <param name="row">行</param>
public void DelAlgorRow(object row)
/// <param name="param"></param>
public void UglifyJson(object param)
{
AlgorithmConfigJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(AlgorithmConfigJson));
}
}

@ -3,10 +3,45 @@
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:hc="https://handyorg.github.io/handycontrol"
xmlns:local="clr-namespace:SparkClient.Views"
xmlns:configuration="clr-namespace:SparkClient.Views.Configuration"
mc:Ignorable="d"
>
<Grid Background="Blue">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="80">算法配置</TextBlock>
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Content="{ DynamicResource Save}" Width="80" Margin="10 5 " Command="{Binding SaveAlgorithmDataCommand}"/>
<Button Grid.Column="1" Grid.Row="0" Content="{DynamicResource BeautifyJson}" Width="80" Margin="10 5 " Command="{Binding BeautifyJsonCommand}"/>
<Button Grid.Column="2" Grid.Row="0" Content="{DynamicResource UglifyJson}" Width="80" Margin="10 5 " Command="{Binding UglifyJsonCommand}"/>
<!-- <TextBox Grid.Column="3" Grid.Row="0" Text="{Binding AlgorithmConfigJson}"></TextBox> -->
<avalonEdit:TextEditor Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1"
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
Name="TextEditor"
SyntaxHighlighting="JSON"
FontFamily="Consolas"
FontSize="16pt"
LineNumbersForeground="Black"
TextChanged="TextEditor_OnTextChanged"
ShowLineNumbers="True" >
<avalonEdit:TextEditor.Options>
<avalonEdit:TextEditorOptions ShowSpaces="True" />
</avalonEdit:TextEditor.Options>
</avalonEdit:TextEditor>
</Grid>
</Border>

@ -1,11 +1,51 @@
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using SparkClient.ViewModel.Configuration;
namespace SparkClient.Views.Configuration;
public partial class AlgorithmConfigPage
{
public AlgorithmConfigPage()
{
InitializeComponent();
DataContext = new AlgorithmConfigVM();
using (Stream stream = typeof(MainWindow).Assembly.GetManifestResourceStream(@"SparkClient.Resource.Other.Json-Mode-Default.xshd"))
using (XmlReader reader = new XmlTextReader(stream))
{
IHighlightingDefinition highlightingDefinition = HighlightingLoader.Load(reader, HighlightingManager.Instance);
HighlightingManager.Instance.RegisterHighlighting("JSON Highlighting", new string[] { ".json" }, highlightingDefinition);
TextEditor.SyntaxHighlighting = highlightingDefinition;
}
if (DataContext is AlgorithmConfigVM vm)
{
TextEditor.Text = vm.AlgorithmConfigJson;
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(vm.AlgorithmConfigJson))
{
if (TextEditor.Text != vm.AlgorithmConfigJson)
{
TextEditor.Text = vm.AlgorithmConfigJson;
}
}
};
}
}
private void TextEditor_OnTextChanged(object? sender, EventArgs e)
{
if(DataContext is AlgorithmConfigVM vm)
vm.AlgorithmConfigJson = TextEditor.Text;
}
}
}

@ -1,4 +1,4 @@
using System.Drawing;

using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

Loading…
Cancel
Save