0

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.");
        }
    }
}
4

2 回答 2

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);
           }
  }
于 2013-09-12T11:50:58.323 回答
1

请确保图像存在于指定位置,并且其Build Action属性设置为Content

在此处输入图像描述

于 2013-09-12T11:04:25.973 回答