使用FileSavePicker与MessageDialog的IUICommand事件事件、FileSavePicker、MessageDialog、IUICommand

2023-09-04 13:31:49 作者:懷戀如初

独立,所有code完美的作品。该代码段用于保存文件的片段采摘的目录将它保存到,并在消息对话框的伟大工程。

但是,当我将其结合在一起,我得到拒绝访问。我不使用的DocumentsLibrary能力,因为这样做在这种情况下,它不是必需的我的,但是,运行到证实,它是不是问题的问题之后实现这种能力。

情景: 用户希望在文本框中输入文本后,创建一个新文档。 A MessageDialog 出现,询问他们是否要保存首先改变现有的文件 - 用户点击是(保存文件)

现在,这里是你处理时引发的事件 MessageDialog

在IUICommand命令事件处理程序,测试该按钮被点击,并采取相应的行动。

我这样做是与switch语句:

 开关(command.Label){
   案是:
   保存存档(); //包含保存文件code对自己的作品扩展方法
   打破;
   为否:
   ClearDocument();
   打破;
   默认:
   打破;
}
 
如何用快捷键添加java中的File类文件,使红叉消失

现在,每一种情况下的伟大工程,除了是按钮。当您单击Yes,电子拉力方法被称为具有code,它保存到一个文件

当你点击Yes按钮,你得到拒绝访问的例外是。除了细节没有透露任何东西。

我觉得它是与我是如何使用 MesaageDialog 。但寻找了几个小时,我还没有找到如何保存文件的样本后, FileSavePicker MesaageDialog 按钮pressed。

任何想法,在这个应该怎么做?

更新瓦特/ code

当用户点击AppBar的新文件按钮,此方法火灾:

 异步私人无效New_Click(对象发件人,RoutedEventArgs E)
{
    如果(NoteHasChanged)
    {
        关闭文件并创建一个新的前//提示保存更改。
        如果(!HasEverBeenSaved)
        {

            MessageDialog对话框=新MessageDialog(你想创建一个新的?之前保存此文件,
                确认书);
            dialog.Commands.Add(新UICommand(是,新UICommandInvokedHandler(this.CommandInvokedHandler)));
            dialog.Commands.Add(新UICommand(否,新UICommandInvokedHandler(this.CommandInvokedHandler)));
            dialog.Commands.Add(新UICommand(取消,新UICommandInvokedHandler(this.CommandInvokedHandler)));

            dialog.DefaultCommandIndex = 0;
            dialog.CancelCommandIndex = 2;

            // 展示下。
            等待dialog.ShowAsync();
        }
        其他 { }
    }
    其他
    {
        //放弃更改,并创建一个新的文件。
        RESET();
    }
}
 

而FileSavePicker的东西:

 私人无效CommandInvokedHandler(IUICommand命令)
{
    显示该命令的被调用的标签//显示信息
    开关(command.Label)
    {
        案是:

            MainPage的rootPage =这一点;
            如果(rootPage.EnsureUnsnapped())
            {
                //是被选择。保存文件。
                SaveNewFileAs();
            }
            打破;
        为否:
            RESET(); //完成。
            打破;
        默认:
            //不知道该怎么做,在这里。
            打破;
    }
}

异步公共无效SaveNewFileAs()
{
    尝试
    {
        FileSavePicker保护程序=新FileSavePicker();
        saver.SuggestedStartLocation = PickerLocationId.Desktop;
        saver.CommitButtonText =保存;
        saver.DefaultFileExtension =.TXT;
        saver.FileTypeChoices.Add(纯文本,新的名单,其中,字符串>(){名.txt});

        saver.SuggestedFileName = noteTitle.Text;

        StorageFile文件=等待saver.PickSaveFileAsync();
        thisFile =文件;

        如果(文件!= NULL)
        {
            CachedFileManager.DeferUpdates(thisFile);

            等待FileIO.WriteTextAsync(thisFile,theNote.Text);

            FileUpdateStatus FUS =等待CachedFileManager.CompleteUpdatesAsync(thisFile);
            //如果(FUS == FileUpdateStatus.Complete)
            //值= TRUE;
            //其他
            //值= FALSE;

        }
        其他
        {
            //操作被取消。
        }

    }
    赶上(例外的例外)
    {
        的Debug.WriteLine(exception.InnerException);
    }
}
 

解决方案

好了,现在我已经想通了:-)。该文件明确说,你不应该表现出新的弹出式广告/文件选择器中的UICommand:

http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.popups.messagedialog.showasync

这是一个坏办法一个例子来做到这一点:

 专用异步无效Button_Click(对象发件人,RoutedEventArgs E)
    {
        MessageDialog对话框=新MessageDialog(preSS确定以显示新的对话框(该应用程序会崩溃)。);
        dialog.Commands.Add(新UICommand(OK,新UICommandInvokedHandler(OnDialogOkTest1)));
        dialog.Commands.Add(新UICommand(取消));

        等待dialog.ShowAsync();
    }

    私人异步无效OnDialogOkTest1(IUICommand命令)
    {
        MessageDialog secondDialog =新MessageDialog(这是第二个对话框);
        secondDialog.Commands.Add(新UICommand(OK));

        等待secondDialog.ShowAsync();
    }
 

这是做了正确的方法:

 专用异步无效Button_Click_1(对象发件人,RoutedEventArgs E)
    {
        MessageDialog对话框=新MessageDialog(preSS确定以显示新的对话框);
        UICommand okCommand =新UICommand(OK);
        UICommand cancelCommand =新UICommand(取消);

        dialog.Commands.Add(okCommand);
        dialog.Commands.Add(cancelCommand);

        IUICommand响应=等待dialog.ShowAsync();

        如果(响应== okCommand)
        {
            MessageDialog secondDialog =新MessageDialog(这是第二个对话框);
            secondDialog.Commands.Add(新UICommand(OK));

            等待secondDialog.ShowAsync();
        }
    }
 

很简单其实,我应该得到这个前面...

Individually, all code works perfectly. The snippet for saving the file, the snippet for picking a directory to save it to and also the message dialog works great.

But when I tie it all together, I get an access denied. I am not using the DocumentsLibrary capability since it is not required of me to do so in this case, however, enabling this capability after running into issues confirmed that it is not the issue.

Scenario: User wants to create a new document after entering text in the text box. A MessageDialog appears, asking them if they want to save changes to the existing file first - the user clicks Yes (save file).

Now, here is where you handle the event that was raised by the MessageDialog.

Inside the IUICommand command event handler, you test for which button was clicked, and act accordingly.

I did this with a switch statement:

switch(command.Label) {
   case "Yes":
   SaveFile(); // extension method containing save file code that works on its own
   break;
   case "No":
   ClearDocument();
   break;
   default:
   break;
}

Now, each case works great except for the Yes button. When you click yes, an e tension method is called which has code that saves to a file

It is when you click yes button that you get the ACCESS DENIED exception. Details of the exception didn't reveal anything.

I think that it has something to do with how I am using the MesaageDialog. But after searching for hours I have yet to find a sample on how to save a file with the FileSavePicker when a MesaageDialog button is pressed.

Any ideas in how this should be done?

Update w/ Code

When the user clicks the New document button on the AppBar, this method fires:

async private void New_Click(object sender, RoutedEventArgs e)
{
    if (NoteHasChanged)
    {
        // Prompt to save changed before closing the file and creating a new one.
        if (!HasEverBeenSaved)
        {

            MessageDialog dialog = new MessageDialog("Do you want to save this file before creating a new one?",
                "Confirmation");
            dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.CommandInvokedHandler)));
            dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.CommandInvokedHandler)));
            dialog.Commands.Add(new UICommand("Cancel", new UICommandInvokedHandler(this.CommandInvokedHandler)));

            dialog.DefaultCommandIndex = 0;
            dialog.CancelCommandIndex = 2;

            // Show it.
            await dialog.ShowAsync();
        }
        else { }
    }
    else
    {
        // Discard changes and create a new file.
        RESET();
    }
}

And the FileSavePicker stuff:

private void CommandInvokedHandler(IUICommand command)
{
    // Display message showing the label of the command that was invoked
    switch (command.Label)
    {
        case "Yes":

            MainPage rootPage = this;
            if (rootPage.EnsureUnsnapped())
            {
                // Yes was chosen. Save the file.
                SaveNewFileAs();
            }
            break;
        case "No":
            RESET(); // Done.
            break;
        default:
            // Not sure what to do, here.
            break;
    }
}

async public void SaveNewFileAs()
{
    try
    {
        FileSavePicker saver = new FileSavePicker();
        saver.SuggestedStartLocation = PickerLocationId.Desktop;
        saver.CommitButtonText = "Save";
        saver.DefaultFileExtension = ".txt";
        saver.FileTypeChoices.Add("Plain Text", new List<String>() { ".txt" });

        saver.SuggestedFileName = noteTitle.Text;

        StorageFile file = await saver.PickSaveFileAsync();
        thisFile = file;

        if (file != null)
        {
            CachedFileManager.DeferUpdates(thisFile);

            await FileIO.WriteTextAsync(thisFile, theNote.Text);

            FileUpdateStatus fus = await CachedFileManager.CompleteUpdatesAsync(thisFile);
            //if (fus == FileUpdateStatus.Complete)
            //    value = true;
            //else
            //    value = false;

        }
        else
        {
            // Operation cancelled.
        }

    }
    catch (Exception exception)
    {
        Debug.WriteLine(exception.InnerException);
    }
}

解决方案

OK, now I have figured it out :-). The documentation says explicit that you shouldn’t show new popups/file pickers in the UICommand:

http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.popups.messagedialog.showasync

This is an example of a bad way to do it:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageDialog dialog = new MessageDialog("Press ok to show new dialog (the application will crash).");
        dialog.Commands.Add(new UICommand("OK", new UICommandInvokedHandler(OnDialogOkTest1)));
        dialog.Commands.Add(new UICommand("Cancel"));

        await dialog.ShowAsync();
    }

    private async void OnDialogOkTest1(IUICommand command)
    {
        MessageDialog secondDialog = new MessageDialog("This is the second dialog");
        secondDialog.Commands.Add(new UICommand("OK"));

        await secondDialog.ShowAsync();
    }

This is the correct way to do it:

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
        MessageDialog dialog = new MessageDialog("Press ok to show new dialog");
        UICommand okCommand = new UICommand("OK");
        UICommand cancelCommand = new UICommand("Cancel");

        dialog.Commands.Add(okCommand);
        dialog.Commands.Add(cancelCommand);

        IUICommand response = await dialog.ShowAsync();

        if( response == okCommand )
        {
            MessageDialog secondDialog = new MessageDialog("This is the second dialog");
            secondDialog.Commands.Add(new UICommand("OK"));

            await secondDialog.ShowAsync();
        }
    }

Quite simple actually, I should have get this earlier...

 
精彩推荐
图片推荐