【问题标题】:Windows Phone 8 Lockscreen doesn't set when LockScreen.SetImageUri() is called调用 LockScreen.SetImageUri() 时未设置 Windows Phone 8 锁屏
【发布时间】:2013-12-06 23:34:33
【问题描述】:

首先,我已经在我的应用清单中添加了扩展,并且我的锁屏图像正在更改,我的问题源于它多次更改。

在应用程序中,我还可以让用户点击按钮,从互联网上抓取图像,将其保存到存储中,然后将其设置为锁屏。有人第一次点击按钮时,它会(大部分时间)正确设置,虽然我的问题是当再次点击按钮时,它经常会选择不设置,从而要求用户必须多次按下按钮次希望它设置正确。

这是我的按钮按下代码

    private async void Set_As_Lockscreen_Click(object sender, EventArgs e)
    {
        Debug.WriteLine("Set_As_Lockscreen_Click(): Entering");
        try
        {
            if (!contentLoaded) { return; }
            if (Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication)
            {
                Debug.WriteLine("Awaiting Lockscreen_Helper.SetImage()");
                await Lockscreen_Helper.SetImage(new Uri(libraryObject.anime.cover_image, UriKind.Absolute));
            }
        }
        catch (Exception) { Debug.WriteLine("Set_As_Locksceen_Click(): Failed"); }
        Debug.WriteLine("Set_As_Lockscreen_Click(): Exiting");
    }

这是我的锁屏助手类

public class Lockscreen_Helper
{
    private const string BackgroundRoot = ""; 
    private const string LOCKSCREEN_IMAGE = "lockscreen.jpg";
    private static int count;

    public static bool DeleteLockscreenImage()
    {
        Storage.DeleteFile(LOCKSCREEN_IMAGE);
        return true;
    }

    public static async Task SetImage(Uri uri)
    {
        //First Delete Old image
        DeleteLockscreenImage();

        Debug.WriteLine(uri.OriginalString);

        string fileName = uri.Segments[uri.Segments.Length - 1];
        string imageName = BackgroundRoot + fileName;

        using (IsolatedStorageFile storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = storageFolder.CreateFile(LOCKSCREEN_IMAGE))
            {
                Debug.WriteLine("Opening Client");
                HttpClient client = new HttpClient();

                Debug.WriteLine("Grabbing File");
                byte[] hummingbirdResult = await client.GetByteArrayAsync(uri);
                Storage.isSavingComplete = false;

                Debug.WriteLine("Writing File");

                await stream.WriteAsync(hummingbirdResult, 0, hummingbirdResult.Length);
                Storage.isSavingComplete = true;

                Debug.WriteLine("File Written");
            }
        }

        await SetLockScreen();
    }

    public static async Task SetLockScreen()
    {
        bool hasAccessForLockScreen = LockScreenManager.IsProvidedByCurrentApplication;

        if (!hasAccessForLockScreen)
        {
            var accessRequested = await LockScreenManager.RequestAccessAsync();
            hasAccessForLockScreen = (accessRequested == LockScreenRequestResult.Granted);

            Consts.HasAccessForLockscreen = hasAccessForLockScreen;
        }

        if (hasAccessForLockScreen)
        {
            // Maybe if I try setting it to another image then setting it
            // back to the downloaded image?

            //bool isAppResource = true;
            //string filePathOfTheImage = "Assets/defaultLockscreenBackground.png";
            //var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
            //var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);
            //LockScreen.SetImageUri(uri);

            // thread.sleep(2.0); // Try having it wait 2 seconds before setting again?

            Uri imgUri = new Uri("ms-appdata:///local/" + LOCKSCREEN_IMAGE, UriKind.Absolute);
            LockScreen.SetImageUri(imgUri);
            Debug.WriteLine("Lockscreen Image Set");
        }
    }
}

这是删除文件的存储代码

    public static bool DeleteFile(string fileName)
    {
        if (!DoesFileExist(fileName))
            return true;

        using (IsolatedStorageFile storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
        {
            storageFolder.DeleteFile(fileName);
            return true;
        }
    }

**此代码只是MSDNChannel-9的代码的略微修改版本

需要注意的是,图像正在正确删除、下载和保存,我已经通过独立存储资源管理器进行了检查。我什至尝试将另一个图像设置为锁屏,然后再设置我的下载图像,以防文件名相似性搞砸了。我什至尝试让它等待 2 秒钟,然后再尝试重新设置。两者都不起作用。同时检查我的输出窗口,它正在输入方法。

我已经研究了几天了,现在试图弄清楚这一点,我一直在苦苦思索。有没有人可以提供帮助或至少提供一些建议?

谢谢。

【问题讨论】:

  • 尝试在 Visual Studio 中使用断点进行调试。在 Set_As_Lockscreen_Click 方法中下一个断点(F9),然后使用 Step Over(F10) 逐行调试你的程序,看看为什么它“选择不设置”。

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


【解决方案1】:

当我在我的应用程序上实现此功能时,我必须解决以下问题:

  • 在 WmAppManiest.xml 支持中启用此选项以让应用程序成为后台提供程序。

  • 请求应用程序的权限,以便设置为后台提供程序。

  • 设置图片

    • 要使用您在应用中提供的图像,请使用 ms-appx:///

      Uri imageUri = new Uri("ms-appx:///background1.png", UriKind.RelativeOrAbsolute); LockScreen.SetImageUri(imageUri);

    • 要使用存储在本地文件夹中的图像,请使用 ms-appdata:///local/shared/shellcontent 必须在 /shared/shellcontent 子文件夹中或之下

      Uri imageUri = new Uri("ms-appdata:///local/shared/shellcontent/background2.png", UriKind.RelativeOrAbsolute);

      LockScreen.SetImageUri(imageUri);

基于(模块 7)http://rasor.wordpress.com/2012/11/30/wp8-jump-start-course/

另外,就我而言,我不得不定期从互联网下载新的后台锁屏。有一次它无法工作,因为操作系统没有重新加载图像,即使文件已用新下载的图像进行了修改。解决方法是交替下载文件的名称。因此,它第一次使用: “ms-appdata:///local/shared/shellcontent/background1.png” 该应用第二次使用: “ms-appdata:///local/shared/shellcontent/background2.png” 下次它将使用 background1.png 重复循环。

药草

【讨论】:

  • 看来我的问题可能出在操作系统上。我注意到如果我将它设置为不同的文件名,它每次都可以工作。因此,我将按照您的建议将其切换为在两张图片之间交替。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多