如何在WPF中重置页面

时间:2015-07-12 09:07:01

标签: c# wpf camera

我有一个相机代码:

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 个答案:

答案 0 :(得分:0)

重置/重新加载我想到的页面的两种最简单的方法是:

编写一个方法,将所有字段和属性重置为默认状态,然后从按下按钮调用此方法。

或者:

启动需要从另一个控件重置的窗口,并将重置按钮链接到父控件上的方法以关闭然后重新打开页面。

希望这有帮助。

答案 1 :(得分:0)

在移动页面之前删除captureManager,并在OnNavigatedTo()中创建它,它将重置它。