如何在 WPF 中重置页面
How to reset page in 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 函数中对象的所有初始构造,但它似乎仍然不起作用。
我想到的重置/重新加载页面的 2 种最简单的方法是:
编写一个将所有字段和属性重置为其默认状态的方法,然后在按下按钮时调用此方法。
或者:
启动需要从另一个控件重置的window和link重置按钮到父控件上的方法以关闭然后重新打开页面。
希望对您有所帮助。
只需在移动页面之前删除 captureManager 并在 OnNavigatedTo() 中创建它,它就会重置它。
我有以下相机代码:
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 函数中对象的所有初始构造,但它似乎仍然不起作用。
我想到的重置/重新加载页面的 2 种最简单的方法是:
编写一个将所有字段和属性重置为其默认状态的方法,然后在按下按钮时调用此方法。
或者:
启动需要从另一个控件重置的window和link重置按钮到父控件上的方法以关闭然后重新打开页面。
希望对您有所帮助。
只需在移动页面之前删除 captureManager 并在 OnNavigatedTo() 中创建它,它就会重置它。