![](/img/trans.png)
[英]How to use LockScreen.SetImageUri to Set Windows Phone 8 Lock Screen Background
[英]Windows Phone 8 Lockscreen doesn't set when LockScreen.SetImageUri() is called
首先,我已經在我的應用程序清單中添加了擴展名,並且鎖屏圖像正在更改,我的問題源於多次更改。
在該應用程序中,我還可以讓某個人按下一個按鈕,該按鈕將從互聯網上抓取圖像,將其保存到存儲中,然后將其設置為鎖定屏幕。 第一次有人按下按鈕時,(大多數情況下)它將正確設置,盡管我的問題是,再次按下按鈕時,它通常會選擇不設置,因此要求用戶必須多次按下按鈕希望它設置正確。
這是我的按鈕按下代碼
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;
}
}
**此代碼只是MSDN和Channel-9中代碼的略微修改版本
需要注意的是,該映像正在正確刪除,下載和保存,這已通過隔離存儲瀏覽器進行了檢查。 我什至嘗試設置其他圖像作為鎖定屏幕,然后再設置下載圖像,以防文件名相似性弄亂了它。 我什至嘗試讓它等待2秒鍾再嘗試設置。 兩者都不起作用。 同時檢查我的輸出窗口,它正在輸入方法。
我已經在這里待了幾天,試圖弄清楚這個問題,但我一直在想。 是否有人可以提供幫助或至少提供一些建議?
謝謝。
當我在應用程序上實現此功能時,我必須解決以下問題:
在WmAppManiest.xml支持中啟用此選項,以使應用程序成為后台提供程序。
請求該應用程序的權限,以便將其設置為后台提供程序。
設置圖像
要使用應用程序附帶的圖像,請使用ms-appx:///
Uri imageUri =新Uri(“ ms-appx:///background1.png”,UriKind.RelativeOrAbsolute); LockScreen.SetImageUri(imageUri);
若要使用存儲在本地文件夾中的圖像,請使用ms-appdata:/// local / shared / shellcontent 必須在/ shared / shellcontent子文件夾中或之下
Uri imageUri =新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/
另外,就我而言,我必須定期從Internet下載新的后台鎖定屏幕。 一次它不起作用是因為操作系統沒有重新加載映像,即使使用新下載的映像修改了文件也是如此。 解決方法是更改下載文件的名稱。 因此,它第一次使用:“ ms-appdata:///local/shared/shellcontent/background1.png”第二次應用程序使用:“ ms-appdata:///local/shared/shellcontent/background2.png下次它將使用background1.png重復循環。
草本植物
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.