C#WPF:无法通过类将texbox.Text连接到label.Content

问题描述 投票:0回答:1

为什么这不起作用。我想在UserInputPage中创建一个带有文本框的用户输入,并将其显示在UserLabelPage的标签上,但是出现错误FirstUserLabel = null。我该如何解决这个问题或我做错了方法?预先感谢您的宝贵时间。

德克

App.xaml.cs:

namespace PlayertoLabeltest
{

    public partial class App : Application
    {
        public static MainWindow ParentWindowRef;
    }
}

MainWindow.xaml:

<Grid>
        <DockPanel>
            <Frame x:Name="ParentFrame" NavigationUIVisibility="Hidden"/>
        </DockPanel>
</Grid>

MainWindow.xaml.cs:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            App.ParentWindowRef = this;
            this.ParentFrame.Navigate(new UserInputPage());
        }
    }

UserInputPage.xaml:

 <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBox x:Name="singlePlayer_Input" Grid.Row="1" Grid.Column="1" Width="150" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <TextBox Grid.Row="1" Grid.Column="2" Width="150" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>

        <Button Click="UserInputToLabel_Click" Grid.Row="3" Grid.Column="3" Width="100" Height="50" Content="Click Here"/>
    </Grid>

UserInputPage.xaml.cs:

 public partial class UserInputPage : Page
    {
        private Player FirstPlayer_Input;
        private TextBox SinglePlayer_TextBox;

        public UserInputPage()
        {
            InitializeComponent();
        }

        public void UserInputToLabel_Click(object sender, RoutedEventArgs e)
        {
            SinglePlayer_TextBox = singlePlayer_Input;
            //System.Diagnostics.Debug.WriteLine(singlePlayer_Input.Text);
            FirstPlayer_Input = new Player(SinglePlayer_TextBox.Text);
            FirstPlayer_Input.PlayertoLabel();
            App.ParentWindowRef.ParentFrame.Navigate(new UserLabelPage());
        }
    }

UserLabelPage.xaml:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Label x:Name="singlePlayer_Label" Content="" Grid.Row="1" Grid.Column="1" Width="150" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>

UserLabelPage.xaml.cs:

 public partial class UserLabelPage : Page
    {
        private Player FirstPlayer_Label;

        public UserLabelPage()
        {
            InitializeComponent();
            FirstPlayer_Label = new Player(singlePlayer_Label);
        }
    }

Player.cs:

 class Player
    {
        private string FirstUser_Input;
        private Label FirstUser_Label;

        public Player(string text)
        {
            FirstUser_Input = text;
            System.Diagnostics.Debug.WriteLine(FirstUser_Input);
        }

        public Player(Label FirstUser)
        {
            FirstUser_Label = FirstUser; //This one gives FirstUser_Label = null
            //FirstUser_Label.Content = "Rubeus Hagrid"; //This One works
            PlayertoLabel();
        }

        public void PlayertoLabel()
        {
            FirstUser_Label.Content = FirstUser_Input;
            System.Diagnostics.Debug.WriteLine(FirstUser_Input);        
        }
    }

c# wpf textbox label
1个回答
1
投票

建议:我建议绑定将产生更清晰代码和更容易理解的值。

不过,请在下面找到您问题的答案

我相信,该错误发生在UserInputPage.xaml.cs内部:

FirstPlayer_Input = new Player(SinglePlayer_TextBox.Text);

在播放器类内部,变量private Label FirstUser_Label;不在构造函数之一(public Player(string text))中初始化。

所以,像下面这样启动它,

 `public Player(string text)
    {
        FirstUser_Input = text;
        FirstUser_Label = new Label(); //This line ensures that the FirstUser_Label is not null. It has a empty label value by default.
        System.Diagnostics.Debug.WriteLine(FirstUser_Input);
    }`

注:尚未亲自测试代码。

© www.soinside.com 2019 - 2024. All rights reserved.