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 > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Windows Phone 8 and Storage - Local Data (Application Data)



Clound SSD Virtual Server

Windows Phone 8 and Storage - Local Data (Application Data)

Windows Phone 8 and Storage - Local Data (Application Data) ฟีเจอร์ใหม่บน Windows Phone 8 เป็นต้นไปคือมีแหล่งสำหรับการจัดเก็บข้อมูลใหม่คือ Application Data จากเดิมมีแค่ Isolated Storage (จัดเก็บไว้ใน Application เฉพาะ Apps นั้น ๆ ) โดย Application Data นี้คือ โฟเดอร์ที่ Windows Phone ได้จัดเตรียมไว้สำหรับเก็บข้อมูลต่าง ๆ โดยแยกในแต่ล่ะ Application (เปรียบเสมือนบน Windows จะอยู่ใน C:\Users\<your user name>\AppData\) ซึ่ง Application Data จะแตกต่างกับ Isolated Storage ตรงที่ Isolated Storage มีความเป็นส่วนตัวปลอดภัยและเฉพาะ Apps นั้น ๆ เท่านั้น แต่ Application Data เป็นโฟเดอร์ที่รวบรวม Apps ทั้งหมด (เฉพาะเครื่องที่ติดตั้ง Apps) สามารถเข้าใช้งานจาก Application อื่น ๆ ได้ เมื่อมีการกำหนดพวกสิทธิ์ในการเข้าถึงหรือใช้งาน โดยความสามารถของ Application Data Storage คือการจัดเก็บข้อมูล โดยข้อมูลที่จะจัดเก็บบน Apps นั้น สามารถแบ่งได้เป็น 2-3 ประเภท คือ ค่าตัวแปรหรือ Settings หรือข้อมูลประเภทไฟล์ต่าง ๆ ที่เกิดจากการใช้งาน Apps หรือไฟล์ที่ต้องการทำมาใช้กับ Apps เช่นรูปภาพ , ไฟล์มีเดีย , เท็กไฟล์ เป็นต้น

และการจัดเก็บข้อมูลด้วย Application Data บน Windows Phone นั้นจะถูกแบ่งออกเป็น 3 ประเภทใหญ่ ๆ คือ Local Data , Roaming Data และ Temporary Data โดยแต่ล่ะประเภทจะมีวัตถุประสงค์ที่แตกต่างกันไป โดยในหัวข้อนี้จะพูดถึง Local Data ซึ่งจะเป็นการจัดเก็บข้อมูลลงใน Apps โดยตรง และสามารถใช้ได้เฉพาะเครื่อง Smartphone ที่ติดตั้ง Apps นั้น ๆ เท่านั้น รูปแบบการจัดเก็บ เก็บได้ทั้งค่าตัวแปร Setting หรือจะเป็นไฟล์ต่าง ๆ ได้ทุกประเภท

ตัวอย่างการจัดเก็บค่าแบบ Settings

เรียกใช้งาน Class ของ Storage
using Windows.Storage;

สร้างตัวแปร Settings
Windows.Storage.ApplicationDataContainer local = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["exampleSetting"] = "Hello Windows Phone";

อ่านตัวแปร Settings
Windows.Storage.ApplicationDataContainer local = Windows.Storage.ApplicationData.Current.LocalSettings;
Object value = local.Values["exampleSetting"];


ตัวอย่างการจัดเก็บค่าแบบ Object

สร้างตัวแปร Object
Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1;
composite["strVal"] = "string";

localSettings.Values["exampleCompositeSetting"] = composite;

อ่านตัวแปร Object
Windows.Storage.ApplicationDataCompositeValue composite = 
   (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["exampleCompositeSetting"];

if (composite == null)
{
   // No data
}
else
{
   // Access data in composite["intVal"] and composite["strVal"]
}


ตัวอย่างการจัดเก็บแบบ Text file

การจัดเก็บแบบ Text file
    // Get the local folder.
    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    // Create a new folder name DataFolder.
    var dataFolder = await local.CreateFolderAsync("DataFolder",
        CreationCollisionOption.OpenIfExists);

    // Create a new file named DataFile.txt.
    var file = await dataFolder.CreateFileAsync("DataFile.txt",
    CreationCollisionOption.ReplaceExisting);

    // Write strint to Text
   await file.WriteTextAsync(file, "String");

การอ่าน Text file
        // Get the DataFolder folder.
        var dataFolder = await local.GetFolderAsync("DataFolder");

        // Get the file.
        var file = await dataFolder.OpenStreamForReadAsync("DataFile.txt");

        // Read the data.
        using (StreamReader streamReader = new StreamReader(file))
        {
            this.textBlock1.Text = streamReader.ReadToEnd();
        }


ลองมาดูตัวอย่างเพื่อความเข้าใจมากขึ้น








Example 1 สร้างตัวแปร Settings และอ่านตัวแปร Settings (Local Data)

Windows Phone 8 and Storage - Local Data (Application Data)

สร้างหน้าจอสำหรับการ Input ข้อมูล และการ Read / Write ข้อมูล

MainPage.xaml
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
            <TextBox 
                Name="textBox1" 
                HorizontalAlignment="Left"
                Height="72" 
                Margin="0,22,0,0" 
                TextWrapping="Wrap" 
                Text="Enter text here." 
                VerticalAlignment="Top" Width="456"/>
            <Button 
                Name='btnWrite' 
                Content="Write"
                HorizontalAlignment="Left" 
                Margin="10,94,0,0" 
                VerticalAlignment="Top"
                Width="156" 
                Click="btnWrite_Click"/>
            <TextBlock 
                Name="textBlock1" 
                HorizontalAlignment="Left"
                Margin="10,293,0,0" 
                TextWrapping="Wrap" Text=""
                VerticalAlignment="Top" 
                Height="61" 
                Width="436"/>
            <Button 
                Name="btnRead" 
                Content="Read"
                HorizontalAlignment="Left" 
                Margin="10,374,0,0" 
                VerticalAlignment="Top"
                Width="156" 
                IsEnabled="False"
                Click="btnRead_Click"/>
        </Grid>

MainPage.xaml.cs
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;


namespace myPhoneApp
{

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }


        private async void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            WriteToSettings();

            // Update UI.
            this.btnWrite.IsEnabled = false;
            this.btnRead.IsEnabled = true;
        }

        private void WriteToSettings()
        {

            // Get the local Settings.
            Windows.Storage.ApplicationDataContainer local = Windows.Storage.ApplicationData.Current.LocalSettings;

            // Create a Setting.
            local.Values["exampleSetting"] = this.textBox1.Text;

        }


        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            ReadSettings();

            // Update UI.
            this.btnWrite.IsEnabled = true;
            this.btnRead.IsEnabled = false;
        }

        private void ReadSettings()
        {

            // Get the local Settings.
            Windows.Storage.ApplicationDataContainer local = Windows.Storage.ApplicationData.Current.LocalSettings;

            Object value = local.Values["exampleSetting"];

            if (value != null)
            {
                this.textBlock1.Text = value.ToString();
            }

        }

 
    }
}


Windows Phone 8 and Storage - Local Data (Application Data)

ทดสอบการ Input ข้อมูลและการ Write ข้อมูล

Windows Phone 8 and Storage - Local Data (Application Data)

ทดสอบอ่าน Read ข้อมูล



Example 2 สร้าง Text file และการอ่าน Text file (Local Data)

Windows Phone 8 and Storage - Local Data (Application Data)

สร้างหน้าจอสำหรับการ Input ข้อมูล และการ Read / Write ข้อมูลลงใน Text file

MainPage.xaml
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
            <TextBox 
                Name="textBox1" 
                HorizontalAlignment="Left"
                Height="72" 
                Margin="0,22,0,0" 
                TextWrapping="Wrap" 
                Text="Enter text here." 
                VerticalAlignment="Top" Width="456"/>
            <Button 
                Name='btnWrite' 
                Content="Write"
                HorizontalAlignment="Left" 
                Margin="10,94,0,0" 
                VerticalAlignment="Top"
                Width="156" 
                Click="btnWrite_Click"/>
            <TextBlock 
                Name="textBlock1" 
                HorizontalAlignment="Left"
                Margin="10,293,0,0" 
                TextWrapping="Wrap" Text=""
                VerticalAlignment="Top" 
                Height="61" 
                Width="436"/>
            <Button 
                Name="btnRead" 
                Content="Read"
                HorizontalAlignment="Left" 
                Margin="10,374,0,0" 
                VerticalAlignment="Top"
                Width="156" 
                IsEnabled="False"
                Click="btnRead_Click"/>
        </Grid>









MainPage.xaml.cs
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;


namespace myPhoneApp
{

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }


        private async void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            await WriteToFile();

            // Update UI.
            this.btnWrite.IsEnabled = false;
            this.btnRead.IsEnabled = true;
        }

        private async Task WriteToFile()
        {
            // Get the text data from the textbox. 
            byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(this.textBox1.Text.ToCharArray());

            // Get the local folder.
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

            // Create a new folder name DataFolder.
            var dataFolder = await local.CreateFolderAsync("DataFolder",
                CreationCollisionOption.OpenIfExists);

            // Create a new file named DataFile.txt.
            var file = await dataFolder.CreateFileAsync("DataFile.txt",
            CreationCollisionOption.ReplaceExisting);

            // Write the data from the textbox.
            using (var s = await file.OpenStreamForWriteAsync())
            {
                s.Write(fileBytes, 0, fileBytes.Length);
            }
        }


        private async void btnRead_Click(object sender, RoutedEventArgs e)
        {
            await ReadFile();

            // Update UI.
            this.btnWrite.IsEnabled = true;
            this.btnRead.IsEnabled = false;
        }

        private async Task ReadFile()
        {
            // Get the local folder.
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

            if (local != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await local.GetFolderAsync("DataFolder");

                // Get the file.
                var file = await dataFolder.OpenStreamForReadAsync("DataFile.txt");

                // Read the data.
                using (StreamReader streamReader = new StreamReader(file))
                {
                    this.textBlock1.Text = streamReader.ReadToEnd();
                }

            }
        }

 
    }
}


Windows Phone 8 and Storage - Local Data (Application Data)

ทดสอบการ Input ข้อมูลและสร้าง Text file

Windows Phone 8 and Storage - Local Data (Application Data)

แสดงค่าใน Text file ที่ได้จากการจัดเก็บไว้ใน Local Data

อ่านเพิ่มเติม


   
Share


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


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


   


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

 
Windows Phone 8 and Isolated Storage Tools
Rating :

 
Windows Phone 8 and Isolated Storage (Settings , Files and folders)
Rating :

 
Windows Phone 8 and Storage - Roaming Data (Application Data)
Rating :

 
Windows Phone 8 and Storage - Temporary Data (Application Data)
Rating :

 
Windows Phone 8 and Storage - Encrypt Data for Windows Phone
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 04
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 อัตราราคา คลิกที่นี่