gpt4 book ai didi

c# - 拍摄照片后无法导航到另一个页面

转载 作者:太空宇宙 更新时间:2023-11-03 21:49:16 25 4
gpt4 key购买 nike

我制作了一个可以拍摄照片的应用程序,保存照片后,它会转到另一个页面,比如 page1.xaml,但是我得到一个错误:|

错误是 System.Windows.ni.dll 中发生类型为“System.UnauthorizedAccessException”的未处理异常,消息显示无效的跨线程访问。 什么就是它?我是开发 WP 应用程序的新手。

这是我的代码

void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
{
string fileName = savedCounter + ".jpg";

try
{ // Write message to the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Captured image available, saving picture.";
});

// Save picture to the library camera roll.
library.SavePictureToCameraRoll(fileName, e.ImageStream);

// Write message to the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Picture has been saved to camera roll.";

});

// Set the position of the stream back to start
e.ImageStream.Seek(0, SeekOrigin.Begin);

// Save picture as JPEG to isolated storage.
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;

// Copy the image to isolated storage.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}

// Write message to the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Picture has been saved to isolated storage.";

});
}
finally
{
// Close image stream
e.ImageStream.Close();

GoTo();
}
}


private void GoTo()
{
this.NavigationService.Navigate(new Uri("/page1.xaml", Urikind.Relative));
}

最佳答案

您正在从后台线程调用 GoTo 方法。导航时,需要在前台线程上进行。

private void GoTo()
{
Dispatcher.BeginInvoke(() =>
{
this.NavigationService.Navigate(new Uri("/page1.xaml", Urikind.Relative));
});
}

关于c# - 拍摄照片后无法导航到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15324955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com