用户输入未分配给变量? (带WPF的C#)

时间:2014-04-02 21:25:35

标签: c# wpf xaml

我正在创建一个应用程序,用户在其中输入所需的名称,该名称在分配给playerName字符串变量之前经过验证。由于某种原因,输入没有分配给变量,因此当我运行程序时,空白区域显示名称应该以文本块的形式存在。我不知道造成这种情况的原因。我使用两个单独的窗口,InputWindow和GameWindow,用于应用程序。

InputWindow的XAML和代码隐藏如下:

<Window x:Class="COMP4_Project.InputWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="New Game" Height="120" Width="250" ResizeMode="CanMinimize">
<Grid Margin="10">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />

    </Grid.RowDefinitions>
    <Label>Enter name:</Label>
    <TextBox x:Name="textInput" Grid.Column="2" Margin="0,0,0,0" MaxLength="10"></TextBox>
    <Button x:Name="Confirm" Click="Confirm_Click" Grid.Row="2" Width="80" Height="25" Content="Confirm" Margin="0,10,0,41" />
    <Button x:Name="Cancel" Click="Cancel_Click" Grid.Row="2" Grid.Column="1" Width="80" Height="25" Content="Cancel" HorizontalAlignment="Right" Margin="54,10,0,41" />
</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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.Shapes;

namespace COMP4_Project
{
public class WorldVariables
{
    public int[] worldLocale = { 0, 1, 2, 3 };
    public string worldName;
    public int playerScore = 0;
    public string playerName;
}

/// <summary>
/// Interaction logic for InputWindow.xaml
/// </summary>
public partial class InputWindow : Window
{
    private readonly WorldVariables worldVariables = new WorldVariables();

    public InputWindow()
    {
        InitializeComponent();
    }

    private void Confirm_Click(object sender, RoutedEventArgs e)
    {
        // Text entered into the "textInput" textbox is assigned to the userInput variable
        string userInput = textInput.Text;

        // Validation for user input using regular expressions library
        Regex r = new Regex("^[a-zA-Z]+$");
        if (r.IsMatch(userInput))
        {
            // Assigns text held in userInput variable to playerName variable
            worldVariables.playerName = userInput;
            // Close input window
            Close();
            // Opens game window
            Window win = new GameWindow();
            // Sets owner of game window as the main window
            win.Owner = Application.Current.MainWindow;
            win.ShowDialog();
        } 
        else
        {
            // Message informing user that input was invalid
            MessageBox.Show("Only letters permitted; name field can't be empty. Try again!");
        }
    }

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        // Close input window
        Close();
    }
}
}

这是GameWindow的XAML和代码隐藏,直到第一个在文本中使用playerName字符串的实例。

<Window x:Class="COMP4_Project.GameWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Psych" Height="400" Width="500" ResizeMode="CanMinimize">
<Grid Margin="10">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="180"/>
        <ColumnDefinition Width="75" />
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="17" />
        <RowDefinition Height="113" />
        <RowDefinition Height="80" />
        <RowDefinition Height="33" />
        <RowDefinition Height="33"/>
        <RowDefinition Height="17" />
        <RowDefinition Height="17" />
        <RowDefinition Height="17" />
        <RowDefinition Height="66" />
    </Grid.RowDefinitions>
    <TextBlock x:Name="WorldName" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="3" TextWrapping="NoWrap" TextAlignment="Left" Text="World: Tunnel Entrance" />
    <TextBlock x:Name="ScoreTracker" Grid.Column="5" Margin="0,0,0,0" TextWrapping="Wrap" TextAlignment="Left" Text="Score:" />
    <TextBlock x:Name="ScoreCount" Grid.Column="6" Margin="0,0,0,0" TextWrapping="Wrap" TextAlignment="Left" Text="0" />
    <TextBlock x:Name="Dialogue"  Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="5" TextWrapping="Wrap" TextAlignment="Justify" Text="Where do you want to go? Click on the options below to change your location or click on an NPC to talk to them." />
    <TextBlock x:Name="Option1" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Option1_MouseLeftButtonDown" Text="   > Go Left" />
    <TextBlock x:Name="Option2" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="7" MouseLeftButtonDown="Option2_MouseLeftButtonDown" Text="   > Go Right" />
    <TextBlock x:Name="Option3" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="8" MouseLeftButtonDown="Option3_MouseLeftButtonDown" Text="   > Enter Tunnel" />
    <TextBlock x:Name="Option4" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Option4_MouseLeftButtonDown"  Text="   > Go Back" Visibility="Hidden" />
    <Image x:Name="Portrait" Source="C:\Psyche\Images\red1.png" Grid.Column="4" Grid.Row="1" Grid.RowSpan="4" Visibility="Hidden" />
    <Image x:Name="Sprite" Source="C:\Psyche\Images\redsprite1.png" Grid.Column="2" Grid.Row="3" MouseLeftButtonDown="Sprite_MouseLeftButtonDown" />
    <TextBlock x:Name="OptionBegin" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="OptionBegin_MouseLeftButtonDown" Text="   > Begin Test" Visibility="Hidden" />
    <TextBlock x:Name="Q1A1" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Q1A1_MouseLeftButtonDown" Text="   > Divergence from Standard Normality" Visibility="Hidden" />
    <TextBlock x:Name="Q1A2" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="7" MouseLeftButtonDown="Q1A2_MouseLeftButtonDown" Text="   > Deviation from Social Norms" Visibility="Hidden" />
    <TextBlock x:Name="Q1A3" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="8" MouseLeftButtonDown="Q1A3_MouseLeftButtonDown" Text="   > Damaging Social Negligence" Visibility="Hidden" />
    <TextBlock x:Name="Q1Cont" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Q1Cont_MouseLeftButtonDown" Text="   > Next question" Visibility="Hidden" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace COMP4_Project
{

/// <summary>
/// Interaction logic for GameWindow.xaml
/// </summary>  

public partial class GameWindow : Window
{
    private readonly WorldVariables worldVariables = new WorldVariables();

    public GameWindow()
    {
        InitializeComponent();

        // Set background image for window
        ImageBrush myBrush = new ImageBrush();
        myBrush.ImageSource =
            new BitmapImage(new Uri(@"C:\Psyche\Images\background0.png", UriKind.Absolute));
        this.Background = myBrush;

        // Ensures non-player character sprite is visible on launching the game window
        Sprite.Visibility = Visibility.Visible;
    }

    private void Option1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Change background image
        this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background1.png")));
        // Hide character sprite
        Sprite.Visibility = Visibility.Hidden;
        // Hide character portrait (should not be visible regardless, this is simply a failsafe)
        Portrait.Visibility = Visibility.Hidden;
        // Change visible options
        Option1.Visibility = Visibility.Hidden;
        Option2.Visibility = Visibility.Hidden;
        Option3.Visibility = Visibility.Hidden;
        Option4.Visibility = Visibility.Visible;
        // Alter WorldName field to match change in scene
        WorldName.Text = "Location: Stream";
        // Alter dialogue to match change in scene
        Dialogue.Text = "There's a tree across the stream. Not much else around, though.";

    }

    private void Option2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background2.png")));
        Sprite.Visibility = Visibility.Hidden;
        Portrait.Visibility = Visibility.Hidden;
        Option1.Visibility = Visibility.Hidden;
        Option2.Visibility = Visibility.Hidden;
        Option3.Visibility = Visibility.Hidden;
        Option4.Visibility = Visibility.Visible;
        WorldName.Text = "Location: Forest";
        Dialogue.Text = "It's pretty dark here due to the dense canopy of trees overhead. Kind of creepy when you think about it.";
    }

    private void Option3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background3.png")));
        Sprite.Visibility = Visibility.Hidden;
        Portrait.Visibility = Visibility.Hidden;
        Option1.Visibility = Visibility.Hidden;
        Option2.Visibility = Visibility.Hidden;
        Option3.Visibility = Visibility.Hidden;
        Option4.Visibility = Visibility.Visible;
        WorldName.Text = "Location: Tunnel";
        Dialogue.Text = "There's nothing here, except a small dot of light in the distance. That might be the exit, but it's too far to check safely.";
    }

    private void Option4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background0.png")));
        Sprite.Visibility = Visibility.Visible;
        Portrait.Visibility = Visibility.Hidden;
        Option1.Visibility = Visibility.Visible;
        Option2.Visibility = Visibility.Visible;
        Option3.Visibility = Visibility.Visible;
        Option4.Visibility = Visibility.Hidden;
        // Revert worldname to previous text
        WorldName.Text = "Location: Tunnel Entrance";
        // Revert dialogue to previous text
        Dialogue.Text = "Where do you want to go? Click on the options below to change your location or click on an NPC to talk to them.";
    }

    private void Sprite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Hide character sprite
        Sprite.Visibility = Visibility.Hidden;
        // Show character portrait
        Portrait.Visibility = Visibility.Visible;
        // Hide all previous options to avoid overlap
        Option1.Visibility = Visibility.Hidden;
        Option2.Visibility = Visibility.Hidden;
        Option3.Visibility = Visibility.Hidden;
        Option4.Visibility = Visibility.Hidden;
        // Dialogue text changes to show NPC dialogue
        Dialogue.Text = "Red: Oh, hey " + worldVariables.playerName + ", you want to revise your knowledge?";
        // Show option to begin test
        OptionBegin.Visibility = Visibility.Visible;
    }

除了这个bug之外,一切正常,所以任何修复它的帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

您的两个窗口都在创建WorldVariables的新私有实例:

private readonly WorldVariables worldVariables = new WorldVariables();

在一个实例中设置变量不会使其在另一个实例中可访问。


WorldVariables类中将变量声明为静态,以便该类的所有实例共享一个值:

public static string playerName;

或者找到一种传递WorldVariables类的第一个实例的方法,因此您总是引用其中包含播放器名称的实例。