带有实时项目面板的PhotoLab示例 [英] PhotoLab sample with live items panel

查看:51
本文介绍了带有实时项目面板的PhotoLab示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试刷新PhotoLab示例应用的项目面板,当新图片添加到该文件夹​​时...我已经添加了  query.ContentsChanged事件但当事件触发并调用GetItemsAsync()时,文件夹中的所有文件都会再次添加到面板中...
如何只添加新文件?

I am trying to refresh PhotoLab sample app's items panel when a new picture added to the folder... I've added query.ContentsChanged event but when the event fired and called GetItemsAsync(), all the files in the folder added to the panel again... How can I add only the new ones?

问候

Gaye

推荐答案

嗨  Gaye_Turkalp,

Hi  Gaye_Turkalp,

GetItemsAsync方法会将所有符合条件的图片添加到 的ObservableCollection< ImageFileInfo>名单。当您添加query.ContentsChanged事件并调用GetItemsAsync()时,您需要添加代码以检查GetItemsAsync
方法中的重复图像。

The GetItemsAsync method will add all eligible pictures into the  ObservableCollection<ImageFileInfo> list. When you added query.ContentsChanged event and called GetItemsAsync(), you need to add the code to check the duplicate images in the GetItemsAsync method.

您可以尝试以下代码避免将文件夹中的所有文件添加到面板。

You can try the following code to avoid add all files in the folder to the panel.

     private async Task GetItemsAsync()
        {
            // See "Using a stream source to show images from the Pictures library".
            // This code is modified to get images from the app folder.

            // Get the app folder where the images are stored.
            StorageFolder appInstalledFolder = Package.Current.InstalledLocation;
            StorageFolder assets = await appInstalledFolder.GetFolderAsync("Assets\\Samples");

            // Get and process files in folder
            IReadOnlyList<StorageFile> fileList = await assets.GetFilesAsync();
            foreach (StorageFile file in fileList)
            {
                // Limit to only png or jpg files.
                if (file.FileType == ".jpg" || file.ContentType == "image/jpeg")
                {
                    if (existsinAssets(file) == false)
                    {
                        Images.Add(await LoadImageInfo(file));
                    }
                }
            }
        }

        private  bool existsinAssets (StorageFile file)
        {
            bool result = false;
            try
            {
                foreach(var ar in Images)
                {
                    if(ar.ImageName == file.Name)
                    {
                        result = true;
                        break;
                    }
                }
            }
            catch
            {
                result = false;
            }
            return result; 
        }


       public async static Task<ImageFileInfo> LoadImageInfo(StorageFile file)
        {
            // Open a stream for the selected file.
            // The 'using' block ensures the stream is disposed
            // after the image is loaded.
            using (IRandomAccessStream fileStream = await file.OpenReadAsync())
            {
                // Create a bitmap to be the image source.
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(fileStream);

                var properties = await file.Properties.GetImagePropertiesAsync();
                ImageFileInfo info = new ImageFileInfo(
                    properties, file, bitmapImage,
                    file.Name, file.DisplayType);
                return info;
            }
        }

最好的问候,

Yong Lu


这篇关于带有实时项目面板的PhotoLab示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆