Hello! I have a question: I want to download, being on the start page of my program, pictures from the Internet and, through a certain function, assign their collections.
After that, I turn from the start page of the program to the page with pictures, but there is some hangup of the program interface, as can be seen from the Dispatcher code, but I don’t. and I don't know what to do.
A piece of WPF, this is a “Tile” ratio of 5 x 6 images:
<ItemsControl Grid.Row="0" ItemsSource="{Binding RecentMedias}" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="5" Rows="6" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border Width="176" Height="247" Margin="20,20,0,0"> <Grid> <Image Source="{Binding Path=Images, IsAsync=True}"/> </Grid> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> C # chunk:
for (int i = 0; i < RecentMedias.Count(); i++) { int temp = i; Task.Factory.StartNew(() => { DownloadInst(RecentMedias[temp]); }); } public void DownloadInst(SelectedMedia m) { while (m.DownloadError) { try { System.Net.WebRequest request = System.Net.WebRequest.Create(m._Media.Images.LowResolution.Url); System.Net.WebResponse response = request.GetResponse(); var memoryStream = new MemoryStream(); using (System.IO.Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); } BitmapImage image = new BitmapImage(); image.BeginInit(); image.CreateOptions = BitmapCreateOptions.IgnoreColorProfile; image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = memoryStream; image.EndInit(); image.Freeze(); m.Images = image; m.DownloadError = false; } catch { Thread.Sleep(1000); App.Log.Write("Не удалось скачать инстаграмм"); } } }