![](/img/trans.png)
[英]Windows Phone 8 Lockscreen doesn't set when LockScreen.SetImageUri() is called
[英]How to use LockScreen.SetImageUri to Set Windows Phone 8 Lock Screen Background
我在IsolatedStorage中有一個圖像,我想以編程方式將其設置為設備鎖定屏幕背景。 我的問題是我無法獲得LockScreen.SetImageUri
所需的正確路徑。 通過引用http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj206968(v=vs.105).aspx ,很明顯“ ms-appdata:/// local /”是本地圖像所需的前體。
var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);
我已經在我的應用程序IsolatedStorage中創建了一個名為Pictures
的文件夾,其中從CameraCaptureTask中保存了jpg圖像。 我已經嘗試了幾種通過上述方案訪問此文件夾中的圖像的方法,但是我總是在下一行收到ArgumentException
Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);
但是,在調試時,我看到uri = "ms-appdata:///Local/Pictures/WP_20130812_001.jpg"
,這不正確嗎?
我的實現如下
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?
要從應用設置鎖定屏幕圖像,您可能需要聲明您的應用為鎖定屏幕提供程序。 您可以通過添加以下標記來修改WMAppManifest.xml文件來做到這一點:
<Extensions>
<Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
</Extensions>
檢查清單文件中是否有此標記。
我希望這可以幫助您解決您的問題。
如果您的應用程序已經安裝,請確保它是背景圖像提供程序。 如果不是,請轉到設置->鎖定屏幕->背景,然后從列表中選擇您的應用程序。
在程序方面:
1.使用XML編輯器在應用清單文件 Edit WMAppManifest.xml中聲明應用的意圖 ,確保存在以下擴展名:
<Extensions> <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions>
2.編寫代碼以更改背景圖像以下是如何編寫用於設置背景的代碼的示例。
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:///
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.