MainWindow_Loaded 未在我的 WPF 应用程序上触发 [英] MainWindow_Loaded isn't triggered on my WPF Application

查看:24
本文介绍了MainWindow_Loaded 未在我的 WPF 应用程序上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在关注 Pluralsight C# Fundamentals: Part 1Classes and Objects 部分,视频指导我在 Visual Studio 中创建一个新的 WPF 应用程序和填写代码.结果如下.

I'm currently following the Pluralsight C# Fundamentals: Part 1 and on the Classes and Objects section the video instructs me to create a new WPF Applicaiton in Visual Studio and fill in the code. This results in the following.

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Employee e1 = new Employee("Ash");

            Employee e2 = new Employee("Lee");

            Output.Text = e1.Name + "" + e2.Name;
        }
    }
}

Employee 基本上是一个虚拟类,它定义了一个实例变量 Name 以演示构造函数的工作原理.

Employee is basically a dummy class which has been defined with a single instance variable Name in order to demonstrate how constructors work.

在我的 MainWindow.xaml 中还有一个名为 OutputTextBlock,我试图在代码的最后一行更新它.

There is also a TextBlock in my MainWindow.xaml called Output which I am trying to update on the last line of the code.

最初我在构造函数 MainWindow 中包含 MainWindow_Loaded 中的代码,教程说这是不好的做法,它应该看起来像我的第一个代码片段.

Initially I had the code contained in MainWindow_Loaded in the constructor MainWindow, the tutorial says this is bad practice and it should look like my first code snippet.

但是这会导致应用程序停止工作.我的问题是有什么问题?

However this stops the application working. My question is what is the problem?

教程过时了吗?我运行的是错误的版本吗?在 Beta 版中是否发生了变化?

Is the tutorial outdated? Am I running the wrong version? Was it changed in Beta?

谢谢.

推荐答案

确保 Loaded 事件映射到 MainWindow 的 XAML 中的 MainWindow_Loaded.

Make sure the Loaded event mapped to MainWindow_Loaded in the XAML for MainWindow.

将我下面的评论移到答案中,因为它似乎更有帮助:

Moving my comment below into the answer, as it seems to be more helpful:

打开 MainWindow.xaml(不是 MainWindow.xaml.cs),单击窗口(确保您没有选择其中一个控件),打开属性框(我相信 F4 会这样做),单击在属性框中的事件选项卡上,找到 Loaded 并确保映射到 MainWindow_Loaded(如果它是空白的,您应该能够选择您已经存在的)

open up MainWindow.xaml (not MainWindow.xaml.cs), click on the window (make sure you don't have one of the controls selected), open the properties box (i believe F4 will do that), click on the events tab in the properties box, find Loaded and make sure that is mapped to MainWindow_Loaded (if it is blank you should be able to select your already existing one)

这篇关于MainWindow_Loaded 未在我的 WPF 应用程序上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆