這是一個很簡單的記事本,利用了本機存放區即時記錄下你寫下的內容,退出程式的時候將自動儲存記事本的內容。下面的工具條是放大和縮小字型的功能。
用自訂的QuickNotesSettings類來儲存記事本的內容和字型的大小,同時封裝了記事本的載入方法和儲存方法。
using System;
using System.IO.IsolatedStorage;
using System.Windows;
namespace QuickNotes
{
public class QuickNotesSettings
{
public QuickNotesSettings()
{
this.Text = "";
this.FontSize = (double)Application.Current.Resources["PhoneFontSizeMediumLarge"];
}
public string Text { set; get; }
public double FontSize { set; get; }
//靜態方法擷取本機存放區的記事本內容
public static QuickNotesSettings Load()
{
IsolatedStorageSettings isoSettings = IsolatedStorageSettings.ApplicationSettings;
QuickNotesSettings settings;
if (!isoSettings.TryGetValue<QuickNotesSettings>("settings", out settings))
settings = new QuickNotesSettings();
return settings;
}
//儲存到本機存放區中
public void Save()
{
IsolatedStorageSettings isoSettings = IsolatedStorageSettings.ApplicationSettings;
isoSettings["settings"] = this;//儲存的就是這個類的執行個體
}
}
}
xaml檔案
<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Quick Notes" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Name="txtbox"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Auto"
TextChanged="OnTextBoxTextChanged" />
</Grid>
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar>
<!--縮小-->
<shell:ApplicationBarIconButton IconUri="/Images/littleletter.icon.png"
Text="smaller font"
Click="OnAppBarSmallerFontClick" />
<!--放大-->
<shell:ApplicationBarIconButton IconUri="/Images/bigletter.icon.png"
Text="larger font"
Click="OnAppBarLargerFontClick" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
對應的cs後台檔案
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
namespace QuickNotes
{
public partial class MainPage : PhoneApplicationPage
{
QuickNotesSettings appSettings = (Application.Current as App).AppSettings;
public MainPage()
{
InitializeComponent();
txtbox.Text = appSettings.Text;
txtbox.FontSize = appSettings.FontSize;
}
//即時儲存記事本的內容到,本機存放區中去
void OnTextBoxTextChanged(object sender, TextChangedEventArgs args)
{
appSettings.Text = txtbox.Text;
}
//縮小字型
void OnAppBarSmallerFontClick(object sender, EventArgs args)
{
txtbox.FontSize = Math.Max(12, txtbox.FontSize - 1);
appSettings.FontSize = txtbox.FontSize;
}
//放大字型
void OnAppBarLargerFontClick(object sender, EventArgs args)
{
txtbox.FontSize = Math.Min(48, txtbox.FontSize + 2);
appSettings.FontSize = txtbox.FontSize;
}
}
}
app.xaml.cs主程式檔案修改
……
public QuickNotesSettings AppSettings { set; get; }
public PhoneApplicationFrame RootFrame { get; private set; }
public App()
{
UnhandledException += Application_UnhandledException;
InitializeComponent();
InitializePhoneApplication();
}
private void Application_Launching(object sender, LaunchingEventArgs e)
{
AppSettings = QuickNotesSettings.Load();
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
AppSettings = QuickNotesSettings.Load();
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
AppSettings.Save();
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
AppSettings.Save();
}
……