await前后的代码是运行两次

The code before and after await is running twice

我创建了一个函数来查找SD卡是否存在,但是var await前后的代码是运行两次。

单击按钮时,我正在调用 InvokeRefresh() 函数,但是 var sdfiles = await removableDevices.GetFilesAsync(); 行前后的代码是 运行 两次。

代码:

<Page
    x:Class="App7.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App7"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Button x:Name="button" Content="Click me" HorizontalAlignment="Left" Margin="144,261,0,0" VerticalAlignment="Top" Click="button_Click"
                RenderTransformOrigin="-0.48,0.63"/>

    </Grid>
</Page>



using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App7
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            InvokeRefresh();
        }

        private async void InvokeRefresh()
        {
            StorageFolder removableDevices;
            removableDevices = (await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();
            if (removableDevices == null)
            {
                MessageDialog mssgboxx1 = new MessageDialog("SD card not found. Add external SD card with downloaded bastas.");
                await mssgboxx1.ShowAsync();
            }
            else
            {
                Debug.WriteLine("before");
                var sdfiles = await removableDevices.GetFilesAsync();
                var epath = await removableDevices.CreateFolderAsync("ebasta", CreationCollisionOption.OpenIfExists);
                Debug.WriteLine("after");
            }
        }
    }
}

输出:

before
before
after
after

我要尝试的第一件事是更改这段代码。如果出于某种原因按钮被点击两次 InvokeRefresh 将同时 运行 两次而不是等待。

private async void button_Click(object sender, RoutedEventArgs e)
    {
        await InvokeRefresh();
    }

您还必须稍微更改一下方法:

private async Task InvokeRefresh()

输出