Windows Phone 7 便捷记事本实例

2011021918280433.jpg

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

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

转载于:https://www.cnblogs.com/linzheng/archive/2011/02/19/1958618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值