<metro>新建文件

       首先,我们用vs2012来选择Blank App模板,再选好Button和BlockText控件,在Button中找到Click事件,点击进去。

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="files_btn" Content="FileSavePicker" HorizontalAlignment="Left" Margin="514,79,0,0" VerticalAlignment="Top" Click="Files_Click"/>
<TextBlock x:Name="fOutput" HorizontalAlignment="Left" Height="207" Margin="90,241,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="552"/>

    </Grid>

 

       其次,在Click事件中,用FileSavePicker类来New一个实例savePicker。 用实例savePicker来调用SuggestedStartLocation带确定起始位置,用FileTypeChoices来确定好要建文件类型,用DefaultFileExtension来确定默认类型,用SuggestedFileName来确定文件名。

       接着,用StorageFile类来新建一个实例,用来等待异步创建文件。然后创建好的文件名输出。

       最后,按F5。

在Click事件中的代码是:

  private async void Files_Click(object sender, RoutedEventArgs e)
        {
            FileSavePicker savePicker = new FileSavePicker();
            savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
            savePicker.FileTypeChoices.Add("Microsoft Word Document", new List<string>() { ".docx", ".doc" });
            savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
            savePicker.DefaultFileExtension = ".docx";
            savePicker.SuggestedFileName = "New Document";
            StorageFile savedItem = await savePicker.PickSaveFileAsync();
            if (null != savedItem)
            {
                fOutput.Text = savedItem.Name;
            }
            else
            {
                fOutput.Text = "End";
            }
        }

 

posted @ 2012-11-02 14:40  伍锋  阅读(302)  评论(0编辑  收藏  举报