我正在开发一个应用程序,其中我蚂蚁在后台做一些工作,在ui中,计时器应该像3 2 1中的开始一样运行,然后到那时我的操作将完成。但问题是,即使我的方法是异步的,我的UI也会被阻止。
我的代码是
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
Task<WriteableBitmap> transparentbordertemp = Render("tilenew.png");
Task<WriteableBitmap> transparentbitmaptemp = Render("greenpattern.png");
if (GameSettingData.mainimageselected == null)
{
bitmapimagemain = await Render("puzzleimage.png");
bitmapimagemain = bitmapimagemain.Resize(550, 550, WriteableBitmapExtensions.Interpolation.Bilinear);
puzzleimage.Source = bitmapimagemain;
GameSettingData.mainimageselected = bitmapimagemain;
}
else
{
bitmapimagemain = GameSettingData.mainimageselected;
bitmapimagemain = bitmapimagemain.Resize(550, 550, WriteableBitmapExtensions.Interpolation.Bilinear);
puzzleimage.Source = bitmapimagemain;
}
height = (puzzleimage.Parent as Grid).ActualHeight + 15;
width = (puzzleimage.Parent as Grid).ActualWidth - 25;
double startx = 0;
double starty = 0;
heightpiece = 546 / numberofrows;
widhpiece = 550 / numberofrows;
animationgrid.Height = heightpiece + 100;
animationgrid.Width = widhpiece + 100;
Rect rectangle;
int counter = 0;
WriteableBitmap transparentborder = new WriteableBitmap(330, 330);
finalbitmapimage = bitmapimagemain;
finalbitmapimage = finalbitmapimage.Resize(330 * numberofrows, 330 * numberofrows, WriteableBitmapExtensions.Interpolation.Bilinear);
int blithelpx = 0;
int blithelpy = 0;
for (int i = 0; i < numberofrows; i++)
{
for (int j = 0; j < numberofrows; j++)
{
imagepeices = new Image();
imagepeices.Height = bitmapimagemain.PixelHeight / (2 * numberofrows);
imagepeices.Width = bitmapimagemain.PixelWidth / (2 * numberofrows);
imagepeices.Stretch = Stretch.Uniform;
counter++;
imagepeices.Tag = counter;
startx = j * bitmapimagemain.PixelWidth / numberofrows;
starty = i * bitmapimagemain.PixelHeight / numberofrows;
rectangle = new Rect(startx, starty, bitmapimagemain.PixelWidth / numberofrows, bitmapimagemain.PixelHeight / numberofrows);
WriteableBitmap bitmapimagemain1 = bitmapimagemain.Crop(rectangle).Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
// transparentborder = await Render("tilenew.png");
transparentborder = await transparentbordertemp;
transparentborder = transparentborder.Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
final = new WriteableBitmap(transparentborder.PixelWidth, transparentborder.PixelHeight);
final.Blit(new Rect(0, 0, (int)bitmapimagemain1.PixelWidth, (int)bitmapimagemain1.PixelHeight), bitmapimagemain1, new Rect(0, 0, (int)bitmapimagemain1.PixelWidth, (int)bitmapimagemain1.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
final.Blit(new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), transparentborder, new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
final = final.Resize((int)widhpiece, (int)widhpiece, WriteableBitmapExtensions.Interpolation.Bilinear);
imagepeices.Source = final;
imagepeices.PointerPressed += ImagePointerPressed;
imagepeices.PointerReleased += ImagePointerReleased;
imagepeices.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
imagepeices.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
listimages.Add(imagepeices);
bitmaplist.Add(bitmapimagemain.Crop(rectangle));
margin += 20;
gridpieces.Children.Add(imagepeices);
finalbitmapimage.Blit(new Rect(blithelpx, blithelpy, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), transparentborder, new Rect(0, 0, (int)transparentborder.PixelWidth, (int)transparentborder.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive);
blithelpx += 330;
}
blithelpx = 0;
blithelpy += 330;
}
foreach (Image item in listimages)
{
var a = (item.Parent as Grid).ActualHeight;
var b = (item.Parent as Grid).ActualWidth;
var x = rand.Next(50, 250);
var y = rand.Next(100, 400);
transformpoints.Add(new Point(x, y));
TranslateTransform posTransform = new TranslateTransform();
posTransform.X = x;
posTransform.Y = y;
item.RenderTransform = posTransform;
}
newgrid = new Grid();
RowDefinition[] rows = new RowDefinition[numberofrows];
ColumnDefinition[] columns = new ColumnDefinition[numberofrows];
for (int q = 0; q < numberofrows; q++)
{
columns[q] = new ColumnDefinition();
columns[q].Width = new GridLength(1, GridUnitType.Star);
newgrid.ColumnDefinitions.Add(columns[q]);
}
for (int t = 0; t < numberofrows; t++)
{
rows[t] = new RowDefinition();
rows[t].Height = new GridLength(1, GridUnitType.Star);
newgrid.RowDefinitions.Add(rows[t]);
}
int counter1 = 0;
startx = 0;
starty = 0;
counter = 0;
for (int p = 0; p < numberofrows; p++)
{
for (int u = 0; u < numberofrows; u++)
{
counter1++;
Grid qwe = new Grid() { Name = "asd" + counter1.ToString(), Tag = counter1, Margin = new Thickness(0, 0, 0, 0) };
Image transparentimage = new Image();
transparentimage.Stretch = Stretch.Uniform;
WriteableBitmap transparentbitmap = await transparentbitmaptemp;
transparentbitmap = transparentbitmap.Resize(330, 330, WriteableBitmapExtensions.Interpolation.Bilinear);
Image imageblur = new Image();
imageblur.Stretch = Stretch.Uniform;
imageblur.Source = transparentbitmap;
imageblur.Height = transparentbitmap.PixelHeight;
imageblur.Width = transparentbitmap.PixelWidth;
if (u == 0)
{
imageblur.Margin = new Thickness(10, 0, -20, 1);
}
else if (u == numberofrows - 1)
{
imageblur.Margin = new Thickness(0, 0, 0, 1);
}
else
{
if (u == 1)
{
imageblur.Margin = new Thickness(0, 0, -13, 1);
}
else
{
imageblur.Margin = new Thickness(-13, 0, -13, 1);
}
}
transparentimage.Source = final;
qwe.Height = heightpiece;
qwe.Width = widhpiece;
qwe.Children.Add(imageblur);
qwe.Opacity = .6;
Grid.SetColumn(qwe, u);
Grid.SetRow(qwe, p);
newgrid.Children.Add(qwe);
}
}
newgrid.Margin = new Thickness(10, 0, 10, 0);
imagegrid.Children.Add(newgrid);
puzzleimage.Source = finalbitmapimage;
disabletopbar = 0;
initialcountdowngrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
imagegrid.Opacity = 1;
gridpieces.Opacity = 1;
dispatchertimer.Start();
}
我的渲染方法是
private async Task<WriteableBitmap> Render(string filename)
{
var Assets = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("JumblerGame");
var folder = await Assets.GetFolderAsync("Images");
StorageFile file1 = await folder.GetFileAsync(filename);
BitmapImage i1 = new BitmapImage();
using (IRandomAccessStream strm = await file1.OpenReadAsync())
{
i1.SetSource(strm);
}
WriteableBitmap img1 = new WriteableBitmap(i1.PixelWidth, i1.PixelHeight);
using (IRandomAccessStream strm = await file1.OpenReadAsync())
{
img1.SetSource(strm);
}
return img1;
}
在我的导航中我已经在ui上启动了一个计时器但是计时器没有启动,直到这个async方法被执行。
我不明白为什么......
答案 0 :(得分:0)
PageLoaded中包含所有for循环和图像处理的所有代码都在UI线程上执行,这可能是阻止调度程序执行时间的原因。
您可以在for循环中添加await Task.Delay(10);
以释放一些UI线程。您还可以使用await Task.Run(()=>{ ....});
在WriteableBitmap上执行不需要在UI线程上执行的图像处理