C# UWP app.cs文件继承来自xaml.cs的静态变量
我已经创建了PlayPage.xaml、PlayPage.xaml.cs和Game.cs文件。 PlayPage.xaml.cs有两个变量,windowWidth和windowHeight 我想从Game.cs访问两个公共静态变量 Game.cs:C# UWP app.cs文件继承来自xaml.cs的静态变量,c#,xaml,uwp,C#,Xaml,Uwp,我已经创建了PlayPage.xaml、PlayPage.xaml.cs和Game.cs文件。 PlayPage.xaml.cs有两个变量,windowWidth和windowHeight 我想从Game.cs访问两个公共静态变量 Game.cs: namespace UwpApp { class Game { static Rectangle rectangle; PlayPage pg = new PlayPage(); //
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);
}
}
}
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.xaml.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);
}
}
}
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());
}
}
}
namespace-UwpApp
{
///
///可以单独使用或在框架内导航到的空页。
///
公共部分类播放页面:第页
{
调度定时器;
矩形矩形;
布尔伊斯塔普;
公共静态双窗宽、窗高;
游戏=新游戏();
公共播放页()
{
this.InitializeComponent();
startCounter.Visibility=Visibility.Collapsed;
isTapped=false;
}
私有void Load_变量(对象发送器、路由目标)
{
windowWidth=canvas.ActualWidth;
windowHeight=canvas.ActualHeight;
}
//计数器动画。(数字不透明度,下降)
私有void反动画()
{
startCounter.Visibility=可见性.Visibility;
//设置计数器水平对齐
左双=(canvas.ActualWidth-startCounter.ActualWidth)/2;
Canvas.SetLeft(startCounter,左侧);
情节提要情节提要=新情节提要();
DoubleAnimation opacityAnim=新的DoubleAnimation();
DoubleAnimation fallAnim=新的DoubleAnimation();
来自的不透明度=1;
乳浊指数为0;
opacityAnim.Duration=新的持续时间(TimeSpan.FromSeconds(1));
opacityAnim.AutoReverse=假;
opacityAnim.RepeatBehavior=新的RepeatBehavior(3);
故事板.SetTarget(opacityAnim,this.startCounter);
Storyboard.SetTargetProperty(opacityAnim,”(UIElement.Opacity)”;
fallAnim.From=-115;
fallAnim.To=canvas.ActualHeight/2;
fallAnim.Duration=新的持续时间(TimeSpan.FromSeconds(1));
fallAnim.AutoReverse=false;
fallAnim.RepeatBehavior=新的RepeatBehavior(3);
故事板.SetTarget(fallanm,this.startCounter);
Storyboard.SetTargetProperty(fallAnim,”(Canvas.Top)”;
情节提要.Children.Add(opacityAnim);
故事板.Children.Add(fallAnim);
故事板。开始();
}
//倒数计时器
私人空间倒计时()
{
计时器=新调度程序();
timer.Tick+=开始计数器倒计时;
timer.Interval=新的时间跨度(0,0,1);
timer.Start();
}
//更改倒计时值
私有void startCounter_倒计时(对象发送方,对象e)
{
int counterNum=int.Parse(startCounter.Text);
counterNum-=1;
startCounter.Text=counterNum.ToString();
if(counterNum==1)
{
timer.Stop();
开始游戏();
}
}
//点击或单击开始游戏
专用void TapToStart(对象发送器,TappedRoutedEventArgs e)
{
如果(!isTapped)
{
ISTAPP=真;
反动画();
倒计时();
this.TapToStartText.Visibility=可见性.已折叠;
}
}
//创建矩形
私有void StartedGame()
{
rect=game.draw();
canvas.Children.Add(game.draw());
Debug.WriteLine(windowWidth.ToString());
}
}
}
还有一件事:我在这一行遇到一个错误:PlayPage pg=newplaypage();
如图所示,图片中的错误是由于无限递归造成的。游戏的构造函数实例化一个新的PlayPage。PlayPage的构造函数实例化了一个新游戏。它实例化了一个新的PlayPage。它实例化了一个新游戏。不断地 静态成员是通过类名而不是实例访问的。像这样:
PlayPage.windowWidth
此外,UWP中最好避免使用静态数据。