C# 如何从Xamarin中的选取器长度计算值
我的目的是在xamarin上创建长度转换器。我创建了picker函数,以便用户可以选择需要转换的长度 但对于下一步,我不知道如何创建一个方法,该方法可以处理和设置每个长度和倍数的值以及提示表单用户的值 有人能帮我用这个方法吗C# 如何从Xamarin中的选取器长度计算值,c#,xamarin,C#,Xamarin,我的目的是在xamarin上创建长度转换器。我创建了picker函数,以便用户可以选择需要转换的长度 但对于下一步,我不知道如何创建一个方法,该方法可以处理和设置每个长度和倍数的值以及提示表单用户的值 有人能帮我用这个方法吗 namespace LengthConversion { public class PickerLength { public int rate = 0; public List<Length> LengthL
namespace LengthConversion
{
public class PickerLength
{
public int rate = 0;
public List<Length> LengthList { get; set; }
public PickerLength()
{
LengthList = GetLength().OrderBy(t => t.Value).ToList();
}
public List<Length> GetLength()
{
var length = new List<Length>()
{
new Length() {Key = 1, Value="Millimetres(mm)"},
new Length() {Key = 2, Value="Centimetre(cm)"},
new Length() {Key = 3, Value="Meters(m)"},
new Length() {Key = 4, Value="Kilometers(km)"},
new Length() {Key = 5, Value="Feet(ft)"},
new Length() {Key = 6, Value="Inches(in)"},
new Length() {Key = 7, Value="Decimeter"},
new Length() {Key = 8, Value="Mile(m)"},
new Length() {Key = 9, Value="Yerd"},
new Length() {Key = 10, Value="Furlong"},
new Length() {Key = 11, Value="Hand(horses)"},
new Length() {Key = 12, Value="Fathom"}
};
return length;
}
}
public class Length
{
public int Key { get; set; }
public string Value { get; set; }
}
}
这可以通过选择参考测量单位来实现,如
米。然后存储所有其他度量单位的系数。然后将每个输入转换为参考度量单位,最后转换为目标度量单位
这不是实现这一点的唯一方法,也不是生产准备好的样品
例如:
private readonly UnitOfMeatures _referenceUnits = UnitOfMeasures.Meter;
// Multiply the input values by these to get to the reference units.
// Divide the reference values by these to get to the output units.
private const double COEFFICIENT_METER = 1.0f;
private const double COEFFICIENT_KILOMETER = 1000.0f;
private const double COEFFICIENT_FOOT = 0.3048f;
private const double COEFFICIENT_INCH = 0.0254f; // use Google to find these.
// Create map of UnitOfMeasures to coefficients
private Dictionary<UnitOfMeasures, double> _coefficientMap = new Dictionary<UnitOfMeasures, double>() {
{UnitOfMeasures.Meter, COEFFICIENT_METER}, {UnitOfMeasure.Kilometer, COEFFICIENT_KILOMETER} // etc...
};
public double ConvertUnits(UnitOfMeasures inputUnits, double value, UnitOfMeasures outputUnits) {
double result = 0.0f;
// convert to reference units
result = value * _coefficientMap[inputUnits];
// convert to output units
result = result / _coefficientMap[outputUnits];
return result;
}
private readonly UnitOfMeasures\u referenceUnits=UnitOfMeasures.Meter;
//将输入值乘以这些值,得到参考单位。
//将参考值除以这些值,得到输出单位。
私人常数双系数表=1.0f;
私人建筑双系数_千米=1000.0f;
私人建筑双系数_FOOT=0.3048f;
私人建筑双系数_英寸=0.0254f;//使用谷歌查找这些。
//创建测量单位到系数的映射
私有字典_coefficientMap=新字典(){
{计量单位.米,系数{米},{计量单位.公里,系数{公里}//等等。。。
};
公共双转换器单位(测量单位输入单位、双值、测量单位输出单位){
双结果=0.0f;
//转换为参考单位
结果=值*_系数映射[输入单位];
//转换为输出单位
结果=结果/_系数映射[输出单位];
返回结果;
}
namespace LengthConversion
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new PickerLength();
}
}
}
private readonly UnitOfMeatures _referenceUnits = UnitOfMeasures.Meter;
// Multiply the input values by these to get to the reference units.
// Divide the reference values by these to get to the output units.
private const double COEFFICIENT_METER = 1.0f;
private const double COEFFICIENT_KILOMETER = 1000.0f;
private const double COEFFICIENT_FOOT = 0.3048f;
private const double COEFFICIENT_INCH = 0.0254f; // use Google to find these.
// Create map of UnitOfMeasures to coefficients
private Dictionary<UnitOfMeasures, double> _coefficientMap = new Dictionary<UnitOfMeasures, double>() {
{UnitOfMeasures.Meter, COEFFICIENT_METER}, {UnitOfMeasure.Kilometer, COEFFICIENT_KILOMETER} // etc...
};
public double ConvertUnits(UnitOfMeasures inputUnits, double value, UnitOfMeasures outputUnits) {
double result = 0.0f;
// convert to reference units
result = value * _coefficientMap[inputUnits];
// convert to output units
result = result / _coefficientMap[outputUnits];
return result;
}