gpt4 book ai didi

c# - UWP app .cs 文件从 xaml.cs 继承静态变量

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:51 27 4
gpt4 key购买 nike

我已经创建了 PlayPage.xaml、PlayPage.xaml.cs 和 Game.cs 文件。PlayPage.xaml.cs 有两个变量,windowWidth 和 windowHeight。

我想从 Game.cs 访问两个公共(public)静态变量。

游戏.cs:

namespace UwpApp
{
class Game
{
static Rectangle rectangle;
PlayPage pg = new PlayPage();

//Create a rectangle
public Rectangle draw()
{
rectangle = new Rectangle();
rectangle.Width = 70;
rectangle.Height = 70;
rectangle.Fill = new SolidColorBrush(Colors.Green);
Canvas.SetLeft(rectangle, randPos());
Canvas.SetTop(rectangle, randPos());
return rectangle;
}

//Create a random X and Y position
private Int32 randPos()
{
Random rnd = new Random();
Debug.WriteLine(pg.windowWidth);
return rnd.Next(0 , (int)pg.windowWidth);
}
}
}

PlayPage.xaml.cs:

namespace UwpApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public partial class PlayPage : Page
{
DispatcherTimer timer;
Rectangle rect;
bool isTapped;
public static double windowWidth, windowHeight;

Game game = new Game();

public PlayPage()
{
this.InitializeComponent();

startCounter.Visibility = Visibility.Collapsed;
isTapped = false;
}

private void Load_Variable(object sender, RoutedEventArgs e)
{
windowWidth = canvas.ActualWidth;
windowHeight = canvas.ActualHeight;
}

//Counter animation. (Number opacity, fall)
private void counterAnimation()
{
startCounter.Visibility = Visibility.Visible;

//Set Counter Horizontal Align
double left = (canvas.ActualWidth - startCounter.ActualWidth) / 2;
Canvas.SetLeft(startCounter, left);

Storyboard storyboard = new Storyboard();

DoubleAnimation opacityAnim = new DoubleAnimation();
DoubleAnimation fallAnim = new DoubleAnimation();

opacityAnim.From = 1;
opacityAnim.To = 0;
opacityAnim.Duration = new Duration(TimeSpan.FromSeconds(1));
opacityAnim.AutoReverse = false;
opacityAnim.RepeatBehavior = new RepeatBehavior(3);

Storyboard.SetTarget(opacityAnim, this.startCounter);
Storyboard.SetTargetProperty(opacityAnim, "(UIElement.Opacity)");

fallAnim.From = -115;
fallAnim.To = canvas.ActualHeight / 2;
fallAnim.Duration = new Duration(TimeSpan.FromSeconds(1));
fallAnim.AutoReverse = false;
fallAnim.RepeatBehavior = new RepeatBehavior(3);

Storyboard.SetTarget(fallAnim, this.startCounter);
Storyboard.SetTargetProperty(fallAnim, "(Canvas.Top)");

storyboard.Children.Add(opacityAnim);
storyboard.Children.Add(fallAnim);

storyboard.Begin();
}

//Countdown Timer
private void countDown()
{
timer = new DispatcherTimer();
timer.Tick += startCounter_CountDown;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}

//Change Countdown value
private void startCounter_CountDown(object sender, object e)
{
int counterNum = int.Parse(startCounter.Text);
counterNum -= 1;
startCounter.Text = counterNum.ToString();

if (counterNum == 1)
{
timer.Stop();
StartedGame();
}
}

//Tap or Click to start the game
private void TapToStart(object sender, TappedRoutedEventArgs e)
{
if(!isTapped)
{
isTapped = true;
counterAnimation();
countDown();
this.TapToStartText.Visibility = Visibility.Collapsed;
}
}

//Create rectangles
private void StartedGame()
{
rect = game.draw();
canvas.Children.Add(game.draw());
Debug.WriteLine(windowWidth.ToString());
}
}
}

还有一件事:这一行我得到一个错误:PlayPage pg = new PlayPage(); Error (pic)

最佳答案

正如它所说,您图片中的错误是由于无限递归造成的。 Game 的构造函数实例化一个新的 PlayPage。 PlayPage 的构造函数实例化一个新游戏。它实例化了一个新的 PlayPage。实例化一个新游戏。等等。

静态成员通过类名访问,而不是通过实例访问。像这样:

PlayPage.windowWidth

关于c# - UWP app .cs 文件从 xaml.cs 继承静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37953627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com