MyApp.DLL中发生'System.IO.FileNotFoundException'
C#代码如下。错误指针在LockScreen.SetImageUri(uri)上指示“这是将要执行的下一个语句”。
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private async void SetBackground1(object sender, RoutedEventArgs e)
{
if (await LockScreenManager.RequestAccessAsync() == LockScreenRequestResult.Granted)
{
var uri = new Uri("ms-appx:///Assets/1.jpg", UriKind.Absolute);
LockScreen.SetImageUri(uri);
}
else
{
MessageBox.Show("You said no, so I can't update your background.");
}
}
}
答案 0 :(得分:2)
将图像保存到隔离存储而不是项目文件夹。然后,只要你愿意,就可以从隔离的存储中检索图像,
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);
}
}
答案 1 :(得分:1)
请确保图片位于指定位置,并且Build Action
属性设置为Content
: