首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用LockScreen.SetImageUri设置Windows phone8锁屏背景

如何使用LockScreen.SetImageUri设置Windows phone8锁屏背景
EN

Stack Overflow用户
提问于 2013-08-21 04:49:05
回答 2查看 3K关注 0票数 1

我在IsolatedStorage中有一个图像,我想以编程方式将其设置为设备锁定屏幕背景。我的问题是我无法获得LockScreen.SetImageUri所需的正确路径。从引用http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206968(v=vs.105).aspx可以明显看出,`“ms-appdata:/ local /”是本地镜像所需的前身。

代码语言:javascript
复制
var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);

我已经在我的应用程序IsolatedStorage中创建了一个名为Pictures的文件夹,其中保存了CameraCaptureTask中的jpg图像。我尝试了几种方法通过上面的方案访问此文件夹中的图像,但我总是在下一行收到ArgumentException

代码语言:javascript
复制
Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);

然而,在调试时,我看到了uri = "ms-appdata:///Local/Pictures/WP_20130812_001.jpg",这怎么会不正确呢?

我的实现如下

代码语言:javascript
复制
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 the IsolatedStorage folder named Pictures
        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;
        }
    }
}

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);

        // 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
    {
        MessageBox.Show("You said no, so I can't update your background.");
    }
}
catch (System.Exception ex)
{
    System.Diagnostics.Debug.WriteLine(ex.ToString());
}

}

如何将LockScreen.SetImageUri修改为正确的预期uri?

EN

回答 2

Stack Overflow用户

发布于 2013-08-23 04:33:26

为了从应用程序设置锁定屏幕图像,您可能希望将您的应用程序声明为锁定屏幕提供程序。您可以通过添加以下标记来修改WMAppManifest.xml文件来完成此操作:

代码语言:javascript
复制
<Extensions>
  <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
</Extensions>

检查清单文件中是否有此标记。

票数 1
EN

Stack Overflow用户

发布于 2013-11-20 00:07:34

我希望这能帮助你解决你的问题。

如果您的应用程序已安装,请确保它是背景图像提供程序。如果没有,则转到settings -> lock screen -> background并从列表中选择您的应用程序。

在编程方面:

1.在应用程序清单文件中声明应用程序的意图使用XML编辑器编辑WMAppManifest.xml,确保存在以下扩展:

代码语言:javascript
复制
<Extensions> <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions>

2.编写更改背景图像的代码以下是如何编写设置背景的代码的示例。

代码语言:javascript
复制
private async void lockHelper(Uri backgroundImageUri, string backgroundAction)
        {
            try
            {
                //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 LockScreenManager.RequestAccessAsync();
                //Check the status to make sure we were given permission. 
                bool isProvider = LockScreenManager.IsProvidedByCurrentApplication; if (isProvider)
                {
                    //Do the update. 
                    Windows.Phone.System.UserProfile.LockScreen.SetImageUri(backgroundImageUri);
                    System.Diagnostics.Debug.WriteLine("New current image set to {0}", backgroundImageUri.ToString());
                }
                else { MessageBox.Show("You said no, so I can't update your background."); }
            }
            catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
        }

关于Uris,有很多选择,但请记住:

要使用应用程序中附带的图像,请使用ms-appx:/

代码语言:javascript
复制
Uri imageUri = new Uri("ms-appx:///background1.png", UriKind.RelativeOrAbsolute); LockScreen.SetImageUri(imageUri);

要使用存储在本地文件夹中的图像,请使用ms-appdata:/ Local /shared/shared内容必须是/shared/shellcontent子文件夹或其下的

代码语言:javascript
复制
Uri imageUri = new Uri("ms-appdata:///local/shared/shellcontent/background2.png", UriKind.RelativeOrAbsolute); LockScreen.SetImageUri(imageUri);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18344689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档