2012-09-20 14 views
0

我想創建一個類似CaptureFileAsync方法的庫,即在方法調用時,它將打開一個帶有標準後退導航的全屏頁面並將結果返回給調用者。如何創建一個像CameraCaptureUI.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的工作原理:在這種情況下,保存了調用頁面的相同實例。

我的問題是:如何在不影響幀導航的情況下顯示來自我的庫的頁面,並無意中導致創建調用頁面的新實例?

回答

1

你可以把你所有的用戶界面放在彈出窗口中。

+0

聽起來是個好主意。我正在考慮今天上班的路上。我會讓你知道它是如何工作的。 –

2

看看PopupHelper

它使用抽象的彈出窗口全部ickines和負責的動畫,禁止訪問控制等

+0

嗯,這不完全是我原來的問題的答案,但它看起來像一個很好的包裝,使彈出式工作更容易。你有沒有考慮過把圖書館拿出來放在NuGet上? –

+0

不 - 但我沒有問題幫助某人如果他們想要。你爲什麼說這不會幫助你原來的問題?你想要一個「頁面」導航到不會導致你的上一頁丟失上下文。這正是這樣做的(上面的Popup答案被標記爲「答案」 - 這使得它更容易) –

+0

你是對的,它可能不是一個直接的答案,但它確實有助於使它工作:) –

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(); 
相關問題