我是 C# 的新手,并试图从我的类中创建一些控制器但不幸的是它没有显示在窗口中。这是我的代码。
<Window x:Class="SWV_IT_V0._2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid x:Name="MainGrid">
</Grid>
</Window>
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBlock txtBlock = new TextBlock();
txtBlock.Text = "Hello 1";
MainGrid.Children.Add(txtBlock);
ManageControlers myC = new ManageControlers();
}
}
运行此代码时,我的窗口上出现“Hello 1”,但是当我尝试从我的班级创建文本块时:
namespace SWV_IT_V0._2
{
public partial class ManageControlers : MainWindow
{
public TextBlock txtBlock;
public ManageControlers()
{
txtBlock = new TextBlock();
txtBlock.Text = "Hello 2";
MainGrid.Children.Add(txtBlock);
}
}
}
窗口什么都没有显示?怎么可能解决这个问题?提前致谢。