加载本地映像以用于 Windows 7 手机应用测试

本文关键字:手机 应用 测试 Windows 用于 映像 加载 | 更新日期: 2023-09-27 18:29:06

全部,我对Windows Phone很陌生,真的不知道该在哪里使用这个。我不想将一些测试图像加载到我的 Windows Phone 应用程序中(仅用于测试目的(。在我的机器上,我有一些 JPEG,我想将其加载到本身包含ImageCanvas中。我知道你可以像这样从本地存储加载

private void LoadFromLocalStorage(string imageFileName, string imageFolder)
{
    var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    if (!isoFile.DirectoryExists(imageFolder))
    {
        isoFile.CreateDirectory(imageFolder);
    }
    string filePath = Path.Combine(imageFolder, imageFileName);
    using (var imageStream = isoFile.OpenFile(
        filePath, FileMode.Open, FileAccess.Read))
    {
        var imageSource = PictureDecoder.DecodeJpeg(imageStream);
        image.Source = imageSource;
    }
}

但是如何将图像从我的机器获取到独立存储中呢?对不起,我是一个真正的菜鸟:[。

加载本地映像以用于 Windows 7 手机应用测试

最简单的方法是使用某种隔离的存储浏览器:

http://wptools.codeplex.com/

假设您在应用程序目录中的 images 文件夹中有图像。确保将"生成操作"设置为"内容">

public class Storage
{
    public void SaveFilesToIsoStore()
    {
        //These files must match what is included in the application package,
        //or BinaryStream.Dispose below will throw an exception.
        string[] files = {
        "images/img1.jpg", "images/img2.png"
    };
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        if (files.Length > 0)
        {
            foreach (string f in files)
            {
                StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
                using (BinaryReader br = new BinaryReader(sr.Stream))
                {
                    byte[] data = br.ReadBytes((int)sr.Stream.Length);
                    SaveToIsoStore(f, data);
                }
            }
        }
    }
    private void SaveToIsoStore(string fileName, byte[] data)
    {
        string strBaseDir = string.Empty;
        string delimStr = "/";
        char[] delimiter = delimStr.ToCharArray();
        string[] dirsPath = fileName.Split(delimiter);
        //Get the IsoStore.
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        //Re-create the directory structure.
        for (int i = 0; i < dirsPath.Length - 1; i++)
        {
            strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
            isoStore.CreateDirectory(strBaseDir);
        }
        //Remove the existing file.
        if (isoStore.FileExists(fileName))
        {
            isoStore.DeleteFile(fileName);
        }
        //Write the file.
        using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
        {
            bw.Write(data);
            bw.Close();
        }
    }
}

在应用启动中添加以下代码

 private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        Storage sg = new Storage();
        sg.SaveFilesToIsoStore();
    }