using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PipeGallery.Manage; using PipeGallery.Model; using PipeGallery.ViewModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PipeGallery.View { /// <summary> /// EditNameView.xaml 的交互逻辑 /// </summary> public partial class EditNameView : UserControl { private int pipeMaterialId; public EditNameView(int id, string name) { InitializeComponent(); this.pipeMaterialId = id; this.txtName.Text = name; this.btnOK.Click += btnOK_Click; this.btnClose.Click += btnClose_Click; this.btnClear.Click += btnClear_Click; } void btnOK_Click(object sender, RoutedEventArgs e) { if (txtName.Text == "") { new PopupWindow("名称不允许为空!").ShowDialog(); return; } foreach(var v in PipeGallery.Correlator.PipeMaterial.MaterialMap.Keys) { if (txtName.Text == v) { new PopupWindow("该名称已存在,请重新输入!").ShowDialog(); return; } } //执行确定事件 StreamReader sr = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "config.json"); JObject jo = (JObject)JsonConvert.DeserializeObject(sr.ReadToEnd()); sr.Close(); jo["CustomMaterial"][pipeMaterialId]["name"] = this.txtName.Text; StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "config.json"); sw.Write(JsonConvert.SerializeObject(jo)); sw.Flush(); sw.Close(); //返回选择材质界面 SelectPipeMaterialView selectPipeMaterialView = new SelectPipeMaterialView(); Global.GetMainView().bdrPreposition.Child = selectPipeMaterialView; } void btnClose_Click(object sender, RoutedEventArgs e) { SelectPipeMaterialView selectPipeMaterialView = new SelectPipeMaterialView(); Global.GetMainView().bdrPreposition.Child = selectPipeMaterialView; } void btnClear_Click(object sender, RoutedEventArgs e) { this.txtName.Text = ""; } } }