C# 访问'时出现异常;FileOpenPicker';这是在WindowsStoreApps中的第二次
我想一个接一个地打开FileOpenPicker两次。也就是说,在第一次从FileOpenPicker中选择图像后,它返回到带有所选图像的页面,此时会显示一个消息对话框,单击MessageDialog上的OK按钮,我将再次打开FileOpenPicker,这一次我遇到一个奇怪的崩溃C# 访问'时出现异常;FileOpenPicker';这是在WindowsStoreApps中的第二次,c#,.net,windows-store-apps,windows-store,C#,.net,Windows Store Apps,Windows Store,我想一个接一个地打开FileOpenPicker两次。也就是说,在第一次从FileOpenPicker中选择图像后,它返回到带有所选图像的页面,此时会显示一个消息对话框,单击MessageDialog上的OK按钮,我将再次打开FileOpenPicker,这一次我遇到一个奇怪的崩溃 open.FileTypeFilter.Clear(); as的访问被拒绝。(来自HRESULT:0x80070005(E_ACCESSDENIED)的异常)。我的代码如下 private async void
open.FileTypeFilter.Clear();
as的访问被拒绝。(来自HRESULT:0x80070005(E_ACCESSDENIED)的异常)。我的代码如下
private async void PickImage()
{
FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
open.FileTypeFilter.Add(".png");
open.FileTypeFilter.Add(".jpeg");
open.FileTypeFilter.Add(".jpg");
StorageFile file = await open.PickSingleFileAsync();
if (file != null)
{
if (prop1.Source == null)
{
Dialogpopup();
return;
}
else if (prop2.Source == null)
{
Dialogpopup();
return;
}
}
}
private async void Dialogpopup()
{
MessageDialog msgDialog = new MessageDialog("Would you like to add additional photos?");
UICommand okBtn = new UICommand("Yes");
okBtn.Invoked = OkBtnClick;
msgDialog.Commands.Add(okBtn);
UICommand cancelBtn = new UICommand("No");
cancelBtn.Invoked = CancelBtnClick;
msgDialog.Commands.Add(cancelBtn);
//Show message
msgDialog.ShowAsync();
}
private async void OkBtnClick(IUICommand command)
{
img_PointerPressed(img_galary, null);
}
private void img_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (sender == img_galary)
{
PickImage();
}
}
为什么要调用
.Clear()
?如果我对该语句进行注释,它将在“open.FileTypeFilter.Add(“.bmp”)”上崩溃。请尝试将代码“If(file!=null)…”移动到第二个函数,并在等待对picker的调用后调用它。看起来你给皮克打了两次电话。是的,您使用的是async/await,但我认为垃圾收集器可能会导致它。如果我移动“If(file!=null)”,则messageDialog不会提示。我希望messageDialog在从选择器中选择image to page后提示。我的意思是添加PickImageMain(),您将在其中调用await PickImage()并通过PickImage()检查返回的值(添加返回值StorageFile并删除if(file!=null)。