从UICommand / MessageDialog启动FilePicker或FolderPicker

时间:2015-07-23 16:29:39

标签: c# asynchronous windows-runtime windows-store-apps

当我的Windows应用商店应用启动时,我想提示用户使用FolderPicker选择本地存储文件夹并将其保存为FutureAccessList。我使用的提示是MessageDialog

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var messageDialog = new MessageDialog("Please pick a folder where you'd like to store your documents", "Choose storage");
    messageDialog.Commands.Clear();
    messageDialog.Commands.Add(new UICommand("OK", async (command) =>
    {
        await PickFolder();
    });
    await messageDialog.ShowAsync();
}

private async Task PickFolder()
{
    FolderPicker folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    folderPicker.FileTypeFilter.Add(".txt");
    folder = await folderPicker.PickSingleFolderAsync();
    // lets just ignore cancellations for now
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("MyFolder", folder);
}

此代码不起作用 - 我收到拒绝访问错误

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

我认为使用messageDialog.ShowAsync()足以解决这个问题,但它似乎不起作用。有任何想法吗?

我是否必须抛弃漂亮的WinRT messageDialogs而转而支持自己种植的东西?

1 个答案:

答案 0 :(得分:3)

MessageDialog的命令在关闭之前触发,当第一个模式对话框仍处于运行状态时,您无法打开第二个模式对话框。

在MessageDialog完成之前,您需要将调用延迟到PickFolder。既然你还在等待它,你可以在ShowAsync之后调用它。这里没有问题,因为ok是唯一的选择,但你可以打开选择的命令来选择多个选项。

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var messageDialog = new MessageDialog("Please pick a folder where you'd like to store your documents", "Choose storage");
    messageDialog.Commands.Clear();
    UICommand okCommand = new UICommand("Ok");
    messageDialog.Commands.Add(okCommand);
    var cmd = await messageDialog.ShowAsync();
    if (cmd == okCommand)
    {
        await PickFolder();
    }
}

另一种选择是在调用PickFolder之前在UICommand处理程序中添加延迟(例如通过在Dispatcher.RunAsync块中调用PickFolder),以便MessageDialog可以关闭。