2010년 5월 9일 일요일

Silverlight Session 기능

IsolatedStorageSettings 클래스

 

격리된 저장소에 키-값 쌍을 저장하는 Dictionary<TKey, TValue>를 제공합니다.

 

설명 :

IsolatedStorageSettings를 이용하면 로컬 IsolatedStorageFile에 사용자별 데이터를 키-값 쌍으로 간편하게 저장할 수 있습니다. 이는 일반적으로 각 페이지에 표시할 이미지 수, 페이지 레이아웃 옵션 등의 설정을 저장하는 데 사용됩니다.

사용자 설정은 특정 응용 프로그램과 관련될 수도 있고 동일한 도메인의 여러 응용 프로그램에서 공유될 수도 있습니다. ApplicationSettings는 응용 프로그램별, 컴퓨터별 및 사용자 설정별로 저장되며, 그 범위는 응용 프로그램 .xap 파일의 전체 경로를 통해 확인됩니다. SiteSettings는 도메인별, 컴퓨터별 및 사용자 설정별로 저장되며, 그 범위는 응용 프로그램 .xap 파일을 호스팅하는 하위 도메인을 통해 확인됩니다. 예를 들어 http://www.contoso.com/site1/application.xap의 응용 프로그램은 http://www.contoso.com/site2/application.xap의 응용 프로그램과 다른 ApplicationSettings를 갖지만 두 응용 프로그램은 모두 동일한 하위 도메인에서 호스팅되므로 동일한 SiteSettings를 공유합니다.

 

예제:

 

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.IO.IsolatedStorage;

namespace IsolatedStorageSample
{
    public partial class Page : UserControl
    {
        private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

        public Page()
        {
            InitializeComponent();

            // Retrieve and set user name.
            try
            {
                string name = (string)userSettings["name"];
                tbGreeting.Text = "Hello, " + name;
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                // No preference is saved.
                tbGreeting.Text = "Hello, World";
            }
        }

        private void btnAddName_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                userSettings.Add("name", tbName.Text);
                tbResults.Text = "Name saved. Refresh page to see changes.";
            }
            catch (ArgumentException ex)
            {
                tbResults.Text = ex.Message;
            }
        }

        private void btnChangeName_Click(object sender, RoutedEventArgs e)
        {
            userSettings["name"] = tbName.Text;
            tbResults.Text = "Name changed. Refresh page to see changes.";
        }

        private void btnRemoveName_Click(object sender, RoutedEventArgs e)
        {
            if (userSettings.Remove("name") == true)
            {
                tbResults.Text = "Name removed. Refresh page to see changes.";
            }
            else
            {
                tbResults.Text = "Name could not be removed. Key does not exist.";
            }

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            userSettings.Clear();
            tbResults.Text = "Settings cleared. Refresh page to see changes.";
        }

        private void btnCount_Click(object sender, RoutedEventArgs e)
        {
            tbResults.Text = "Count: " + userSettings.Count();
        }

        private void btnKeys_Click(object sender, RoutedEventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("Keys: ");

            foreach (string k in userSettings.Keys)
            {
                sb.Append(k + "; ");
            }
            tbResults.Text = sb.ToString();
        }

        private void btnValues_Click(object sender, RoutedEventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("Values: ");

            foreach (Object v in userSettings.Values)
            {
                sb.Append(v.ToString() + "; ");
            }
            tbResults.Text = sb.ToString();
        }
    }
}

댓글 없음:

댓글 쓰기