我想创建一个库的行为就像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
工作原理:在其案件调用页面的同一个实例被保留。
我的问题是:我怎么能显示我的内容库页面,而不会影响帧导航和无意中导致调用页面的新实例被创建?
看看PopupHelper 。
它使用抽象的弹出窗口全部ickines和动画需要照顾,禁止以控制等方式接入
我有同样的问题,这就是我想出了:
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();