这是一个很简单的记事本,利用了本地存储实时记录下你写下的内容,退出程序的时候将自动保存记事本的内容。下面的工具条是放大和缩小字体的功能。

用自定义的QuickNotesSettings类来保存记事本的内容和字体的大小,同时封装了记事本的加载方法和保存方法。

 

 
  
  1. using System;  
  2. using System.IO.IsolatedStorage;  
  3. using System.Windows;  
  4.  
  5. namespace QuickNotes  
  6. {  
  7.     public class QuickNotesSettings  
  8.     {  
  9.         public QuickNotesSettings()  
  10.         {  
  11.             this.Text = "";  
  12.             this.FontSize = (double)Application.Current.Resources["PhoneFontSizeMediumLarge"];  
  13.         }  
  14.  
  15.         public string Text { set; get; }  
  16.         public double FontSize { set; get; }  
  17.         //静态方法获取本地存储的记事本内容  
  18.         public static QuickNotesSettings Load()  
  19.         {  
  20.             IsolatedStorageSettings isoSettings = IsolatedStorageSettings.ApplicationSettings;  
  21.             QuickNotesSettings settings;  
  22.  
  23.             if (!isoSettings.TryGetValue<QuickNotesSettings>("settings", out settings))  
  24.                 settings = new QuickNotesSettings();  
  25.  
  26.             return settings;  
  27.         }  
  28.         //保存到本地存储中  
  29.         public void Save()  
  30.         {  
  31.             IsolatedStorageSettings isoSettings = IsolatedStorageSettings.ApplicationSettings;  
  32.             isoSettings["settings"] = this;//保存的就是这个类的实例  
  33.         }  
  34.     }  

xaml文件

 

 
  
  1. <!--LayoutRoot contains the root grid where all other page content is placed--> 
  2.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  3.         <Grid.RowDefinitions> 
  4.             <RowDefinition Height="Auto"/> 
  5.             <RowDefinition Height="*"/> 
  6.         </Grid.RowDefinitions> 
  7.  
  8.         <!--TitlePanel contains the name of the application and page title--> 
  9.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  10.             <TextBlock x:Name="ApplicationTitle" Text="Quick Notes" Style="{StaticResource PhoneTextNormalStyle}"/> 
  11.         </StackPanel> 
  12.  
  13.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  14.             <TextBox Name="txtbox" 
  15.                      TextWrapping="Wrap" 
  16.                      AcceptsReturn="True" 
  17.                      VerticalScrollBarVisibility="Auto" 
  18.                      TextChanged="OnTextBoxTextChanged" /> 
  19.         </Grid> 
  20.     </Grid> 
  21.       
  22.     <phone:PhoneApplicationPage.ApplicationBar> 
  23.         <shell:ApplicationBar> 
  24.             <!--缩小--> 
  25.             <shell:ApplicationBarIconButton IconUri="/Images/littleletter.icon.png"   
  26.                                             Text="smaller font" 
  27.                                             Click="OnAppBarSmallerFontClick" /> 
  28.             <!--放大--> 
  29.             <shell:ApplicationBarIconButton IconUri="/Images/bigletter.icon.png"   
  30.                                             Text="larger font" 
  31.                                             Click="OnAppBarLargerFontClick" /> 
  32.         </shell:ApplicationBar> 
  33.     </phone:PhoneApplicationPage.ApplicationBar> 

对应的cs后台文件

 

 
  
  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Controls;  
  4. using Microsoft.Phone.Controls;  
  5.  
  6. namespace QuickNotes  
  7. {  
  8.     public partial class MainPage : PhoneApplicationPage  
  9.     {  
  10.         QuickNotesSettings appSettings = (Application.Current as App).AppSettings;  
  11.           
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.  
  16.             txtbox.Text = appSettings.Text;  
  17.             txtbox.FontSize = appSettings.FontSize;  
  18.         }  
  19.         //即时保存记事本的内容到,本地存储中去  
  20.         void OnTextBoxTextChanged(object sender, TextChangedEventArgs args)  
  21.         {  
  22.             appSettings.Text = txtbox.Text;  
  23.         }  
  24.         //缩小字体  
  25.         void OnAppBarSmallerFontClick(object sender, EventArgs args)  
  26.         {  
  27.             txtbox.FontSize = Math.Max(12, txtbox.FontSize - 1);  
  28.             appSettings.FontSize = txtbox.FontSize;  
  29.         }  
  30.         //放大字体  
  31.         void OnAppBarLargerFontClick(object sender, EventArgs args)  
  32.         {  
  33.             txtbox.FontSize = Math.Min(48, txtbox.FontSize + 2);  
  34.             appSettings.FontSize = txtbox.FontSize;  
  35.         }  
  36.     }  

app.xaml.cs主程序文件修改

 

 
  
  1. ……  
  2. public QuickNotesSettings AppSettings { set; get; }  
  3.         public PhoneApplicationFrame RootFrame { get; private set; }  
  4.  
  5.         public App()  
  6.         {  
  7.  
  8.             UnhandledException += Application_UnhandledException;  
  9.             InitializeComponent();  
  10.             InitializePhoneApplication();  
  11.         }  
  12.  
  13.         private void Application_Launching(object sender, LaunchingEventArgs e)  
  14.         {  
  15.             AppSettings = QuickNotesSettings.Load();  
  16.         }  
  17.  
  18.         private void Application_Activated(object sender, ActivatedEventArgs e)  
  19.         {  
  20.             AppSettings = QuickNotesSettings.Load();  
  21.         }  
  22.  
  23.         private void Application_Deactivated(object sender, DeactivatedEventArgs e)  
  24.         {  
  25.             AppSettings.Save();  
  26.         }  
  27.  
  28.         private void Application_Closing(object sender, ClosingEventArgs e)  
  29.         {  
  30.             AppSettings.Save();  
  31.         }  
  32. ……