为什么 ProgressBar.SetValue 会抛出带有有效值的 ArgumentException ?
我正在使用 WPF 的 ProgressBar 对象,它有一个 Value 属性,我可以向它传递一个常量 int 或十进制值,它不会抱怨,但如果我向它传递一个 float(或 int 或 string)变量值,它会抛出一个ArgumentException (例如 "Message='0.02380952' 不是属性 'Value' 的有效值。"
)对我来说似乎很荒谬,并且让我难住了。我下面的 MS 示例 使用常量浮点值。 对 Value 属性的 MS 文档引用 然而说它是一个 int,这似乎是错误的,因为我可以传递它常量 .1 或 .5 并获得正确的行为,而我的 MS 示例使用 0 和 1 以及最小值和最大值。那么,我需要在这里做什么?
我的代码:
xaml:
<ProgressBar x:Name="prgProgress" Width="200" Height="20" Minimum="0" Maximum="100" />
c#:
float numMems = ListToShow.Count;
float numDone = 0.0f;
int fracDone = 0;
string sProgress = "0%";
foreach (RAM_Member mem in ListToShow)
{
if (isCanceled == true) break;
mem.CalculateValues();
numDone++;
fracDone = (int)(100.0 * numDone / numMems);
sProgress = (100 * numDone / numMems).ToString("0.") + "%";
Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { prgProgress.SetValue(ProgressBar.ValueProperty, fracDone); }, null);
Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { txtProgress.SetValue(TextBlock.TextProperty, sProgress); }, null);
}
I am using WPF's ProgressBar object, which has a Value property, and I can pass it a constant int or decimal value and it doesn't complain, but if I pass it a float (or int or string) variable value, it barfs an ArgumentException (e.g. "Message='0.02380952' is not a valid value for property 'Value'."
) at me, which just seems preposterous to me, and has me stumped. The MS example I am following uses constant float values. The MS doc reference to the Value property however says it is an int, which seems just wrong since I can pass it constant .1 or .5 and get the right behavior, and my MS example uses 0 and 1 and the min and max values. So, what do I need to do here?
My code:
xaml:
<ProgressBar x:Name="prgProgress" Width="200" Height="20" Minimum="0" Maximum="100" />
c#:
float numMems = ListToShow.Count;
float numDone = 0.0f;
int fracDone = 0;
string sProgress = "0%";
foreach (RAM_Member mem in ListToShow)
{
if (isCanceled == true) break;
mem.CalculateValues();
numDone++;
fracDone = (int)(100.0 * numDone / numMems);
sProgress = (100 * numDone / numMems).ToString("0.") + "%";
Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { prgProgress.SetValue(ProgressBar.ValueProperty, fracDone); }, null);
Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { txtProgress.SetValue(TextBlock.TextProperty, sProgress); }, null);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您链接到等效的 Windows 窗体 WPF 它是双精度型,这意味着如果您使用
SetValue
,您将需要使用该确切类型,因为没有隐式转换。来自
SetValue
:(为什么不使用包装器呢?即
prgProgress.Value = fracDone
)You linked to the windows form equivalent, in WPF it's a double, which means that if you use
SetValue
you will need to use that exact type as there is no implicit conversion.From
SetValue
:(Why don't you use the wrapper instead? i.e.
prgProgress.Value = fracDone
)