【问题标题】:How to Use IsolatedStorage to Set Lock Screen Background in Windows Phone 8如何在 Windows Phone 8 中使用 IsolatedStorage 设置锁屏背景
【发布时间】:2013-08-20 04:24:32
【问题描述】:

我希望能够使用我的 IsolatedStorage 中的图像来修改锁定屏幕背景,但我无法获取 IsolatedStorage 文件路径的正确语法来设置锁定屏幕背景。

http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206968(v=vs.105).aspx 之后,一旦从名为Recent 的列表中选择了图像,我将在按钮单击事件中调用LockHelper 方法(该列表已从PictureRepository.cs 填充,从IsolatedStorage 加载图像)

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        capturedPicture = (sender as LongListSelector).SelectedItem as CapturedPicture;

        if (capturedPicture != null)
        {
            //filename is the name of the image in IsolatedStorage
            fileName = capturedPicture.FileName;
        }
    }

void setAsLockScreenMenuItem_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(fileName)) 
        {
            //PictureRepository.IsolatedStoragePath is a string = "Pictures"                
            LockHelper("isostore:/" + PictureRepository.IsolatedStoragePath + "/" + fileName, false);  //results in FileNotFoundException
            LockHelper(PictureRepository.IsolatedStoragePath + "/" + fileName, false);  //results in ArgumentException
        }
        else
        {
            MessageBoxResult result = MessageBox.Show("You must select an image to set it as your lock screen.", "Notice", MessageBoxButton.OK);
            if (result == MessageBoxResult.OK)
            {
                return;
            }
        }
    }

一旦调用LockHelper,事件就会继续

private async void LockHelper(string filePathOfTheImage, bool isAppResource)
    {
        try
        {
            var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
            if (!isProvider)
            {
                // If you're not the provider, this call will prompt the user for permission.
                // Calling RequestAccessAsync from a background agent is not allowed.
                var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync();

                // Only do further work if the access was granted.
                isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
            }

            if (isProvider)
            {
                // At this stage, the app is the active lock screen background provider.

                // The following code example shows the new URI schema.
                // ms-appdata points to the root of the local app data folder.
                // ms-appx points to the Local app install folder, to reference resources bundled in the XAP package.
                var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
                var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);

                //The Error Occurs Here!
                // Set the lock screen background image.
                Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);

                // Get the URI of the lock screen background image.
                var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri();
                System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString());
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("Cannot update your lock screen background at this time.", "Notice", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    return;
                }
            }
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

错误发生在Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); 方法中的LockHelper 上。它是 FileNotFoundException 提及 The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B) 或 ArgumentException。我引用的所有地方都说使用IsolatedStorage Uri 应该是ms-appdata:///local/ 然后是IsolatedStorage 文件路径(在我的情况下为Pictures/imagename.jpg),我相信我的错误在于Uri 路径,但我不确定的正确语法?如果不是这个,还有其他想法吗?

【问题讨论】:

  • 您是否尝试跳过架构前缀之一(“isostore”或“ms-app...”)?也许不能同时应用它们。
  • 你一定是在我更新编辑之前发现了我。我确实删除了 isostore 前缀并以 ArgumentException 告终。我会尝试删除 ms-app 看看会发生什么..
  • 我相信它确实与架构前缀有关,尽管我不确定修复是什么?这被证明很有趣social.msdn.microsoft.com/Forums/wpapps/en-US/…。我确信我的图像存在于 IsolatedStorage 中,因为它正在加载到我的视图中。

标签: c# windows-phone-8 isolatedstorage lockscreen


【解决方案1】:

这对我有用:

const string filePathOfTheImage = "/Shared/ShellContent/shot2.jpg"; //This is where my image is in isostore

var uri = new Uri("ms-appdata:///local" + filePathOfTheImage, UriKind.Absolute);

另外,我使用WP Power Tools 来跟踪我的应用存储空间。希望这会有所帮助。

【讨论】:

  • 我确实使用了类似的东西。我认为我的问题是我保存图像的位置。我在不久前的一些代码中找到了一个示例,当我使用它时,它在字符串之前使用@ 创建了IsolatedStorage 的路径。知道为什么吗?
  • 我无法真正指出“@”背后的技术方面,但我还必须在我的 WP7 应用程序中使用带有 isostore 的“@”。但有了 WP8,没有它也能正常工作。
【解决方案2】:

nokia developer 试试这个。希望你能找到解决办法。

【讨论】:

  • 谢谢,这是我引用的网站之一。我的问题是,如果我从隔离存储中提取图像,并且我知道它们所在的文件夹并具有文件名,那么 Uri 的正确语法是什么 Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);。似乎在将我的变量传递给LockScreenChange(string filePathOfTheImage, bool isAppResource) 之后,没有创建正确的Uri
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多