private class Model : BaseViewModel
{
private const double KlConst = 273.15;
private const double InitialKelvin = KlConst + 20;
public Model()
{
Title = Resource.ConverterTemperatureTitle;
ActiveButton = nameof(KValue);
KValue = InitialKelvin;
}
#region Props
private double K2C() => KValue - KlConst;
private double C2K() => CValue + KlConst;
private double K2F() => (KValue - KlConst) * (9D / 5) + 32;
private double F2K() => (FValue - 32) * 5d / 9 + KlConst;
private double K2Re() => (4d / 5) * (KValue - KlConst);
private double Re2K() => (5d / 4) * ReValue + KlConst;
private void Calculate()
{
if (ActiveButton != nameof(CValue)) CValue = K2C();
if (ActiveButton != nameof(FValue)) FValue = K2F();
if (ActiveButton != nameof(ReValue)) ReValue = K2Re();
}
#region CValue
private double _cValue;
public double CValue
{
get => _cValue;
set
{
SetProperty(ref _cValue, value);
if (ActiveButton != nameof(CValue)) return;
KValue = C2K();
Calculate();
}
}
#endregion
#region FValue
private double _fValue;
public double FValue
{
get => _fValue;
set
{
SetProperty(ref _fValue, value);
if (ActiveButton != nameof(FValue)) return;
KValue = F2K();
Calculate();
}
}
#endregion
#region ReValue
private double _reValue;
public double ReValue
{
get => _reValue;
set
{
SetProperty(ref _reValue, value);
if (ActiveButton != nameof(ReValue)) return;
KValue = Re2K();
Calculate();
}
}
#endregion
#region KValue
private double _kValue;
public double KValue
{
get => _kValue;
set
{
SetProperty(ref _kValue, value);
if (ActiveButton != nameof(KValue)) return;
Calculate();
}
}
#endregion
#endregion
#region ActiveButton
private string _activeButton = "";
public string ActiveButton
{
get => _activeButton;
set => SetProperty(ref _activeButton, value);
}
#endregion
}
public class DoubleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return "";
var doubleValue = (double)value;
return $"{doubleValue}";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var strValue = value as string;
if (string.IsNullOrEmpty(strValue))
return 0;
if (double.TryParse(strValue, out var resultDecimal))
{
return resultDecimal;
}
return 0;
}
}
public int Num
{
get => _num; // а это новая форма записи, короче и красивей той что у вас
set =>_num = value; // Присвоение значения собственно почему у вас и ругается, нет этого метода
}
public int NumSecond
{
get => _sec; // а это новая форма записи, короче и красивей той что у вас
private set =>_sec = value; // А тут закрываете доступ извне
}
void Test()
{
var peoples = new People[]
{
new People{LastName = "Ivanov", Name = "Ivan", School = 17, One = 5, Two = 4},
new People{LastName = "Petrov", Name = "Petr", School = 17, One = 5, Two = 3},
new People{LastName = "Dariyana", Name = "Daria", School = 17, One = 5, Two = 4},
new People{LastName = "Ivanov", Name = "Ivan", School = 16, One = 5, Two = 4},
new People{LastName = "Petrov", Name = "Petr", School = 16, One = 5, Two = 3},
new People{LastName = "Dariyana", Name = "Daria", School = 16, One = 5, Two = 4},
new People{LastName = "Dariyana", Name = "Daria", School = 19, One = 2, Two = 2},
};
var path = "file.xml";
Save(path,peoples);
peoples = Load(path);
var schoolList = peoples.Select(s => s.School).Distinct();
var best =
(from sn in schoolList let max = peoples.Where(s => s.School == sn)
.Max(x => x.One + x.Two)
select
peoples.First(x => x.School == sn && (x.One + x.Two) == max))
.ToList();
;
}
private static People[] Load(string path)
{
var formatter = new XmlSerializer(typeof(People[]));
using var fs = new FileStream(path, FileMode.OpenOrCreate);
var peoples = (People[])formatter.Deserialize(fs);
return peoples;
}
private static void Save(string path, People[] peoples)
{
var formatter = new XmlSerializer(typeof(People[]));
using var fs = new FileStream(path, FileMode.OpenOrCreate);
formatter.Serialize(fs, peoples);
}
[Serializable]
public class People
{
public string LastName { get; set; }
public string Name { get; set; }
public int School { get; set; }
public int One { get; set; }
public int Two { get; set; }
}