0

我已经隔离了以下代码,它在 OnNavigatedTo 事件中工作,所以我知道代码可以工作。但是,我不能在那里使用它。我需要在 Suspending 事件中使用它。但它不会在那里工作。当我设置断点时,它们不会在这个事件中的任何地方被击中。也没有编译时或运行时错误。

到底是怎么回事?

async void App_Suspending(
        Object sender,
        Windows.ApplicationModel.SuspendingEventArgs e)
        {
            IReadOnlyList<StorageFile> thefiles;

            var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
            thefiles = await localFolder.GetFilesAsync();

            foreach (var f in thefiles)
            {
                await f.DeleteAsync(StorageDeleteOption.Default);
            }
        }
4

2 回答 2

3

我的猜测是,当您在此方法中等待时,应用程序会退出 Suspending 方法,并以这种方式授予操作系统终止进程的权限。您可以通过在第一个等待(在 foreach 上)之后放置一个断点并检查它是否到达来测试它。

于 2013-01-17T12:09:36.727 回答
0

我找到了我的解决方案。它涉及检查应用程序是否被用户关闭。如果是这样,(在我的情况下,无论如何)删除这些临时文件是可以的。您可以在 OnLaunched 方法内的 App.xaml.cs 文件中执行此操作:

if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
            {

                IReadOnlyList<StorageFile> thefiles;

                var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
                thefiles = await localFolder.GetFilesAsync();

                foreach (var f in thefiles)
                {
                    await f.DeleteAsync(StorageDeleteOption.Default);
                }
            }
于 2013-01-17T13:28:47.037 回答