У меня есть следующий код для камеры:Как восстановить страницу в WPF
public sealed partial class Camera : Page
{
double width, height;
MediaCapture captureManager;
bool flashOn = false;
public Camera()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
captureManager = new MediaCapture();
imagePreview.Source = null;
capturePreview.Source = null;
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
width = Convert.ToInt32(Window.Current.Bounds.Width);
height = Convert.ToInt32(Window.Current.Bounds.Height);
capturePreview.Width = width;
capturePreview.Height = height;
imagePreview.Width = width;
imagePreview.Height = height;
Starter();
}
async private void Starter()
{
await captureManager.InitializeAsync();
StartCapturePreview_Click();
}
async private void StartCapturePreview_Click()
{
capturePreview.Source = captureManager;
await captureManager.StartPreviewAsync();
}
async private void StopCapturePreview_Click(object sender, RoutedEventArgs e)
{
await captureManager.StopPreviewAsync();
}
private async void focus_Click(object sender, RoutedEventArgs e)
{
try
{
while (true)
{
await captureManager.VideoDeviceController.FocusControl.UnlockAsync();
var focusSettings = new FocusSettings();
focusSettings.AutoFocusRange = AutoFocusRange.Normal;
focusSettings.Mode = FocusMode.Auto;
focusSettings.WaitForFocus = true;
focusSettings.DisableDriverFallback = false;
captureManager.VideoDeviceController.FocusControl.Configure(focusSettings);
await captureManager.VideoDeviceController.FocusControl.FocusAsync();
}
}
catch { }
}
private async void flash_Click(object sender, RoutedEventArgs e)
{
if (flashOn) flashOn = false;
else flashOn = true;
CapturePhoto.Content = flashOn.ToString();
var foc = captureManager.VideoDeviceController.FocusControl;
await foc.FocusAsync();
}
async private void CapturePhoto_Click(object sender, RoutedEventArgs e)
{
var flash = captureManager.VideoDeviceController.TorchControl;
if (flashOn) flash.Enabled = true;
await Task.Delay(500);
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
// create storage file in local app storage
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"Photo.jpg",
CreationCollisionOption.ReplaceExisting);
// take photo
await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
// Get photo as a BitmapImage
BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
// imagePreivew is a <Image> object defined in XAML
imagePreview.Source = bmpImage;
flash.Enabled = false;
Frame.Navigate(typeof(MainPage));
}
}
Я пытаюсь найти способ сброса страницы (например, добавив кнопку, чтобы вернуть фотографию или вернуться к одна и та же страница позже), но не может найти способ ... Я попытался изменить все начальную конструкцию объектов в функции OnNavigatedTo, но она по-прежнему не работает.
Первый способ не работает. Я не совсем понял, что вы имели в виду со вторым решением – IsaacDj