跨多个类更改变量(使用 WPF 的 C#)
本文关键字:WPF 使用 变量 改变 | 更新日期: 2023-09-27 17:56:04
我正在制作一个视觉小说风格的游戏应用程序,其中包含一个 20 个问题的测验供用户回答(该应用程序被设计为修订工具)。我希望能够更新用户正确回答的每个问题的分数,并在 UI 元素中显示分数,但我似乎无法在私有课程中使用我的 playerScore 变量。如果我能解决这个问题,将不胜感激。
这是我在其中使用变量的窗口的当前 XAML:
<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" />
<TextBlock x:Name="Q2A1" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Q2A1_MouseLeftButtonDown" Text=" > Failure to Function Adequately" Visibility="Hidden" />
<TextBlock x:Name="Q2A2" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="7" MouseLeftButtonDown="Q2A2_MouseLeftButtonDown" Text=" > Feeling Fairly Awful" Visibility="Hidden" />
<TextBlock x:Name="Q2A3" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="8" MouseLeftButtonDown="Q2A3_MouseLeftButtonDown" Text=" > Failing to Feel Alright" Visibility="Hidden" />
<TextBlock x:Name="Q2Cont" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Q2Cont_MouseLeftButtonDown" Text=" > Next question" Visibility="Hidden" />
</Grid>
这是我的代码隐藏,包括第二个问题之前的所有内容:
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
{
public class WorldVariables
{
public int[] worldLocale = { 0, 1, 2, 3 };
public string worldName;
public int playerScore = 0;
public string playerName;
}
/// <summary>
/// Interaction logic for GameWindow.xaml
/// </summary>
public partial class GameWindow : Window
{
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 playerName, you want to revise your knowledge?";
// Show option to begin test
OptionBegin.Visibility = Visibility.Visible;
}
private void OptionBegin_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Change dialogue for each question
Dialogue.Text = "Red: Alright, first question. What does DSN stand for?";
// Hide option to begin test as this is no longer needed
OptionBegin.Visibility = Visibility.Hidden;
// Show three new options as answers to question
Q1A1.Visibility = Visibility.Visible;
Q1A2.Visibility = Visibility.Visible;
Q1A3.Visibility = Visibility.Visible;
}
private void Q1A1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Dialogue.Text = "Red: Not quite. Why don't we try another question?.";
Q1A1.Visibility = Visibility.Hidden;
Q1A2.Visibility = Visibility.Hidden;
Q1A3.Visibility = Visibility.Hidden;
Q1Cont.Visibility = Visibility.Visible;
}
private void Q1A2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Can't access variable from here? Could be a problem
// playerScore = playerScore + 1;
Dialogue.Text = "Red: Correct! Let's try another one, shall we?";
Q1A1.Visibility = Visibility.Hidden;
Q1A2.Visibility = Visibility.Hidden;
Q1A3.Visibility = Visibility.Hidden;
Q1Cont.Visibility = Visibility.Visible;
}
private void Q1A3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Dialogue.Text = "Red: Not quite. Why don't we try another question?.";
Q1A1.Visibility = Visibility.Hidden;
Q1A2.Visibility = Visibility.Hidden;
Q1A3.Visibility = Visibility.Hidden;
Q1Cont.Visibility = Visibility.Visible;
}
private void Q1Cont_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Q1Cont.Visibility = Visibility.Hidden;
Dialogue.Text = "Red: What does FFA stand for?";
Q2A1.Visibility = Visibility.Visible;
Q2A2.Visibility = Visibility.Visible;
Q2A3.Visibility = Visibility.Visible;
}
正如您在Q1A2_MouseLeftButtonDown中看到的那样,我已经在注释中包含了我对 playerScore 变量的意图,因为当我实际尝试使用它时它将无法运行。
您需要将
WorldVariables
的实例添加到GameWindow
类中:
public partial class GameWindow : Window
{
private readonly WorldVariables worldVariables = new WorldVariables();
然后,可以访问事件处理程序中的playerScore
字段:
private void Q1A2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
worldVariables.playerScore = playerScore + 1;
除非这是一个简单的疏忽,否则您可能需要更多地研究类、实例和静态变量之间的差异,以便更好地了解正在发生的事情。一个好的起点是 MSDN 上的类和结构(C# 编程指南)。