您的位置:首页 > 其它

<metro>新建文件

2012-11-02 14:40 120 查看
首先,我们用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";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: