在我的MainWindow.xaml中,我有:
<Window x:Class="UnionPayExhangeRate3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Union Pay Exchange Rate" Height="350" Width="525">
<Grid>
<Label Content="" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top" Width="497"/>
</Grid>
</Window>
在我的App.xaml.cs中,我有:
namespace UnionPayExhangeRate3
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
//Change the label in MainWindow
}
}
}
我没有看到任何识别标签的方法,所以我是否必须为标签设置ID并以某种方式调用它?
答案 0 :(得分:1)
将Name
属性添加到Label
<Label x:Name="myLabel" Content="" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top" Width="497"/>
所以你可以在这里访问它
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
//Change the label in MainWindow
mainWindow.myLabel.Content = "Hello World";
}