[英]'System.IO.FileNotFoundException'
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.");
}
}
}
將圖像保存到隔離存儲而不是項目文件夾。 然后,您可以隨時從隔離的存儲中檢索圖像,
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);
}
}
請確保圖像位於指定位置,並且其Build Action
屬性設置為Content
:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.