1

我是异步世界的新手,我开始为 Windows 商店应用程序开发一些东西。我的问题是我需要等待 filepicker 的异步方法结束才能做其他事情。

所以我尝试搜索:“运行同步和异步方法 c#”,并在 Stephen Cleary 博客上结束。我尝试使用 Nito AsyncEx 来解决我的问题,但它不起作用,因为当我调用以下方法时它不会停止。

private List<Templates> classModel = new List<Templates>();
private void btnLoadClassExercises_Click(object sender, RoutedEventArgs e)
{
   AsyncContext.Run(() => loadFile());
   // do some stuff with classModel
}

private async void loadFile()
{
   FileOpenPicker openPicker = new FileOpenPicker();
   openPicker.FileTypeFilter.Add(".xml");
   var file = await openPicker.PickSingleFileAsync();
   classModel = xml.getControlClass(file);
 }

在我的搜索过程中,我发现了一个名为 AsyncInLine 的类,它帮助我解决了一些类似的东西。但是在这种特殊情况下,我也没有工作。(http://social.msdn.microsoft.com/Forums/en-US/async/thread/163ef755-ff7b-4ea5-b226-bbe8ef5f4796

有人能帮我吗?

编辑#1:

    //When i click on this button, it loads the file and all canvas are drawn on canvasSegment.
    private async void btnLoadClassExercises_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (classModel != null)
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.FileTypeFilter.Add(".xml");
                var file = await openPicker.PickSingleFileAsync();
                classModel = xml.getControlClass(file);

                auxCV = 0;
                if (editClass)
                    CanvasSegment.Children.Clear();
                foreach (Templates temp in classModel)
                    createCanvasSegment(temp);
                editClass = true;
            }
        }
        catch { }
    }

    // When i want to del some canvas, i put the removeCV = true and click on a canvas
    private void btnDelExercise_Click(object sender, RoutedEventArgs e)
    {
        classEdit = classModel; //To debuf if the classModel.Count = 0
        removeCV = true;
    }

    //When i click on a canvas, if removeCV = false, i draw the canvas on the main canvas(canvasClass), otherwise, i delete it. The problem is here. My modelClass.count here is = 0.  
    private void cv_PointerPressed(Templates temp, Canvas cv)
    {
        if (!removeCV)
        {
            foreach (StackPanel sp in CanvasSegment.Children)
                foreach (Canvas c in sp.Children)
                    c.Opacity = 1;

            cv.Opacity = 0.8;
            canvasClass.Children.Clear();

            for (int i = 0; i < temp.Shapes.Count; i++)
            {
                //Do some stuff with points and draws on CanvasClass later
                rh.renderModel(new InkManager(), canvasClass, temp.Shapes[i], true, false);
            }
        }
        else
        {
            for (int i = 0; i <= classModel.Count; i++)
                if (classModel[i] == temp)
                {
                    classModel.RemoveAt(i);
                    break;
                }

            CanvasSegment.Children.Clear();
            auxCV = 0;
            foreach (Templates tempModel in classModel)
                createCanvasSegment(tempModel);
            removeCV = false;
        }
    }

    //I create a mini canvas foreach template in file.
    private void createCanvasSegment(Templates temp)
    {
        Thickness thk = new Thickness();
        thk.Right = 5;
        thk.Bottom = 5;
        if (temp != null || temp.Shapes != null)
        {
            if (auxCV == 0)
            {
                spcv = new StackPanel();
                spcv.Orientation = Orientation.Horizontal;
                spcv.Width = 540;
            }
            Canvas cv = new Canvas()
            {
                Background = new SolidColorBrush(Colors.Wheat),
                Name = temp.Template,
                Height = 73.125,
                Width = 130,
                Margin = thk,
            };

            cv.PointerPressed += (s, e) => cv_PointerPressed(temp, cv);
            foreach (ShapePoints tempshapePoints in temp.Shapes)
            {
                ShapePoints tempS = tempShapePoints;
                if (!removeCV)
                {
                    //Do some stuff with points
                }
                rh.renderModel(new InkManager(), cv, tempS, true, false);
            }

            auxCV++;
                spcv.Children.Add(cv);
            if (auxCV == 1)
                CanvasSegment.Children.Add(spcv);
            else if (auxCV == 4)
                auxCV = 0;
        }
    }
4

1 回答 1

1

由于这已经在 UI 线程的上下文中,因此不需要AsyncContext.Run- 只需使用:

private async void btnLoadClassExercises_Click(object sender, RoutedEventArgs e)
{
   await loadFile());
   // do some stuff with classModel
}

// Make this task returning (or even return the list instead of setting a local)
private async Task loadFile()
{
   FileOpenPicker openPicker = new FileOpenPicker();
   openPicker.FileTypeFilter.Add(".xml");
   var file = await openPicker.PickSingleFileAsync();
   classModel = xml.getControlClass(file);
 }
于 2013-04-12T15:23:25.483 回答