我在WindowAfterLogin.xaml
中有一段代码:
<TextBlock x:Name="In_Time" Foreground="#FF55534F"
FontSize="71.312" FontFamily="HelveticaNeueCyr"
Height="79.45" LineStackingStrategy="BlockLineHeight"
Canvas.Left="2.724" LineHeight="71.312"
TextAlignment="Center" TextWrapping="Wrap"
Canvas.Top="69.985" Width="231.581" Text="09:00" />
然后我想更改TextBlock
中WindowAfterLogin.xaml.cs
的值,所以我已经这样做了:
MainWindow objMainWindow = new MainWindow();
WindowAfterLogin objAfterLogin = new WindowAfterLogin();
objMainWindow.Show();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close();
但是,当我按下F5按钮(编译)时,它没有改变。这里的问题在哪里?
答案 0 :(得分:1)
您是否正在创建WindowAfterLogin
的单独实例?如果您想这样做,则必须Show()
新实例。试试这个:
WindowAfterLogin objAfterLogin = new WindowAfterLogin();
objAfterLoginShow();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
//Close(); //Close any other window that needs to be closed..
如果您希望In_Time.Text
更改为当前实例(假设已经显示或可见),您可以尝试使用构造函数或初始化WindowAfterLogin
的方法:
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
答案 1 :(得分:0)
作为 位 答案的简化,请尝试编写类似这样的内容而不是代码
MainWindow objMainWindow = new MainWindow();
objMainWindow.Show();
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close(); //Not sure if You need it. Try to comment this line to be able to notice textblock text changes
我认为你需要
DateTime.Now.ToString("hh:mm")
而不是
DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()