Windows Phone 7 便捷记事本实例

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

用自定义的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();
}
……
复制代码

posted on   linzheng  阅读(1419)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

统计

点击右上角即可分享
微信分享提示