使用Nito AsyncEx/AsyncInLine来引导异步filePicker

本文关键字:异步 filePicker AsyncInLine Nito AsyncEx 使用 | 更新日期: 2023-09-27 18:09:06

我是async world的新手,我开始为windows商店应用程序开发一些东西。我的问题是,我需要等待文件拾取器的异步方法结束做其他事情。

所以我试着搜索:"运行同步和异步方法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;
        }
    }

使用Nito AsyncEx/AsyncInLine来引导异步filePicker

由于这已经在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);
 }