簡體   English   中英

下載后未加載WinRT Image XAML

[英]WinRT Image XAML is not loaded after download

我正在為Win 8.1(WinRT)設計Windows應用商店。 該應用程序的頁面之一是通訊錄。 在某些情況下,該應用會接收推送通知。 其中之一可能是某位聯系人有新照片。 當應用處理推式通知時(不是推式通知的后台任務,它可以正常工作),它將從推式通知中指定的鏈接下載新圖像,並觸發一個事件,該事件應使用新圖像更新UI。 圖像存儲在LocalFolder(ApplicationData.Current.LocalFolder)中。 觸發事件后,將更新UI,但現在沒有用於相應聯系人的圖片。 重新啟動應用程序后,將顯示圖片。 在應用程序運行時,我還打開了Windows資源管理器,以查看文件是否已下載。 一件事是,在應用重新啟動(或停止)之前,新圖片沒有縮略圖。 我真的不知道為什么我需要重新啟動應用程序才能顯示圖片。 圖像很小,大約40-60KB。 通過重置綁定到引發PropertyChanged的屬性的ItemsSource來實現更新。 可以看出,由於短暫的閃爍而設置了ItemsSource,但是現在更新的項目沒有圖片了。

這是執行下載的代碼:

public async Task GetContactImage(String[] pushContent)
    {
        var folder = ApplicationData.Current.LocalFolder;
        var contactImagesfolder = await folder.CreateFolderAsync("ContactImages", CreationCollisionOption.OpenIfExists);

        HttpClient httpClient = new HttpClient();
        // Increase the max buffer size for the response so we don't get an exception with so many web sites
        httpClient.MaxResponseContentBufferSize = 256000;
        HttpResponseMessage response = await httpClient.GetAsync(pushContent[2]);

        StorageFile picture = await contactImagesfolder.CreateFileAsync(pushContent[2].Split('/').Last(), CreationCollisionOption.ReplaceExisting);
        IRandomAccessStream pictureStream = await picture.OpenAsync(FileAccessMode.ReadWrite);
        DataWriter writer = new DataWriter(pictureStream.GetOutputStreamAt(0));
        writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
        await writer.StoreAsync();
        writer.DetachStream();
        await pictureStream.FlushAsync();

        pictureStream.Dispose();
        writer.Dispose();

        Dal.Instance.UpdateContactImage(pushContent[1], pushContent[2]);
    }

各自的XAML,它是ListView(duh)的DataTemplate:

<Image Height="128" Width="128" Source="{Binding Converter={StaticResource contactImageConverter}}" />

我用來設置Source屬性的轉換器:

public object Convert(object value, Type targetType, object parameter, string language)
    {
        var contact = value as Contact;
        String imagePath = "ms-appx:///Assets/UnknownMan.png";
        if (contact != null)
        {
            if (contact.Image != null && contact.Image != "" && contact.Image != "Unknow_man.png")
            {
                imagePath = "ms-appdata:///local/ContactImages/" + contact.Image.Split('/').Last();                    
            }
        }

        return imagePath;
    }

非常感謝您的幫助!

原來,將文件保存到文件系統的方法會創建某種形式的句柄,這將使應用程序無法加載圖像。 我所做的是更改了保存文件的方法。

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFolder contactImagesfolder = await folder.CreateFolderAsync("ContactImages", CreationCollisionOption.OpenIfExists);
StorageFile picture = await contactImagesfolder.CreateFileAsync(imageURL.Split('/').Last(), CreationCollisionOption.FailIfExists);

HttpClient httpClient = new HttpClient();
// Increase the max buffer size for the response so we don't get an exception with so many web sites
httpClient.MaxResponseContentBufferSize = 256000;
HttpResponseMessage response = await httpClient.GetAsync(imageURL);

var responseByteArray = await response.Content.ReadAsByteArrayAsync();
await FileIO.WriteBytesAsync(picture, responseByteArray);

Dal.Instance.UpdateContactImage(contactID, imageURL);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM