await之前和之后的代码运行两次

时间:2016-10-14 09:12:32

标签: c# windows-phone-8.1

我创建了一个函数来查找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");
            }
        }
    }
}

输出:

  

之前
  
前   
后   之后

1 个答案:

答案 0 :(得分:1)

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

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

您还必须稍微更改方法:

private async Task InvokeRefresh()

<强>输出

enter image description here