可能是一件愚蠢的事,但我找不到答案......
我在C#WPF项目中有以下代码:
string timer = "5:00";
Button.Content = timer;
一旦代码执行到达这一点,它就会给我System.InvalidOperationException
。它也不适用于Button.Content = "5:00";
,但在另一个函数中这个代码很有趣
string newLabel = Math.Floor(timerSec / 60f).ToString() + ":" + (timerSec % 60).ToString("0#");
Button.Content = newLabel;
工作得很好。我错过了什么?
//编辑:这个问题引起混淆,因为它是伪代码,当我提出它时我并没有想太多。这是按钮实际定义的方式:
<Viewbox Grid.Row="1" Grid.Column="0">
<Button x:Name="_5v5OwnBlueButton" Content="5:00" Margin="5" />
</Viewbox>
答案 0 :(得分:1)
你在使用线程吗?
试试这个:
string timer = "5:00";
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action) () =>
{
Button.Content = timer;
});