0

我有一个下载图像的功能,下载完成后会触发 toast 通知。当用户单击 toast 通知时,我想使用默认程序打开文件。但是,当启动器在匿名委托中时,它会失败。

这是设置匿名委托的代码

            var savefolder= await KnownFolders.PicturesLibrary.GetFolderAsync("savefolder");
            Uri source;
            if (!Uri.TryCreate(ImageUrl, UriKind.Absolute, out source))
            {
                return;
            }
            var destination = imageFileName;
            if (string.IsNullOrWhiteSpace(destination))
            {
                return;
            }
            var destinationFile = await savefolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);
            var httpClient = new HttpClient();
            var data = await httpClient.GetByteArrayAsync(source);
            var targetStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);
            await targetStream.AsStreamForWrite().WriteAsync(data, 0, data.Length);
            await targetStream.FlushAsync();
            targetStream.Dispose();
            TypedEventHandler<ToastNotification, object> openPic =
                async delegate(ToastNotification toastSender, object toastArgs)
                {
                    var success = await Launcher.LaunchFileAsync(destinationFile);
                    var i = 0;
                };
            CommentProcessing.LoadToast("The image has been saved to your pictures library", openPic);

这是 Toast 功能

    public static void LoadToast(string text, TypedEventHandler<ToastNotification, object> toastActivated = null)
    {
        var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
        var elements = toastXml.GetElementsByTagName("text");
        elements[0].AppendChild(toastXml.CreateTextNode(text));
        var toast = new ToastNotification(toastXml);
        if (toastActivated != null)
            toast.Activated += toastActivated;
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

现在,它几乎完全按计划工作。委托触发并调用启动器。但是当它试图启动时,它失败了。

我已经能够单步执行代码,并且当委托触发时,所有文件信息仍然完好无损。

有任何想法吗?

4

1 回答 1

2

这有点长镜头,但您可以尝试将图像的开头卸载到调度程序:

TypedEventHandler<ToastNotification, object> openPic =
    async delegate(ToastNotification toastSender, object toastArgs)
    {
        Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
            () => var success = await Launcher.LaunchFileAsync(destinationFile);
    };
于 2013-05-21T04:42:51.733 回答