Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在wpf后面创建控件异步代码_C#_Wpf_Asynchronous_Async Await - Fatal编程技术网

C# 在wpf后面创建控件异步代码

C# 在wpf后面创建控件异步代码,c#,wpf,asynchronous,async-await,C#,Wpf,Asynchronous,Async Await,我正在创建一个照片库应用程序,我想按类别显示图片 在正常模式下,一切正常,图像显示 但是当图像的数量变大(300)时,程序将挂起,并且需要很长时间才能显示 所以我想使用异步和显示图像 我使用了下面的代码,但是什么都没有发生,图像也没有显示 int HandleFileAsync() { AllofItems.ForEachWithIndex((item, idx) => { var cv = new CoverViewItem(); var

我正在创建一个照片库应用程序,我想按类别显示图片

在正常模式下,一切正常,图像显示

但是当图像的数量变大(300)时,程序将挂起,并且需要很长时间才能显示

所以我想使用异步和显示图像

我使用了下面的代码,但是什么都没有发生,图像也没有显示

int HandleFileAsync()
{
    AllofItems.ForEachWithIndex((item, idx) =>
    {

        var cv = new CoverViewItem();
        var contentImg = new Image();
        contentImg.Stretch = Stretch.UniformToFill;
        contentImg.Source = new BitmapImage(new Uri(item, UriKind.Absolute));
        var img = new Image();
        img.Source = new BitmapImage(new Uri(item, UriKind.Absolute));

        //-< source >- 
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = new Uri(item, UriKind.Absolute);
        //< thumbnail > 
        src.DecodePixelWidth = 160;
        src.CacheOption = BitmapCacheOption.OnLoad;
        //</ thumbnail > 

        src.EndInit();
        img.Source = src;
        //-</ source >- 

        img.Stretch = Stretch.Uniform;
        img.Height = 160;

        cv.Header = img;
        cv.Tag = item;
        cv.Content = contentImg;
        cv.Selected += Cv_Selected;
        cv.Deselected += Cv_Deselected;
        Dispatcher.Invoke(() =>
        {
          cover.Items.Add(cv);
        });
    });
    return AllofItems.Count();
}

async void Example()
{
    // This method runs asynchronously.
    int t = await Task.Run(() => HandleFileAsync());
    Console.WriteLine("Compute: " + t);
}

private void Listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    AllofItems = GetFileList(@"E:\DL\newArtWork\Art\" + listbox.SelectedItem).ToArray();
    cover.Items.Clear();

    Example();
}
int-HandleFileAsync()
{
AllofItems.ForEachWithIndex((项目,idx)=>
{
var cv=新的CoverViewItem();
var contentImg=新图像();
contentImg.Stretch=Stretch.UniformToFill;
contentImg.Source=新的位图图像(新的Uri(item,UriKind.Absolute));
var img=新图像();
img.Source=新的位图图像(新的Uri(item,UriKind.Absolute));
//--
BitmapImage src=新的BitmapImage();
src.BeginInit();
src.UriSource=新Uri(项,UriKind.Absolute);
//<缩略图>
src.DecodePixelWidth=160;
src.CacheOption=BitmapCacheOption.OnLoad;
// 
src.EndInit();
img.Source=src;
//-- 
img.拉伸=拉伸均匀;
内模高度=160;
cv.Header=img;
cv.Tag=项目;
cv.Content=contentImg;
选定的cv+=选定的cv_;
取消选择的cv+=取消选择的cv_;
Dispatcher.Invoke(()=>
{
封面。项目。添加(cv);
});
});
返回AllofItems.Count();
}
异步void示例()
{
//此方法异步运行。
int t=wait Task.Run(()=>HandleFileAsync());
控制台写入线(“计算:+t”);
}
私有无效列表框\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
AllofItems=GetFileList(@“E:\DL\newArtWork\Art\”+listbox.SelectedItem.ToArray();
cover.Items.Clear();
示例();
}
将一个
try{}catch()
放在循环的主体上并检查,肯定会有接受

async void
不适合异常处理,它是一种反模式

必须在GUI线程上创建图像。由于您使用了
Task.Run
,因此您可以从GUI线程转到.NET线程池。创建图像时必须出现异常


在后台线程/NET线程池中加载数据,并使用
Dispatcher.Invoke()

在xaml中创建GUI对象:

<ItemsControl ItemsSource="{Binding FilesList}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <Image Source="{Binding IsAsync=True}"/>
      <DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

这将使每个图像加载到背景线程中,并在完成后显示。如果要显示大量图像,可以添加ValueConverter以缩小图像的比例


关键是,
{Binding IsAsync=True}
发挥了您所需的所有魔力,即使使用转换器,它仍然是异步的

您使用调度程序的方式可能是个问题,请尝试像这样重写您的代码