06 March 2017

Controls: File Pickers với Save File Dialog trên Windows Store Apps (C #).

Yêu cầu: Windows 10 và Visual Studio 2015
- File Pickers: Lưu File văn bản
FileSavePicker saver = new FileSavePicker();
saver.SuggestedStartLocation = PickerLocationId.Desktop;
saver.FileTypeChoices.Add("Text File", new List<string>() { ".txt" });
saver.SuggestedFileName = "text-name";
StorageFile file = await saver.PickSaveFileAsync();

if (file != null)
{
    CachedFileManager.DeferUpdates(file);
    await FileIO.WriteTextAsync(file, this.txtName.Text);
    FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
}
- Example: Ghi file 
MainPage.xaml
Windows Store 2017
<Page
    x:Class="FilePicker.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FilePicker"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="SaveFileDialog" Width="140" Height="54" Content="Save a File" Click="SaveAFile_Click" Margin="223,232,0,482" />

        <TextBox x:Name="txtName" HorizontalAlignment="Left" Margin="30,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="560" Height="140" Background="YellowGreen"/>
    </Grid>
</Page>
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
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 FilePicker
{
    /// <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();
        }

        private async void SaveAFile_Click(object sender, RoutedEventArgs e)
        {
            FileSavePicker saver = new FileSavePicker();
            saver.SuggestedStartLocation = PickerLocationId.Desktop;
            saver.FileTypeChoices.Add("Text File", new List<string>() { ".txt" });
            saver.SuggestedFileName = "text-name";
            StorageFile file = await saver.PickSaveFileAsync();

            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                await FileIO.WriteTextAsync(file, this.txtName.Text);
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }
    }
}
- Chạy ứng dụng ghi file:

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang