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 - Roaming Data (Application Data)



Clound SSD Virtual Server

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

Windows Phone 8 and Storage - Roaming Data (Application Data) สำหรับการจัดเก็บข้อมูลแบบ Roaming บน Windows Phone 8 สามารถจัดเก็บข้อมูลประเภทต่าง ๆ ได้เหมือนกับ Local Data เช่น ตัวแปรแบบ Settings , ตัวแปรแบบ Object และไฟล์ประเภทต่าง ๆ ได้ทุกประเภท แต่ Roaming Data จะเป็นการจัดเก็บข้อมูลในรูปแบบของ Cloud Server ซึ่งแบบ Roaming นั้น จะควบคุมจัดการให้ทุกๆเครื่องที่มีผู้ใช้คนเดียวกันมีข้อมูลเหมือนกัน และเรียกใช้ข้อมูลได้เหมือนกันทุกประการ ไม่ว่าจะเรียกใช้จากเครื่องไหนก็ตาม ซึ่งจะแตกต่างกับ Local data ตรงที่ Local จะเก็บเฉพาะเครื่องนั้น ๆ เท่านั้น

เพิ่มเติม การจัดเก็บข้อมูล Roaming นิยมจัดเก็บข้อมูลเล็ก ๆ ที่มีขนาดไม่ใหญ่ เช่นค่า Setting , Preferences และ Customization เพราะถ้าใช้ Roaming เก็บข้อมูลมากจนเกินไป Apps ของเราอาจจะถูกระงับการรับส่งข้อมูลผ่าน Roaming ได้

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

สร้างตัวแปร Settings
Windows.Storage.ApplicationDataContainer roaming = Windows.Storage.ApplicationData.Current.RoamingSettings;

// Simple setting
roamingSettings.Values["exampleSetting"] = "Hello World";

อ่านตัวแปร Settings
Windows.Storage.ApplicationDataContainer roaming = Windows.Storage.ApplicationData.Current.RoamingSettings;

// Simple setting
Object value = roaming.Values["exampleSetting"];


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

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

roaming.Values["exampleCompositeSetting"] = composite;

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

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


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

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

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

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

            // Write text file
            await file.WriteTextAsync(file, "String");

การอ่าน Text file
            // Get the Roaming folder.
            StorageFolder roaming = Windows.Storage.ApplicationData.Current.RoamingFolder;

            if (roaming != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await roaming.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 (Roaming Data)

Windows Phone 8 and Storage - Roaming 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 Roaming Settings.
            Windows.Storage.ApplicationDataContainer roaming = Windows.Storage.ApplicationData.Current.RoamingSettings;

            // Create a Setting.
            roaming.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 Roaming Settings.
            Windows.Storage.ApplicationDataContainer roaming = Windows.Storage.ApplicationData.Current.RoamingSettings;

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

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

        }

 
    }
}


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

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

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

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


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

Windows Phone 8 and Storage - Roaming 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 Roaming folder.
            StorageFolder roaming = Windows.Storage.ApplicationData.Current.RoamingFolder;

            // Create a new folder name DataFolder.
            var dataFolder = await roaming.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 Roaming folder.
            StorageFolder roaming = Windows.Storage.ApplicationData.Current.RoamingFolder;

            if (roaming != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await roaming.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 - Roaming Data (Application Data)

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








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

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

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


   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-07-27 19:05:22 / 2017-03-25 22:34:03
  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 - Local 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 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 อัตราราคา คลิกที่นี่