简体   繁体   English

为什么此文本块仅显示“ TextBlock”作为其文本?

[英]Why does this textblock only display “TextBlock” as its text?

Really new and learning C# and following along some training video from PluralSight. 真正的新手,正在学习C#,并跟随PluralSight的一些培训视频。 Great videos, except you cannot ask questions, of course and I am not understanding why what I am seeing is different that what his screen is displaying even though I typed exactly what he has. 很棒的视频,当然,除了您不能问问题外,我也不理解为什么我看到的内容与他的屏幕显示内容有所不同,即使我输入的内容完全相同。

Textbox is named "Output". 文本框被命名为“输出”。 Initially, the actions were directly in the MainWindow constructor (which he explained is not good practice, so we moved it. Initially, this worked as it should: 最初,这些动作直接在MainWindow构造函数中(他解释说这不是一种好习惯,因此我们将其移动了。最初,它应按以下方式工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

            Employee e1 = new Employee();
            e1.Name = "Mike";

            Employee e2 = new Employee();
            e2.Name = "Miller";

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


        }
    }
}

This would display "Mike Miller" in the TextBlock. 这将在TextBlock中显示“ Mike Miller”。

However, when we moved it to this, all it says for the text is "TextBlock" 但是,当我们将其移至该位置时,它表示的文字是“ TextBlock”

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();
            e1.Name = "Mike";

            Employee e2 = new Employee();
            e2.Name = "Miller";

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

        }

    }
}

Am I missing something simple here? 我在这里缺少简单的东西吗?

Thanks! 谢谢!

If you take the .Length off the two strings, it should work. 如果您从两个字符串中删除.Length,它应该可以工作。 You're concatenating integers with a string using "+" and that doesn't work so well. 您正在使用“ +”将整数与字符串连接,但效果并不理想。

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

As Nico Schertler stated, verify that you subscribed to Loaded event of Window: 如Nico Schertler所述,请确认您已订阅Window的Loaded事件:

<Window ... Loaded="MainWindow_Loaded">
    ...
</Window>

In first case your code runs, because constructor of Window is called when Window is created. 在第一种情况下,您的代码运行,因为在创建Window时会调用Window的构造函数。 In second case, event handler is not called by default. 在第二种情况下,默认情况下不调用事件处理程序。 You should subscribe to this event. 您应该订阅此活动。

Answer is very certain from your question: Firstly if you are expecting output to be "Mike Miler" change your code to the one which is posted by Bravan. 从您的问题中可以肯定答案:首先,如果您期望输出为“ Mike Miler”,则将您的代码更改为Bravan发布的代码。 Secondly you need to add Loaded event to your MainWindow declaration in XAML. 其次,您需要在XAML中将Loaded事件添加到MainWindow声明中。 other than that nothings wrong there...!!! 除了那没什么不对...

Happy Coding...!! 快乐编码...! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM