Windows Phone 7 便捷記事本執行個體

來源:互聯網
上載者:User

這是一個很簡單的記事本,利用了本機存放區即時記錄下你寫下的內容,退出程式的時候將自動儲存記事本的內容。下面的工具條是放大和縮小字型的功能。

用自訂的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();
}
……
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.