Files
CapMachine/CapMachine.Wpf/ViewModels/DialogTherdyConfigViewModel.cs

115 lines
3.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CapMachine.Core;
using CapMachine.Wpf.Models.PPCalc;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Globalization;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
public class DialogTherdyConfigViewModel : DialogViewModel
{
private const string H3TempOffsetConfigKey = "Therdy_H3TempOffset_C";
public DialogTherdyConfigViewModel()
{
TherdyConfig = new TherdyConfigModel();
LoadConfig();
}
private string _h3TempOffsetText = string.Empty;
public string H3TempOffsetText
{
get { return _h3TempOffsetText; }
set { _h3TempOffsetText = value; RaisePropertyChanged(); }
}
public TherdyConfigModel TherdyConfig { get; set; }
private DelegateCommand saveCmd;
public DelegateCommand SaveCmd
{
get
{
if (saveCmd == null)
{
saveCmd = new DelegateCommand(SaveCmdMethod);
}
return saveCmd;
}
set { saveCmd = value; }
}
private DelegateCommand cancelCmd;
public DelegateCommand CancelCmd
{
get
{
if (cancelCmd == null)
{
cancelCmd = new DelegateCommand(CancelCmdMethod);
}
return cancelCmd;
}
set { cancelCmd = value; }
}
private void LoadConfig()
{
try
{
string raw = ConfigHelper.GetValue(H3TempOffsetConfigKey);
if (!string.IsNullOrWhiteSpace(raw) && double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var v))
{
TherdyConfig.H3TempOffset_C = v;
}
}
catch
{
}
H3TempOffsetText = TherdyConfig.H3TempOffset_C.ToString(CultureInfo.InvariantCulture);
}
private void SaveConfig()
{
ConfigHelper.SetValue(H3TempOffsetConfigKey, TherdyConfig.H3TempOffset_C.ToString(CultureInfo.InvariantCulture));
}
private void SaveCmdMethod()
{
try
{
if (!double.TryParse(H3TempOffsetText, NumberStyles.Float, CultureInfo.InvariantCulture, out var offsetC))
{
MessageBox.Show("回差值格式不正确,请输入数字(例如 -10。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
TherdyConfig.H3TempOffset_C = offsetC;
SaveConfig();
DialogParameters pars = new DialogParameters
{
{ "H3TempOffset_C", TherdyConfig.H3TempOffset_C }
};
RaiseRequestClose(new DialogResult(ButtonResult.OK, pars));
}
catch
{
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
}
}
private void CancelCmdMethod()
{
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
}
public override void OnDialogOpened(IDialogParameters parameters)
{
LoadConfig();
}
}
}