如何刷新GridView内容?

时间:2018-08-30 11:52:48

标签: c# gridview uwp

我的应用程序中有一个GridView,基本上包含以下图像:

<GridView x:Name="ImageGridView">
    <Image x:Name="Image_1"/>
    <Image x:Name="Image_2"/>
    <Image x:Name="Image_3"/>
</GridView>

将其隐藏在代码后面,以加载图像:

public void SetImages()
{
  StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
  Image_1.Source= new BitmapImage(new Uri(localFolder.Path + "/Image_1.png"));
  Image_1.Source= new BitmapImage(new Uri(localFolder.Path + "/Image_2.png"));
  Image_1.Source= new BitmapImage(new Uri(localFolder.Path + "/Image_3.png"));
}

GridView可以很好地显示图像。但是,我也有一个Class,可以根据现有的值更改这些Picture的颜色,以使其变得更休闲:

Changing the Images Colors

这意味着图像源不会更改,并且这些图像的路径也将保持原样。唯一改变的是图像中显示的图像中的颜色。

但是,当完成变色任务并且在App的LocalState文件夹中更新了图像时,在GridView图像中没有更新它们。

Ithe Images are not updated in the UI

我也尝试过使用相关图像创建一个可观察的集合。将GridView绑定到此集合,并在每次颜色更新时包括以下内容:

GridView.ItemsSource = null;
GridView.ItemsSource = ImagesCollection;

它可以通过更改某些图像而不能更改其他图像来工作。这是随机发生的。如果有办法在执行第二个之前等待第一个,那么可能会解决!

问题是:我如何要求GridView重新加载其内容?更新其内容?清除缓存并重新获取图像?

3 个答案:

答案 0 :(得分:0)

将此代码放入方法中

public void SetImages()
{
    StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    Image_1.Source= new BitmapImage(new Uri(localFolder.Path + "/Image_1.png"));
    Image_2.Source= new BitmapImage(new Uri(localFolder.Path + "/Image_2.png"));
    Image_3.Source= new BitmapImage(new Uri(localFolder.Path + "/Image_3.png"));
}

在开始修改图像时调用一次此方法。

答案 1 :(得分:0)

不要害怕使用MVVM。在您的xaml中,将图像源绑定到视图模型中的属性。

XAML:

function getQuotations() {
  return fetch('http://localhost:8080')
    .then(response => response.json())
    .then(alertContents)
    .catch(console.error);
}

function alertContents(contents) {
  console.log(contents);
  alert(`"${contents.Phrase}" ~${contents.Author}`);
}

ViewModel:

<GridView>
    <Image Source="{Binding ImageSource1}"/>
    <Image Source="{Binding ImageSource2}"/>
</GridView>

答案 2 :(得分:0)

好吧,我发现了一种间接的肮脏方式。更新GridView中的图像。 我首先清除了GridView绑定到的ObservableCollection:

ThumnailsCollection.Clear();

导航到缩略图保存到的文件夹。每种类型的缩略图都分组在一个文件夹中。每次更改颜色(不存在其缩略图的位置)时,都会生成新的缩略图。否则,它会回忆起已有的东西:

StorageFolder localFolder =
    Windows.Storage.ApplicationData.Current.LocalFolder;

//Check if the main folder where the images are saved exist 

IStorageItem storageItem = await localFolder.TryGetItemAsync("Thumnails");
if (storageItem != null)
  {
    //If it does, look into the subfolders. each image group of the same
    //type and different colors are saved to tier own folder

    StorageFolder ImagesFolder =
        await localFolder.GetFolderAsync("Thumnails");
    var queryOption =
        new QueryOptions { FolderDepth = FolderDepth.Deep };
    var flatWallpaperSubFolders =
        await ImagesFolder .CreateFolderQueryWithOptions(queryOption).GetFoldersAsync();
    foreach (StorageFolder subFolder in subFolders)
      {
        IStorageItem thumnail =
            await subFolder.TryGetItemAsync($"{AlphaColorID} - {BetaColorID}.png");
        if (thumnail != null)
          {
             //in each of the folders, look if the needed image
             //exist or not
             //If it does, add it to the ObservableCollection
             BitmapImage icon =
                 new BitmapImage(new Uri(subFolder.Path + $"/{AlphaColorID} - {BetaColorID}.png"));
             ThumnailsCollection.Add(new ThumnailsBitmap(icon));
          }
        else
          {
            //If the Image with the requested colors does not exist
            //Render a new image with the needed colors
            //the rendered image is added to the same subfolder with the other
            // similar images
            await ApplyColor(new Uri($"ms-appx:///Images/{subFolder.Name}-Thumnail.png"), $"{AlphaColor} - {BetaColor}.png", $"{subFolder.Name}");
            BitmapImage icon =
                new BitmapImage(new Uri(flatWallpaperSubFolder.Path + $"/{AlphaColor} - {BetaColor}.png"));

            ThumnailsCollection.Add(new ThumnailsBitmap(icon));
          }
      }
  }
else
  {
    //when run for the first time, all folders and images are missing
    //through ApplyColor task, all Images are rendered and folders created
    await ApplyColor(new Uri("ms-appx:///Images/Number-1-Thumnail.png"), $"{AlphaColor} - {BetaColor}.png", "Number-1");

    await ApplyColor(new Uri("ms-appx:///ModernWallpaperGrayscale/Number-2-Thumnail.png"), $"{AlphaColor} - {BetaColor}.png", "Number-2");


    //All needed to do is to all of those Images to the
    //ObservableCollection
    StorageFolder ImagesFolder = await localFolder.GetFolderAsync("Thumnails");
    var queryOption =
        new QueryOptions { FolderDepth = FolderDepth.Deep };
    var flatWallpaperSubFolders =
        await ImagesFolder.CreateFolderQueryWithOptions(queryOption).GetFoldersAsync();
    foreach (StorageFolder subFolder in subFolders)
      {
        BitmapImage icon =
            new BitmapImage(new Uri(subFolder.Path + $"/{AlphaColor} - {BetaColor}.png"));
        ThumnailsCollection.Add(new ThumnailsBitmap(icon));
      }
  }