![](/img/trans.png)
[英]MediaCapture + CaptureElement lifecycle/navigation management
[英]Handling Resuming event for MediaCapture - CaptureElement when combined with the File Picker sample
這適用於從File Picker示例創建的Windows Universal App。 該示例的基本代碼包括Windows Phone項目中的ContinuationManager
類和App.xaml.cs
文件中的OnActivated
方法,以及一個常見的NavigationHelper
類。
我還使用MediaCapture
和CaptureElement
的解決方案,但我不能正確地與處理Resuming
事件。 這是我的工作:
我使用NavigationHelper_LoadState
和NavigationHelper_SaveState
方法來啟動和停止相機預覽(這是LiveCamera.xaml.cs
文件的一部分)。
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// Start the camera preview
await StartCameraPreview();
}
private async void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
// Stop the camera preview
await StopCameraPreview();
}
在應用程序內的頁面之間導航時,此方法效果很好,但不會在“掛起/恢復”事件時停止/重新啟動相機。
我通過在App.xaml.cs
添加以下方法來處理Resuming事件來解決此問題( SuspensionManager
負責在恢復應用程序時調用NavigationHelper_LoadState
方法):
async void App_Resuming(object sender, object e)
{
await SuspensionManager.RestoreAsync();
}
上面的代碼在附加了Visual Studio的情況下(在“調試”和“釋放”模式下)執行時效果很好:當接收到“掛起/恢復”事件並且文件選取器正確返回文件時,攝像機預覽將停止/重新啟動。
但是,如果我在沒有Visual Studio的情況下執行該應用程序(僅從應用程序列表中啟動該應用程序),則在接收到掛起/恢復事件時,相機預覽仍會停止/重新啟動,但是當使用“文件選擇器”選擇一個文件時,會看到“正在恢復。 ..”進度條,然后該應用就會崩潰。
選擇文件后, App_Resuming
和OnActivated
方法以某種方式發生沖突。 我已經通過在輸入每種方法時顯示MessageDialog
驗證了這一點(因為我無法使用Visual Studio再現問題):選擇圖片后,我在應用程序崩潰前短暫地看到了App_Resuming消息(從未看到過OnActivated消息) )。 我不希望在文件選擇器之后調用該方法,因為在附加了VS的應用程序執行時不會調用該方法。
當未附加VS時,為什么調用的方法不同(據我所知,不正確)?
存在此問題是因為您正在Page的構造函數中運行FileOpenPicker 。 那沒什么 為了測試,我在您的LoadPhoto頁面中提供了一個按鈕:
在XAML中:
<Grid>
<Button Name="myButton" Content="Click me"/>
<Image x:Name="Image" Stretch="Uniform"/>
</Grid>
在構造函數中:
public LoadPhoto()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
myButton.Click += (sender, e) => LaunchPicker();
}
您可以在此處下載代碼。
最好先選擇一個文件,然后導航到頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.