[英]System.InvalidOperationException on Button.Content in WPF
Probably a stupid thing but I found the answer nowhere... 可能是愚蠢的事情,但是我却找不到答案...
I have the following code in my C# WPF Project: 我的C#WPF项目中包含以下代码:
string timer = "5:00";
Button.Content = timer;
and it gives me System.InvalidOperationException
once code execution reaches this point. 一旦代码执行到这一点,它就会给我System.InvalidOperationException
。 It also doesn't work with Button.Content = "5:00";
它也不适用于Button.Content = "5:00";
but funny enough in another function this code 但是在另一个函数中这个代码足够有趣
string newLabel = Math.Floor(timerSec / 60f).ToString() + ":" + (timerSec % 60).ToString("0#");
Button.Content = newLabel;
works just fine. 效果很好。 What am I missing? 我想念什么?
//edit: the question cause confusion because it is pseudo code and I didn't think too much when I put it. // edit:这个问题引起了混乱,因为它是伪代码,我放的时候并没有想太多。 here is how the button is actually defined: 这是按钮的实际定义方式:
<Viewbox Grid.Row="1" Grid.Column="0">
<Button x:Name="_5v5OwnBlueButton" Content="5:00" Margin="5" />
</Viewbox>
Are you using threads? 您在使用线程吗?
Try this: 尝试这个:
string timer = "5:00";
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action) () =>
{
Button.Content = timer;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.