Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Store Apps > Windows Store and Controls (C#) > File Pickers กับ Save Dialog / Open Dialog บน Windows Store Apps (C#)



Clound SSD Virtual Server

File Pickers กับ Save Dialog / Open Dialog บน Windows Store Apps (C#)

File Pickers กับ Save Dialog / Open Dialog บน Windows Store Apps (C#) บทความนี้เราจะมาเรียนรู้การใช้งาน File Pickers ซึ่งการเขียน File Pickers บน Windows Store Apps จะมีรูปแบบที่เหมือนกับการเขียน Windows Form คือ สามารถที่จะ Browse เพื่อ Select เลือกไฟล์ และ Browse เพื่อที่จะ Save ไฟล์ ลงบน Windows 8 ได้ แต่รูปแบบ File และ Folder ที่อยู่บน Windows 8 อาจจะแตกต่างไปจาก Windows ก่อนหน้านี้พอสมควร เรามาลองดูตัวอย่างทั้ง 2 วิธี

Example 1 ทดสอบสร้าง File Pickers แบบเลือก Save ไฟล์บนเครื่อง Windows 8

            FileSavePicker saver = new FileSavePicker();
            saver.SuggestedStartLocation = PickerLocationId.Desktop;
            saver.FileTypeChoices.Add("Text File", new List<string>() { ".txt" });
            saver.SuggestedFileName = "text-name";

รูปแบบการเลือก Save ไฟล์ โดยสามารถกำหนด Location เป็น PickerLocationId.Desktop; (ไว้สำแหน่ง Default ที่ Desktop)

Windows Store Apps  Save Dialog / Open Dialog

ในตัวอย่างนี้เราจะทดสอบรับค่าและเขียนลงใน Textfile และเลือก Save ไฟล์ในตำแหน่ง Location ที่ต้องการ

MainPage.xaml
<Page
    x:Class="WindowsStoreApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    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="29,103,0,611" />

        <TextBox x:Name="txtName" HorizontalAlignment="Left" Margin="30,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
    </Grid>

</Page>









MainPage.xaml.cs
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.AccessCache;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;
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 WindowsStoreApps
{
    /// <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();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        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);
            }
        }

    }
}


Screenshot

Windows Store Apps  Save Dialog / Open Dialog

ทดสอบการกรอกข้อมูล และคลิกที่ Save a File

Windows Store Apps  Save Dialog / Open Dialog

จากนั้นจะปรากฏ Dialog เพื่อให้เลือก Save ไฟล์ในตำแหน่ง Desktop

Windows Store Apps  Save Dialog / Open Dialog

หลังจาก Save ไฟล์แล้ว ไฟล์จะถูกสร้างใน Desktop หรือ Location อื่น ๆ ที่เราได้เลือก Save ไว้

Windows Store Apps  Save Dialog / Open Dialog

ทดสอบเปิดไฟล์



Example 2 ทดสอบ File Pickers แบบเลือก Browse ไฟล์และเรียกไฟล์นั้นมาใช้งาน

Windows Store Apps  Save Dialog / Open Dialog

โดยในตัวอย่างนี้จะใช้ Image และ Button สำหรับ Browse ไฟล์รูปภาพ จากนั้นเรียกรูปภาพนั้นมาแสดงผล

Windows Store Apps  Save Dialog / Open Dialog

สร้าง Control ของ Image และ Button สำหรับแสดงผลรูปภาพ และการ Browse เลือกรูปภาพ

            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".bmp");
            picker.SuggestedStartLocation = PickerLocationId.Desktop;
            picker.ViewMode = PickerViewMode.Thumbnail;
            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    BitmapImage bitmapImage = new BitmapImage();

                    await bitmapImage.SetSourceAsync(fileStream);
                    this.imgView.Source = bitmapImage;
                }
            }

คำสั่งสำหรับ Browse เลือกไฟล์ และ เรียกใช้งาน Image มาแสดงบนหน้าจอ

MainPage.xaml
<Page
    x:Class="WindowsStoreApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    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}">
        <Image x:Name="imgView" Margin="37,29,1147,587"/>
        <Button x:Name="SelectFile" Width="140" Height="54" Content="Select File" Click="SelectFile_Click" Margin="39,212,0,502" />
    </Grid>

</Page>

MainPage.xaml.cs
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.AccessCache;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;
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;
using Windows.UI.Xaml.Media.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <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 SelectFile_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".bmp");
            picker.SuggestedStartLocation = PickerLocationId.Desktop;
            picker.ViewMode = PickerViewMode.Thumbnail;
            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    BitmapImage bitmapImage = new BitmapImage();

                    await bitmapImage.SetSourceAsync(fileStream);
                    this.imgView.Source = bitmapImage;
                }
            }

        }

    }
}


Screenshot

Windows Store Apps  Save Dialog / Open Dialog

แสดงหน้าจอ Apps ให้คลิกที่ Select File เพื่อ Open Dialog

Windows Store Apps  Save Dialog / Open Dialog

เลือกไฟล์ที่อยู่บน Desktop และเลือก Open

Windows Store Apps  Save Dialog / Open Dialog

ไฟล์รูปภาพจะถุกนำมาแสดงผลบน Control ของ Image ในหน้าจอ Apps







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-03-02 10:56:43 / 2017-03-19 14:42:45
  Download : No files
 Sponsored Links / Related

 
Windows Store Apps กับ Template เช่น Blank , Grid , Hub , Split Apps (C#)
Rating :

 
โครงสร้างของ Project บน Windows Store Apps และไฟล์ต่าง ๆ ที่เกี่ยวข้อง (C#)
Rating :

 
Page และ Controls หรือ Toolbox เครื่องมือสำหรับออกแบบ UI บน XAML (C#)
Rating :

 
รู้จัก Toolbox และ Controls พื้นฐานเช่น TextBlock , TextBox และ Button (C#)
Rating :

 
AppBar : TopAppBar / BottomAppBar บน Windows Store Apps (C#)
Rating :

 
ListBox แสดงข้อมูลแบบ List และ Combobox บน Windows Store (C#)
Rating :

 
ListView และ GridView บน Windows Store Apps (C#)
Rating :

 
FlipView สร้าง Image FlipView / Image Slide บน Windows Store Apps (C#)
Rating :

 
เล่นไฟล์ Media / Sound เช่น Video Clip / Sound ด้วย MediaElement (C#)
Rating :

 
WebView แสดงผล URL เว็บไซต์ หรือ HTML บน Windows Store Apps (C#)
Rating :

 
เล่นไฟล์ยูทูป บน Windows Store Apps ด้วย MediaElement (C#)
Rating :

 
Context Menus and Popup Menu บน Windows Store Apps (C#)
Rating :

 
Geolocation and Location หาตำแหน่งของ Client ที่ใช้งาน Apps ของเรา (C#)
Rating :

 
Windows Store App และ Progress Bar การสร้าง Progress Bar (C#)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่