0

我正在开发一个应用程序,我想在后台做一些工作,在 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 上启动了一个计时器,但在执行此异步方法之前,计时器不会启动。

我不明白为什么...

4

1 回答 1

0

PageLoaded 中所有 for 循环和图像处理的所有代码都在 UI 线程上执行,因此这可能是阻止调度程序执行时间的原因。
您可以await Task.Delay(10);在 for 循环中添加以释放一些 UI 线程。您还可以await Task.Run(()=>{ ....});用于在不需要在 UI 线程上执行的 WriteableBitmap 上执行图像处理

于 2013-09-12T12:53:57.283 回答