将图像保存到独立存储而不是项目文件夹。然后像这样随时从隔离存储中检索图像,
var lockimageuri = new Uri("ms-appdata:///Local/" + "lockimage0.jpg", UriKind.Absolute);
LockScreen.SetImageUri(lockimageuri);
这里 locimage0.jpg 是存在于隔离存储中的图像。
这是将图像保存到独立存储中的代码。
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
string filePath = "lockimage0.jpg";
if (store.FileExists(filePath))
{
store.DeleteFile(filePath);
}
IsolatedStorageFileStream fileStream = store.CreateFile(filePath);
wbm.SaveJpeg(fileStream, wbm.PixelWidth, wbm.PixelHeight, 0, 100);
fileStream.Close();
}
您也可以使用此方法从项目文件夹中读取本地图像。
private WriteableBitmap ReadLocalImage(string Uri)
{
StreamResourceInfo sri = null;
Uri uri = new Uri(Uri, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
return wb;
}
这就是我在我的应用程序中实现自定义锁屏的方式。
还要确保您更新了清单文件
<Extensions>
<Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
</Extensions>
我还尝试使用本地项目文件夹中的图像,这对我来说也很好。这是我尝试过的代码。
private async void SetLockScreen()
{
//Check to see if the app is currently the lock screen provider
if (!LockScreenManager.IsProvidedByCurrentApplication)
{
//Request to be lock screen provider
await LockScreenManager.RequestAccessAsync();
}
//Check to see if the app is currently the lock screen provider
if (LockScreenManager.IsProvidedByCurrentApplication)
{
//Set the image to the lock screen image
Uri imageUri = new Uri("ms-appx:///Images/lockscreen.png", UriKind.RelativeOrAbsolute);
LockScreen.SetImageUri(imageUri);
}
}