如何创建像CameraCaptureUI.CaptureFileAsync这样的对话框?

时间:2012-09-20 08:20:54

标签: windows-8 windows-runtime winrt-xaml

我想创建一个类似于CaptureFileAsync方法的库,即在方法调用中,它会打开一个带有标准后导航的全屏页面并将结果返回给调用者。

我希望能够像调用CaptureFileAsync一样调用它:

var dialog = new Library.Dialog();
var result = await dialog.Show();

Show方法中,我当前导航到我自己的页面并返回Task,来电者可以等待:

public Task<string> Show()
{
    var task = new Task<string>(() => result);

    var frame = ((Window.Current.Content) as Frame);
    frame.Navigate(typeof(DialogPage));

    return task;
}

我在关闭对话框时调用task.Start()(通过向后导航取消或通过按下按钮确认),这会导致结果返回给等待的来电者。

问题在于,当调用Frame.GoBack()时,会创建上一页的新实例,并将结果返回到不再显示的旧实例。这不是CaptureFileAsync的工作方式:在这种情况下,保留了调用页面的相同实例。

我的问题是:如何在不影响框架导航的情况下显示我的库中的页面,并且无意中导致创建调用页面的新实例?

3 个答案:

答案 0 :(得分:2)

看看PopupHelper

它抽象使用Popups的所有ickine并处理动画,禁用对控件的访问等。

答案 1 :(得分:1)

您可以将所有UI放在弹出窗口中。

答案 2 :(得分:1)

我遇到了同样的问题,这就是我提出的问题:

<强> XAML

<Grid x:Name="MainGrid" 
      Background="#7F000000">
    <Grid Width="480" Height="180" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Black">
        <StackPanel>
            <TextBlock Text="Custom Capture!" Style="{StaticResource HeaderTextBlockStyle}" Margin="20"/>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button x:Name="SaveButton" Content="Save" HorizontalAlignment="Center" Click="CloseButton_Click"/>
                <Button x:Name="CloseButton" Content="Close" HorizontalAlignment="Center" Click="CloseButton_Click"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Grid>

控件

public sealed partial class CustomCaptureControl : UserControl
{
    private StorageFile file;
    private ManualResetEvent reset;
    private Popup _mainPopup;

    /// <summary>
    /// 
    /// </summary>
    public CustomCaptureControl()
    {
        this.InitializeComponent();

        Rect windowBounds = CoreWindow.GetForCurrentThread().Bounds;
        this.MainGrid.Width = windowBounds.Width;
        this.MainGrid.Height = windowBounds.Height;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public async Task<StorageFile> ShowAsync()
    {
        StorageFile file = await Task.Run(() => this.GetFile());
        return file;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private async Task<StorageFile> GetFile()
    {
        //Launch Popup in UI thread
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => 
        {
            this._mainPopup = new Popup();
            this._mainPopup.Child = this;
            this._mainPopup.IsOpen = true;
        });

        //Await user input
        await Task.Run(() => this.AwaitUserInput());

        return this.file;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private Task AwaitUserInput()
    {
        return Task.Run(() =>
        {
            this.reset = new ManualResetEvent(false);
            WaitHandle.WaitAll(new WaitHandle[] { this.reset });
        });
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        Uri fileURI = new Uri("ms-appx:///Assets/SomeFile.pdf");
        this.file = await StorageFile.GetFileFromApplicationUriAsync(fileURI);

        this.reset.Set();

        this._mainPopup.IsOpen = false;
        this._mainPopup = null;
    }
}

<强>用法

CustomCaptureControl capture = new CustomCaptureControl();
StorageFile file = await capture.ShowAsync();