首先,我已经在我的应用程序清单中添加了扩展,并且我的锁定屏幕图像正在改变,我的问题源于多次更改。
在这个应用程序中,我还可以让一个人点击一个按钮,在那里它会从互联网上抓取一张图像,保存到存储中,然后设置为一个锁定屏幕。当有人第一次按下按钮时,它会(大部分时间)正确设置,尽管我的问题是,当再次按下按钮时,它通常会选择不设置,因此需要用户多次按该按钮,希望它能正确设置。
这是我的按钮按键代码
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和第九频道代码的一个稍微修改过的版本
需要注意的是,图像正在正确删除、下载和保存,这是我通过隔离存储资源管理器进行的检查。我甚至尝试将另一个图像设置为锁定屏幕,在设置我的下载图像之前--只是在文件名相似的情况下--把它搞砸了。我甚至试着让它等待2秒,然后再尝试重新设置。这两件事都没有。同时检查我的输出窗口,它正在输入方法。
我已经在这里呆了几天了,我一直在想这件事。有没有人能帮上忙,或者至少提供一些建议?
谢谢。
发布于 2014-04-02 06:18:27
当我在我的应用程序上实现这个特性时,我必须解决以下问题:
- To use an image that you shipped in your app, use ms-appx:///Uri imageUri =新Uri(“ms://backland1.png”,UriKind.RelativeOrAbsolute);LockScreen.SetImageUri(imageUri);
-若要使用存储在本地文件夹中的图像,请使用ms-appdata://本地/共享/外壳内容必须位于/shared/shared内容子文件夹中或以下
Uri imageUri = new imageUri 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”,下一次它将使用背景1.png重复循环。
草本
https://stackoverflow.com/questions/20435478
复制相似问题