【问题标题】:Copy an image from Windows Phone installation folder to isolated sotorage将图像从 Windows Phone 安装文件夹复制到独立存储
【发布时间】:2013-12-07 21:27:07
【问题描述】:

假设我的应用安装文件夹中有一张图片。

例如"/Assets/Images/BackgroundImage.jpg"

知道如何将此图像保存到隔离的存储文件夹中吗? 例如"/Shared/ShellContent/BackgroundImage.jpg"

我必须使用 WriteableBitmap 来渲染它吗?

我想这不会那么难,但是,我太笨了,无法弄清楚。

【问题讨论】:

  • 渲染是什么意思?只是显示还是更高级?

标签: windows-phone-8


【解决方案1】:

回答您的第一个问题 - 如何将文件从安装文件夹复制到独立存储:

        var uri = new Uri("Assets\\Images\\BackgroundImage.jpg", UriKind.Relative);
        var sri = Application.GetResourceStream(uri);
        var data = sri.Stream;
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

        using (IsolatedStorageFileStream stream = storage.CreateFile("Shared\\ShellContent\\BackgroundImage.jpg"))
        {
           data.CopyTo(stream);
        }

你的第二个问题 - 如果你想将图片显示为背景:

 <Image Source="/Shared/ShellContent/BackgroundImage.jpg" />

当然,您需要根据您的布局分配适当的属性。此外,为了避免硬编码,您可以将 Source 绑定到任何变量并在运行时切换它

【讨论】:

  • 感谢您的回复,您的代码看起来很有希望,但是,当我尝试使用 storage.CopyFile 时,出现以下异常:System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted. 我错过了什么吗?
  • 不,IsolatedStorage 无法访问本地文件夹,所以异常是好的 - 我的错。修复和测试示例 - 它是使用 Stream 的解决方法。只要确保所有目录都创建为CreateFile 否则会抛出异常
  • 嗨,Nogard,但是,当图像确实复制到目标文件夹时,对图像的引用无法按预期工作,当我使用此语法 &lt;Image Source="/Shared/ShellContent/BackgroundImage.jpg" /&gt; 时,它只显示黑色图像反而。有什么想法吗?
  • 这样它会在安装文件夹中查找图片。如果您想从独立存储中获取它,您需要与代码隐藏或 ViewModel 绑定(取决于您的项目结构)。检查这个 sn-p:code.msdn.microsoft.com/CSWP8ImageFromIsolatedStora-8dcf8411 如果您需要详细说明,请指定如何设置图像源
  • 你完全正确!感谢您的链接,那里的代码示例就像一个魅力!当我获得足够的声望时,我会给你投票!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
相关资源
最近更新 更多