为什么我的UWP模拟器不能在Visual Studio中启动



我正在创建一个相当简单的应用程序,以了解Xamarin和SkiaSharp等的基本知识。基本上,只是一个球在背景上移动,但当我试图运行我的代码来查看模拟器中发生了什么时,它会在加载时卡住,当我跳回Vis时。Studio模拟器崩溃。

我的代码是:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.CompilerServices;
namespace Ball_Bounce
{
public partial class MainPage : ContentPage
{
int posX = 0;
int posY = 0;
int dx = 5;
int dy = 5;
int ballD = 100;
bool running = true;


SKPaint blackFillPaint = new SKPaint
{
Style = SKPaintStyle.Fill,
Color = SKColors.Black
};
SKPaint Ball = new SKPaint
{
Color = SKColors.Black,
};
public MainPage()
{
InitializeComponent();
Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
{
CanvasView.InvalidateSurface();
return true;
});

}
private void CanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
SKSurface surface = e.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.SteelBlue);
int width = e.Info.Width;
int height = e.Info.Height;
canvas.Translate(width / 2, height / 2);
do
{
posX += dx;
posY += dy;
} while (running == true);
canvas.DrawCircle(posX, posY, ballD, blackFillPaint);


}
}
}

在我添加声明变量的部分之前:

int posY = 0;
int dx = 5;
int dy = 5;
int ballD = 100;
bool running = true;

后来这个:

do
{
posX += dx;
posY += dy;
} while (running == true);

它会启动并显示漂亮的石板蓝色背景和黑色球(我启动时没有声明在哪里画球的变量,而是使用整数。(但一旦我试图让球穿过屏幕,它就根本不会启动。如有任何帮助,我们将不胜感激。

你的do..虽然循环永远不会退出,但它只是旋转,永远吞噬你的系统资源,直到操作系统杀死它。。。每次计时器启动时,只需要设置一帧的动画。不需要循环@Jason

移除循环意味着模拟器可以渲染图像,并自行刷新代码,就像它自己围绕代码构建的循环一样。

最新更新