我创建了一个函数来查找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");
}
}
}
}
输出:
之前
前
后 之后