0

可能是一件愚蠢的事情,但我无处找到答案......

我的 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;

工作得很好。我错过了什么?

//edit:这个问题会引起混淆,因为它是伪代码,我放的时候没有想太多。这是按钮的实际定义方式:

<Viewbox Grid.Row="1" Grid.Column="0">
     <Button x:Name="_5v5OwnBlueButton" Content="5:00" Margin="5" />
</Viewbox>
4

1 回答 1

1

你在使用线程吗?

试试这个:

string timer = "5:00"; 
     this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action) () => 
            {

              Button.Content = timer;
            });
于 2013-04-03T07:29:47.627 回答