我想问一下:即时测量转换,这是详细信息:
要求:显示单位测量并考虑设置。关注点: - 只有基本(中性)单位测量将存储在数据库中,并且是一次性决定的。
- 网格控件直接绑定到我们的业务对象,因此转换值很复杂。
问题:
如何显示不同的单位测量(遵循设置),考虑到控件绑定到业务对象?
您的帮助将不胜感激。谢谢
伊卡德维
我想问一下:即时测量转换,这是详细信息:
要求:显示单位测量并考虑设置。关注点: - 只有基本(中性)单位测量将存储在数据库中,并且是一次性决定的。
问题:
如何显示不同的单位测量(遵循设置),考虑到控件绑定到业务对象?
您的帮助将不胜感激。谢谢
伊卡德维
如果我正确理解了您的问题(您有点模糊......)您希望以一种方式存储测量数据,但让用户可以选择以不同的方式显示它(使用不同的单位)。您没有指定您正在使用哪种技术/语言环境,但(至少)有一种非常简单的方法可以做到这一点:创建一个转换器类。
如果您的测量数据是长度(以毫米为单位),这里有一些伪 C# 骨架代码。您可能会弄清楚如何对您正在测量的任何内容使用相同的方法,但是您想显示它:
class LenghtConverter {
double ToCentimeters(double millimeters) {
// 1 centimeter = 10 millimeters
return millimeters / 10;
}
double ToInches(double millimeters) {
// 1 inch = 25.4 millimeters
return millimeters / 25.4
}
// You get the drift. Add whatever conversions you need. If you wish,
// you can return strings instead of numbers, and append the unit
// signature as well.
}
现在,在您的网格中,您可以使用某种表示语法来显示您的数据。我正在编造一些东西来给你一个想法,因为我进入了 ASP.NET,所以语法与此非常相似。希望你能原谅我=)
而不仅仅是
<%= MyMeasurement.Data %>
要以存储的方式显示测量数据,您可以使用
<%= LenghtConverter.ToInches(MyMeasurement.Data) %>
这将以英寸为单位显示结果。
如果您实际上使用的是 C#(或 VB.NET,我想),那么 .NET 3.5 中有一个很好的功能,称为扩展方法,您可能想要使用它。这会让你输出更酷、更流线型的语法
<%= MyMeasurement.Data.ToInches() %>
我有一个 QML Qt C++ UI。它与后端应用程序交互。
UI 支持英制和公制模式。用户可以在运行时从 UI 中进行此选择。
用户可以通过 UI 查看和编辑数据值。后端应用程序仅在英制模式下工作。
C++ 实用程序对象作为上下文属性公开给 QML。此实用程序对象具有以下方法:
C++ 数据对象具有以下两个属性:
Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QString unitString READ unitString NOTIFY unitStringChanged)
// value - In Imperial mode, you get Imperial value. In Metric mode, you get Metric value.
// unitString - In Imperial mode, you get Imperial units. In Metric mode, you get Metric units.
QVariant data::value()
{
// fetch Imperial data value from back-end application
// get current System of measurement
// if current System of measurement is Metric, convert data value from Imperial to Metric
// return data value
}
QString data::unitString()
{
// fetch Imperial unit from back-end application
// get current System of measurement
// if current System of measurement is Metric, convert unit from Imperial to Metric
// return unit
}
void data::setValue(QVariant value)
{
// get current System of measurement
// if current System of measurement is Metric, convert value from Metric to Imperial
// write value to back-end Controller application
}