Windows Store应用程序启动时,我想提示用户使用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,而选择自己种植的东西?

最佳答案

MessageDialog的命令在关闭之前会触发,并且您无法在第二个模式对话框仍然打开时打开第二个模式对话框。

您需要将对PickFolder的调用延迟到MessageDialog完成之前。由于您仍在等待它,因此可以在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。

关于c# - 从UICommand/MessageDialog启动FilePicker或FolderPicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31593106/

10-10 14:25