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 Isolated Storage (Settings , Files and folders)



Clound SSD Virtual Server

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

Windows Phone 8 and Isolated Storage (Settings , Files and folders) ในการเขียนโปรแกรมบน Windows Phone 8 เพื่อติดต่อกับ Data Storage ที่อยู่ใน Isolated Storage ซึ่งเป็น Storage ที่ถูกสร้างขึ้นสำหรับแต่ล่ะ Application เราสามารถเรียกใช้งานความสามารถในการจัดเก็บข้อมูลได้ 3 รูปแบบคือ Settings (เป็นเหมือนพวกค่าตัวแปร) , Files and folders (จัดเก็บพวกไฟล์ต่าง ๆ เช่น text , media) และ Relational data (จัดเก็บในรูปแบบ Database) โดยทั้ง 3 ประเภทนี้ค่อนข้างครบครันสำหรับการเขียนโปรแกรม และแต่ล่ะแบบหน้าที่ประประโยชน์การใช้งานแตกต่างกันไป

ในการเรียกใช้งาน Isolated Storage นั้นเราสามารถเรียกใช้งานมันได้ทันที โดยไม่จำเป็นจะต้องมีการเรียกใช้งาน Class หรือ Library อื่น ๆ ภายนอก และการใช้งานก็ค่อนข้างง่าย มีความปลอดภัยต่อข้อมูลที่จะถูกสร้างขึ้น และในบทความนี้จะยกตัวอย่างการใช้งานเกี่ยวกับ Settings และ Files and folders แบบง่าย ๆ พอที่จะเป็นตัวอย่างในการนำไปใช้

อ่านเพิ่มเติม : Windows Phone 8 and Isolated Storage Tools


Example 1 การใช้งาน Isolated Storage Settings เพื่อจัดเก็บค่าตัวแปร Settings

เรียกใช้งาน Class ของ IsolatedStorage
using System.IO.IsolatedStorage;

Syntax การสร้าง (Create) ตัวแปร Settings
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (!settings.Contains("userData"))
    {
        settings.Add("userData", txtInput.Text);
    }
    else
    {
        settings["userData"] = txtInput.Text;
    }
    settings.Save();

Syntax การอ่าน (Read) ตัวแปร Settings
    if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
    {
        txtDisplay.Text =  IsolatedStorageSettings.ApplicationSettings["userData"] as string;
    }

Syntax การลบ (Remove) ตัวแปร Settings
    if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
    {
        IsolatedStorageSettings.ApplicationSettings.Remove("userData");
    }


ตัวอย่าง

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

ออกแบบหน้าจอเพื่อทดสอบการจัดเก็บค่าตัวแปร Settings

MainPage.xaml
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            
            <TextBox Height="72" HorizontalAlignment="Left" Margin="12,6,0,0" Name="txtInput" 
                Text="INPUT DATA" VerticalAlignment="Top" Width="438" />
            
            <Button Content="Save Data" Height="72" HorizontalAlignment="Left" Margin="125,84,0,0" 
                Name="btnSave" VerticalAlignment="Top" Width="216" Click="btnSave_Click" />
            
            <Button Content="Display Data" Height="72" HorizontalAlignment="Left" Margin="125,174,0,0" 
                Name="btnDisplay" VerticalAlignment="Top" Width="216" Click="btnDisplay_Click" />
            
            <Button Content="Remove Data" Height="72" HorizontalAlignment="Left" Margin="125,266,0,0" 
                Name="btnRemove" VerticalAlignment="Top" Width="216" Click="btnRemove_Click" />

            <TextBlock Height="60" HorizontalAlignment="Left" Margin="25,422,0,0" Name="txtDisplay" 
                Text="USER DATA:" VerticalAlignment="Top" Width="395" />
        </Grid>









MainPage.xaml.cs
using System.IO.IsolatedStorage;


namespace myPhoneApp
{

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


        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            // txtInput is a TextBox defined in XAML.
            if (!settings.Contains("userData"))
            {
                settings.Add("userData", txtInput.Text);
            }
            else
            {
                settings["userData"] = txtInput.Text;
            }
            settings.Save();
        }


        private void btnDisplay_Click(object sender, RoutedEventArgs e)
        {
            // txtDisplay is a TextBlock defined in XAML.
            txtDisplay.Text = "USER DATA: ";
            if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
            {
                txtDisplay.Text +=
                IsolatedStorageSettings.ApplicationSettings["userData"] as string;
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
            {
                IsolatedStorageSettings.ApplicationSettings.Remove("userData");
            }
        }

    }
}


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

ทดสอบการทำงาน

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

กรอกค่าลงใน Input และคลิกเลือก Save Data

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

ทดสอบการ Display Data

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

ทดสอบการ Remove Data


Example 2 การใช้งาน Isolated Storage เพื่อจัดเก็บ File และ Folder

เรียกใช้งาน Class ของ IsolatedStorage
using System.IO.IsolatedStorage;

Syntax การสร้าง (Create) ไฟล์และเขียนข้อมูลลงไฟล์ และจัดเก็บลงใน Isolated Storage
            private string strFileName = "thaicreate.txt";

            // Create Isolated StorageFile
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // Create File Name
            if (!isoStore.FileExists(strFileName))
            {
                IsolatedStorageFileStream dataFile = isoStore.CreateFile(strFileName);
            }

            // Write text file
            StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream(strFileName, FileMode.Append, isoStore));
            sw.WriteLine(this.textBox1.Text);
            sw.Close();

Syntax การอ่าน (Read) ไฟล์ที่อยู่ใน Isolated Storage
            private string strFileName = "thaicreate.txt";

            // Create Isolated StorageFile
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // Read text file
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream(strFileName, FileMode.Open, isoStore));
            string rawData = reader.ReadToEnd();
            reader.Close();


ตัวอย่าง

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

ออกแบบหน้าจอเพื่อทดสอบการรับค่าและเขียนลงใน Text file พร้อมกับการจัดเก็บลงใน Isolated Storage

MainPage.xaml
        <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 System.IO.IsolatedStorage;


namespace myPhoneApp
{

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

        private string strFileName = "thaicreate.txt";

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

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

        private void WriteToFile()
        {

            // Create Isolated StorageFile
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // Create File Name
            if (!isoStore.FileExists(strFileName))
            {
                IsolatedStorageFileStream dataFile = isoStore.CreateFile(strFileName);
            }

            // Write text file
            StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream(strFileName, FileMode.Append, isoStore));
            sw.WriteLine(this.textBox1.Text);
            sw.Close();

        }


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

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

        private void ReadFile()
        {

            // Create Isolated StorageFile
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // Read text file
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream(strFileName, FileMode.Open, isoStore));
            string rawData = reader.ReadToEnd();
            reader.Close();

            // Display text
            this.textBlock1.Text = rawData;
        }

 
    }
}


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

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

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

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

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

เมื่อใช้ Isolated Storage Tools เพื่อเข้าไปดูใน Isolated Storage จะเห็นไฟล์ที่ถูกสร้างขึ้น








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


   
Share


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


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


   


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

 
Windows Phone 8 and Isolated Storage Tools
Rating :

 
Windows Phone 8 and Storage - Local Data (Application Data)
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 03
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 อัตราราคา คลิกที่นี่