0

我想创建一个行为类似于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工作方式:在这种情况下,调用页面的相同实例被保留。

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

4

3 回答 3

2

看看PopupHelper

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

于 2012-10-14T11:17:27.270 回答
1

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

于 2012-09-21T04:31:07.683 回答
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();
于 2014-02-12T21:31:15.623 回答