第二个cs代码文件窗口上的C#WPF更新标签
这里的新手很抱歉这个愚蠢的问题,但出于某种原因,我对这个简单的小问题有一个真正的问题。我在WPF窗口上有一个标签和文本块,我正试图从第二个cs代码文件中更新它们。我已经尝试了下面的代码,但是标签没有更新…非常感谢任何帮助或指导第二个cs代码文件窗口上的C#WPF更新标签,c#,wpf,C#,Wpf,这里的新手很抱歉这个愚蠢的问题,但出于某种原因,我对这个简单的小问题有一个真正的问题。我在WPF窗口上有一个标签和文本块,我正试图从第二个cs代码文件中更新它们。我已经尝试了下面的代码,但是标签没有更新…非常感谢任何帮助或指导 820File.cs MainWindow main = new MainWindow(); string status820Text = "Now importing blah"; string status820Label = "Now importing blah
- 820File.cs
MainWindow main = new MainWindow(); string status820Text = "Now importing blah"; string status820Label = "Now importing blah"; main.Update820Status(ref status820Text, ref status820Label);
MyWindow w = new MyWindow(); w.MyProperty = "Now importing blah"; // Here we set the initial text w.ShowDialog(); // Show a modal dialog so this class waits for the changed text string changedText = w.MyProperty; // Here we read the changed text
- MainWindow.cs
public void Update820Status(ref string status820Text, ref string status820Label) { this.StatusLabel.Content =status820Label; this.StatusTextBlock.Text = status820Text; }
谢谢。当您编写
main=newmainwindow()
时,您正在创建一个全新的MainWindow
实例,它与屏幕上现有的窗口无关
您需要传递现有的
main窗口
实例。该实例将在WinForms中工作,而不是在WPF窗口中
请尝试添加一个参数。然后在新窗口关闭后读取(或添加eventhandler以立即更新)
820File.cs
MainWindow main = new MainWindow();
string status820Text = "Now importing blah";
string status820Label = "Now importing blah";
main.Update820Status(ref status820Text, ref status820Label);
MyWindow w = new MyWindow();
w.MyProperty = "Now importing blah"; // Here we set the initial text
w.ShowDialog(); // Show a modal dialog so this class waits for the changed text
string changedText = w.MyProperty; // Here we read the changed text
我的窗口
public partial class Mywindow : Window
{
public string MyProperty { get; set; }
public MyWindow()
{
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
MyProperty = "Some new text"; // Set the text to something new
this.Close();
}
}
以下代码在WPF中工作 窗口中的axml:
<Label x:Name="lblStatus" Foreground="Red" Content=""/>
这看起来很好,问题可能在其他地方。您可以演示如何创建标签和文本块吗?您不应该使用
ref
parameters.SLaks是正确的。在像您这样的某些情况下,您也可以尝试使用Application.Current.MainWindow
。错误。它在WinForms和WPF中都会失败,因为他使用了错误的实例。