Bài 6: Persisting to Windows Phone Storage

Category: Windows Phone Comments: No comments

Trong phần này tôi sẽ cùng các bạn tìm hiểu về những vấn đề sau của Windows Phone:

  • Isolated Storage
  • Updating the Puzzle UI

Adding Assembly References and Other Assets

Project để thực hành cho bài này:
Mục đích của bài này là tôi muốn lưu những trạng thái của người dùng khi chơi. Qua đó lần sau người dùng vào lại APP là có thể tiếp tục chơi, không phải chơi lại từ đầu.
Đầu tiên bạn hãy Tạo 1 Class tên IsolatedStorageHelper.cs
Để tạo 1 class, bạn right-click tại project Trong Solution Explorer, chọn Add, chọn Class, đặt tên là IsolatedStorageHelper.cs và bấm Add.
06-1
Hãy thêm vào 3 thư viện sau trong Class:

using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;

Tạo một interface để Load đối tượng (Object) từ Isolated storage :

public static T GetObject<T>(string key)
{
    return default(T);
}

Hàm Save:

public static void SaveObject<T>(string key, T objectToSave)
{
}

Hàm Delete:

public static void DeleteObject(string key)
{
}

Hàm Serialize và Deserialize:

private static string Serialize(object objectToSerialize)
{
    using (MemoryStream ms = new MemoryStream())
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
        serializer.WriteObject(ms, objectToSerialize);
        ms.Position = 0;
 
        using (StreamReader reader = new StreamReader(ms))
        {
            return reader.ReadToEnd();
        }
    }
}
 
private static T Deserialize<T>(string jsonString)
{
    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
        return (T)serializer.ReadObject(ms);
    }
}

 

Như vậy, tôi đã tạo xong class để thực hiện ý muốn của tôi. Bây giờ tôi sẽ chuyển đến phần xử lý để lưu và và khôi phục trạng thái.

Updating the Puzzle UI

Bạn hãy mở PuzzlePage.xaml và thêm vào dòng code sau:

<Grid x:Name="LayoutRoot" Background="Transparent">
  <StackPanel Orientation="Vertical" VerticalAlignment="Top" Grid.Row="1">
    <Border x:Name="CongratsBorder"...>
      ...
    </Border>
    <Border x:Name="border"...>
      ...
    </Border>
    <TextBlock x:Name="TapToContinueTextBlock" HorizontalAlignment="Center" Text="Tap the picture to start the puzzle" TextWrapping="Wrap" Foreground="#FFD0D0D0" FontSize="17.333"/>
//Thêm từ đoạn này nhé:
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
      <Button x:Name="LoadButton" Content="Load" Margin="10" />
      <Button x:Name="SaveButton" Content="Save" Margin="10" />
      <Button x:Name="ClearStorageButton" Content="Clear" Margin="10" />
//Kết thúc
    </StackPanel>
  </StackPanel>
</Grid>

06-2
bạn sẽ được một giao diện như sau:
06-3
Code xử lý xự kiện:
Nút Load:

private void LoadButton_Click(object sender, RoutedEventArgs e)
{
    var gameState = IsolatedStorageHelper.GetObject<PuzzleState>("PuzzleState");
     //Hàm GetObject sẽ trả về 1 đối tượng kiêu "PuzzleState"
    if (gameState == null)
    {
        MessageBox.Show("Sorry, no game state found.", "Oops!", MessageBoxButton.OK);
    }
    else
    {
        // set game state
        this.game.SetState(gameState);
    }
}

Nút Save:

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
    // save game state
    PuzzleState gameState = this.game.GetState();
    IsolatedStorageHelper.SaveObject("PuzzleState", gameState);
}

Nút Clear:

private void ClearStorageButton_Click(object sender, RoutedEventArgs e)
{
    // remove state and image
    IsolatedStorageHelper.DeleteObject("PuzzleState");
}

Bây giờ hãy quay trở lại file IsolatedStorageHelper.cs tôi viết code xử lý tiếp:
Hàm GetObject của tôi:

public static T GetObject<T>(string key)
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
    {
        string serializedObject = IsolatedStorageSettings.ApplicationSettings[key].ToString();
        return Deserialize<T>(serializedObject);
    }
 
    return default(T);
}

Để ý trong hàm Load ở Page game tôi gọi phương thức GetObject:
06-4
Hàm SaveObject:

public static void SaveObject<T>(string key, T objectToSave)
{
    string serializedObject = Serialize(objectToSave);
    IsolatedStorageSettings.ApplicationSettings[key] = serializedObject;
}

Hàm DeleteObject:

public static void DeleteObject(string key)
{
    IsolatedStorageSettings.ApplicationSettings.Remove(key);
}

Bây giờ bạn hãy chạy APP và thử các chức năng xem thế nào.Chơi thử và bấm Save, sau đó thoát APP và chọn Load xem ? vậy là bạn đã biết cách xử lý lưu trạng thái cho APP của bạn lên ổ cứng. Tôi nghĩ không quá khó nhưng rất hưu ích phải không ?
Bạn cũng có thể ứng dụng để lưu tên người chơi, điểm số cao,…..
Đối với bài này một số người mới tiếp cận Windows Phone sẽ hơi khó hiểu. Vì thế tôi sẽ có 1 bài giới thiệu riêng về các vấn đề sau:

  • App Lifecycle
  • Storage APIs
  • App to App communication and Model-View-ViewModel.

chúc bạn thành công !

magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
sports
sports
fashion
art
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Fitness equipment
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Motor car
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
news
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
technique
transact
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
travel
The recipe
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
outdoors
Reduce weight
science
stretch
training
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
sports
sports
fashion
art
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Fitness equipment
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Motor car
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
news
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
evaluation
technique
transact
business
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
The recipe
travel
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
lifestyle
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
Classic cars
evaluation
technique
transact
Classic cars
technique
transact
Fashion magazine
Fashion show
Female models
fitness
health
lifestyle
Small tools
travel
The recipe
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
catering
health
Lose weight
outdoors
Reduce weight
science
stretch
training
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
business
fashion
fitness
health
news
Small tools
Street fashion
The recipe
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
technique
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
Fitness equipment
fitness
nutrition
Nutritious food
routine
training
video
Yoga
art
fashion
health
music
photography
sports
The recipe
tourism
travel
hygiene
industry
invest
markets
medical
Motor car
Real estate
animation
car
games
Motor car
movies
music
news
Science and technology
tutorial
business
cookbook
fashion
fitness
health
magazine
news
sports
Street fashion
travel
Big data
business
Fashion show
fitness
health
lifestyle
Small tools
The recipe
A bad girl
appetite
attitude
movies
rendezvous
sex
toys
catering
health
outdoors
Reduce weight
science
stretch
training

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>