保存、讀取、刪除應(yīng)用數(shù)據(jù)

2018-08-12 21:55 更新

保存、讀取、刪除應(yīng)用數(shù)據(jù)

在前面的幾節(jié)中,都是關(guān)于數(shù)據(jù)的,這方面的內(nèi)容其實還有很多很多,省略掉一部分后,也還是有很多。這一節(jié)是很重要的一部分,它關(guān)于如何保存和讀取數(shù)據(jù)。

先來看看一個大概的背景吧,我這里寫的很簡單啦。

保存的內(nèi)容就是這四個框框里填寫的數(shù)據(jù)咯。先上 XAML 代碼。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Orientation="Vertical">
<Rectangle Name="recRed" Width="200" Height="200" Fill="Red"/>
<Rectangle Name="recGreen" Width="100" Height="400" Fill="Green"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<TextBlock Text="紅色矩形的長" FontSize="25" Margin="12" Width="150" Height="50" />
<TextBlock Text="紅色矩形的寬" FontSize="25" Margin="12" Width="150" Height="50" />
<TextBlock Text="綠色矩形的長" FontSize="25" Margin="12" Width="150" Height="50" />
<TextBlock Text="綠色矩形的寬" FontSize="25" Margin="12" Width="150" Height="50"  />
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBox Name="tBoxRedHeight" FontSize="25" Width="150" Height="50" Margin="12"/>
<TextBox Name="tBoxRedWidth" FontSize="25" Width="150" Height="50"  Margin="12"/>
<TextBox Name="tBoxGreenHeight" FontSize="25" Width="150" Height="50"  Margin="12" />
<TextBox Name="tBoxGreenWidth" FontSize="25" Width="150" Height="50"  Margin="12" />
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Width="150" Height="70" Name="btnSaveAppData" Click="btnSaveAppData_Click" Content="保存應(yīng)用數(shù)據(jù)"/>
<Button Width="150" Height="70" Name="btnReadAppData" Click="btnReadAppData_Click" Content="讀取應(yīng)用數(shù)據(jù)"/>
</StackPanel>
</StackPanel>
</StackPanel>  
</Grid>

單個設(shè)置

先來看看單個設(shè)置唄,下面就是代碼咯。

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
public MainPage()
{
     this.InitializeComponent();
}
private void btnSaveAppData_Click(object sender, RoutedEventArgs e)
{                              
    localSettings.Values["RectangleRedHeight"] = tBoxRedHeight.Text;
    localSettings.Values["RectangleRedWidth"] = tBoxRedWidth.Text;
    localSettings.Values["RectangleGreenHeight"] = tBoxGreenHeight.Text;
    localSettings.Values["RectangleGreenWidth"] = tBoxGreenWidth.Text;
}
private void btnReadAppData_Click(object sender, RoutedEventArgs e)
{
     Object objRectangleRedHeight = localSettings.Values["RectangleRedHeight"];
     Object objRectangleRedWidth = localSettings.Values["RectangleRedWidth"];
     Object objRectangleGreenHeight = localSettings.Values["RectangleGreenHeight"];
     Object objRectangleGreenWidth = localSettings.Values["RectangleGreenWidth"];
     recRed.Height = double.Parse(objRectangleRedHeight.ToString());  
     recRed.Width = double.Parse(objRectangleRedWidth.ToString());          
     recGreen.Height = double.Parse(objRectangleGreenHeight.ToString());
     recGreen.Width = double.Parse(objRectangleGreenWidth.ToString());            
}

首先定義了兩個全局變量,如果看過前面幾篇文章,這個應(yīng)該就非常清楚了。顧名思義,第一個是用來保存本地設(shè)置的,第二個則是用來訪問本地文件夾的。這里是單個設(shè)置地進行保存的,后面還有 2 種方式。那么就來調(diào)試吧,注意在點擊了保存數(shù)據(jù)按鈕之后把 App 關(guān)掉哦,關(guān)掉之后再加載,這樣才算是保存了應(yīng)用數(shù)據(jù)嘛,你說對不對呢?

以下就是我的測試結(jié)果了。

復合設(shè)置

我們的設(shè)計都不用變,后臺代碼修改如下。

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
  public MainPage()
    {
      this.InitializeComponent();
    }
 private void btnSaveAppData_Click(object sender, RoutedEventArgs e)
    {
      Windows.Storage.ApplicationDataCompositeValue compositeSettings = new ApplicationDataCompositeValue();
      compositeSettings["RectangleRedHeight"] = tBoxRedHeight.Text;
      compositeSettings["RectangleRedWidth"] = tBoxRedWidth.Text;
      compositeSettings["RectangleGreenHeight"] = tBoxGreenHeight.Text;
      compositeSettings["RectangleGreenWidth"] = tBoxGreenWidth.Text;               
      localSettings.Values["RectangleSettings"] = compositeSettings;     
    }
 private async void btnReadAppData_Click(object sender, RoutedEventArgs e)
    {
      Windows.Storage.ApplicationDataCompositeValue compositeSettings =  
        (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["RectangleSettings"];
      if (compositeSettings == null)
        {
          Windows.UI.Popups.MessageDialog messageDialog =   
          new Windows.UI.Popups.MessageDialog("你好像沒有保存任何應(yīng)用數(shù)據(jù)哦!");
          await messageDialog.ShowAsync();
        }
      else
        {
          recRed.Height = double.Parse(compositeSettings["RectangleRedHeight"].ToString());
          recRed.Width = double.Parse(compositeSettings["RectangleRedWidth"].ToString());
          recGreen.Height = double.Parse(compositeSettings["RectangleGreenHeight"].ToString());
          recGreen.Width = double.Parse(compositeSettings["RectangleGreenWidth"].ToString());                 
        }  
    }

使用 ApplicationDataCompositeValue 會創(chuàng)建一個復合設(shè)置,通過代碼所示方式即可添加數(shù)據(jù),最后會將其添加到 localSettings中。

讀取數(shù)據(jù)的時候,同樣是先在 localSettings 中通過鍵值對的方式來取出這個復合設(shè)置。如果該設(shè)置為空,就會調(diào)用 MessageDialog 控件彈窗通知沒有保存數(shù)據(jù)。對于這個控件,可以訪問這里:【萬里征程——Windows App開發(fā)】控件大集合2。如果復合設(shè)置存在則將他們分別進行類型轉(zhuǎn)換后復制給相應(yīng)的矩形的屬性。

在容器中存放數(shù)據(jù)

在容器存放數(shù)據(jù)其實也就這么回事啦,無非就是先創(chuàng)建一個容器,然后如果創(chuàng)建成功了,就在其中添加相應(yīng)的數(shù)據(jù)即可。

至于加載數(shù)據(jù),在這里我使用了一個 bool 變量來檢查容器是不是已經(jīng)創(chuàng)建好了,如果創(chuàng)建好了就可以將相應(yīng)的數(shù)據(jù)取出然后賦值了,如果沒有的話則一樣挑出彈窗。

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
  public MainPage()
    {
      this.InitializeComponent();
    }
  private void btnSaveAppData_Click(object sender, RoutedEventArgs e)
    {    
      Windows.Storage.ApplicationDataContainer containerSettings =
      localSettings.CreateContainer("RecSettingsContainer", Windows.Storage.ApplicationDataCreateDisposition.Always);
    if (localSettings.Containers.ContainsKey("RecSettingsContainer"))
      {
        localSettings.Containers["RecSettingsContainer"].Values["RectangleRedHeight"] = tBoxRedHeight.Text;
        localSettings.Containers["RecSettingsContainer"].Values["RectangleRedWidth"] = tBoxRedWidth.Text;
        localSettings.Containers["RecSettingsContainer"].Values["RectangleGreenHeight"] = tBoxGreenHeight.Text;
        localSettings.Containers["RecSettingsContainer"].Values["RectangleGreenWidth"] = tBoxGreenWidth.Text;
       }
     }
  private async void btnReadAppData_Click(object sender, RoutedEventArgs e)
    {      
      bool hasContainerSettings = localSettings.Containers.ContainsKey("RecSettingsContainer");
    if(hasContainerSettings)
       {       
         recRed.Height = double.Parse(localSettings.Container  
         ["RecSettingsContainer"].Values["RectangleRedHeight"].ToString());
         recRed.Width = double.Parse(localSettings.Containers  
         ["RecSettingsContainer"].Values["RectangleRedWidth"].ToString());
         recGreen.Height = double.Parse(localSettings.Containers  
         ["RecSettingsContainer"].Values["RectangleGreenHeight"].ToString());
         recGreen.Width = double.Parse(localSettings.Containers  
         ["RecSettingsContainer"].Values["RectangleGreenWidth"].ToString());
       }
    else
       {
         Windows.UI.Popups.MessageDialog messageDialog =
         new Windows.UI.Popups.MessageDialog("你好像沒有保存任何應(yīng)用數(shù)據(jù)哦!");
         await messageDialog.ShowAsync();
        }         
      }

接下來就來個運行的截圖咯,還有彈框的截圖。

刪除數(shù)據(jù)

1.對于單個設(shè)置和復合設(shè)置

localSettings.Values.Remove("compositeSettings");

2.對于復合數(shù)據(jù)

localSettings.DeleteContainer("containerSettings");

刪除并不難,或者說,這一節(jié)都不難。有了這些,我們在做游戲的時候,就可以將用戶對游戲的設(shè)置都保存下來啦。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號