Wiki


Wiki Table of Contents

Tags

Page Details

Published by:
This page has not yet been rated

Silverlight Tip of the Day #20 – How to Increase your Isolated Storage Quota.

Filed under: [Edit Tags]

 

Mỗi ứng dụng mặc định được 1 MB cho việc lưu trữ dữ liệu trong Isolated Storage nơi mà server có thể lưu trữ những dữ liệu đặc biệt của client trên máy clients. Nhưng điều gì sẽ xảy ra nếu bạn cần không gian lưu trữ nhiều hơn 1 MB? May mắn thay, đối tượng IsolatedStorageFile đã có cung cấp một phương thức gọi là IncreaseQuotaTo(), cho phép 1 server tăng dung lượng lưu trữ dữ liệu trên máy user.

Bạn có thể nhìn thấy danh sách các ứng dụng sử dụng IsolatedStorage trên hộp thoại của bạn. Để làm điều này:

  1. Click chuột phải lên bất kì ứng dụng Silverlight nào và từ menu hiện ra chọn lựa chọn duy nhất : "Silverlight Configuration"

Khi click vào lựa chọn duy nhất ấy, sẽ làm xuất hiện hộp thoại Silverlight Configuration

 

2.      Click vào tab cuối cùng "Application Storage" và bạn sẽ nhìn thấy danh sách của tất cả các websites đang sử dụng Isolated Storage trên máy của bạn.  Không gian lưu trữ hiện thời của mỗi ứng dụng được sử dụng cũng như là dung lượng giới hạn được cho phép cho mỗi ứng dụng cũng được hiển thị trong danh sách này.

 

Còn demo của chúng ta sẽ hiển thị 3 mảng thông tin sau:

 

  1. Không gian đang được sử dụng
  2. Không gian hiện có thể sử dụng
  3. Dung lượng giới hạn hiện có

Tôi đã thêm vào 1 textbox  nơi mà bạn có thể chứa số bytes mà bạn muốn tăng dung lượng giới hạn. Bằng việc click vào button, nó sẽ làm việc cần thiết là gọi IncreaseQuotaTo() để tăng dung lượng giới hạn.

***Chú ý: Bạn cần phải gọi hàm này từ một sự kiện của user thí dụ như button click. Vì lí do bảo mật, việc gọi IncreaseQuotaTo() trực tiếp sẽ tự động được trả về  false nếu user không nhanh chóng thực hiện việc gọi.

Sau đây là cửa sổ của ứng dụng beta mà chúng ta đang tạo:

 

Khi bạn chấp nhận một giá trị và click vào button bạn sẽ nhận được hộp thoại sau:

Nếu bạn click yes, bạn sẽ nhận được kết quả sau:

Nào bây giờ hãy nhìn vào code XAML trong trang Page.xaml mà chúng ta sử dụng để tạo giao diện này:

<UserControl x:Class="IncreaseIsolatedStorage.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="800" Height="600">
<Grid x:Name="LayoutRoot" Background="White">
    <Canvas Canvas.Left="10" Canvas.Top="10">
        <TextBlock Canvas.Left="10" x:Name="SpacedUsed" >Current Spaced Used=</TextBlock>
        <TextBlock Canvas.Left="10" x:Name="SpaceAvaiable" Canvas.Top="20">Current Space Available=</TextBlock>
        <TextBlock Canvas.Left="10" x:Name="CurrentQuota" Canvas.Top="40">Current Quota=</TextBlock>
        <TextBlock Canvas.Left="10" x:Name="NewSpace" Canvas.Top="70">New space (in bytes) to request=</TextBlock>
        <TextBox Canvas.Left="255" Canvas.Top="70" Width="100" x:Name="SpaceRequest"></TextBox>
        <TextBlock Canvas.Left="365" Canvas.Top="70" Width="60">(1048576 = 1 MB)</TextBlock>
        <Button Canvas.Left="10" Content="Increase Storage" Canvas.Top="100" 
                Width="100" Height="50" Click="Button_Click">                
        </Button>
        <TextBlock Canvas.Left="10" Canvas.Top="160" x:Name="Results"></TextBlock>
    </Canvas>
</Grid>
</UserControl>

Và code phía sau của chúng ta:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
 
namespace SilverlightApplication11
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
 
            SetStorageData();
        }
 
        private void SetStorageData()
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                SpacedUsed.Text     = "Current Spaced Used = "+(isf.Quota - isf.AvailableFreeSpace).ToString() +" bytes";
                SpaceAvaiable.Text  = "Current Space Available=" + isf.AvailableFreeSpace.ToString() + " bytes";
                CurrentQuota.Text   = "Current Quota=" + isf.Quota.ToString() + " bytes";
            }
        }
        
        private void IncreaseStorage(long spaceRequest)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                long newSpace = isf.Quota + spaceRequest; 
                try
                {
                    if (true == isf.IncreaseQuotaTo(newSpace))
                    {
                        Results.Text = "Quota successfully increased.";
                    }
                    else
                    {
                        Results.Text = "Quota increase was unsuccessfull.";
                    }
                }
                catch (Exception e)
                {
                    Results.Text = "An error occured: "+e.Message;
                }
                SetStorageData();
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                long spaceRequest = Convert.ToInt64(SpaceRequest.Text);
                IncreaseStorage(spaceRequest);
            }
            catch { // User put bad data in text box }
        }
    }
}

Thank you,
--Mike Snow

 

 

 

Recent Comments

Leave the first comment for this page.