我正在编写简单的WP应用程序,它可以处理存储在设备中的图像或者在我的应用程序中由相机拍摄的图像。我可能会对导航和相机初始化感到困惑。应用程序中有一个MainPage,用于选择相机或库的按钮。 Camera按钮就像这样导航到CameraPage:
private void CameraButton_Click(object sender, RoutedEventArgs e)
{
...
this.Frame.Navigate(typeof(CameraPage));
}
然后我在CameraPage OnNavigatedTo中初始化相机:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
...
cameraCapture = new CameraCapture();
PreviewElement.Source = await cameraCapture.Initialize(Dispatcher);
await cameraCapture.StartPreview();
...
}
和CameraCapture初始化:
public async Task<MediaCapture> Initialize(CoreDispatcher dispatcher)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
// Create MediaCapture and init
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
mediaCapture.VideoDeviceController.PrimaryUse = Windows.Media.Devices.CaptureUse.Video;
this.dispatcher = dispatcher;
var maxResolution = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution);
imgEncodingProperties = ImageEncodingProperties.CreateJpeg();
return mediaCapture;
}
问题是 - 当我点击主页上的CameraButton时,应用程序在MainPage上冻结,直到相机未初始化,然后加载CameraPage。处理这个问题的最佳方法是什么?我应该只将内容从CameraPage移动到MainPage(所以在MainPage上初始化相机并在这里拍照)?或者有更好的方法 - 就像CameraPage上的某些事件一样?点击CameraPage上的另一个按钮来初始化相机会让我和用户都很烦恼。