• 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();
    }
    ……
  • 相关阅读:
    windows操作系统的电脑越用越卡?简说几种原因和解决方法。
    不懂电脑也能自己重装系统,可视化傻瓜式一键重装系统不求人!!!
    023.Ubuntu常见个性化配置
    Blazor带我重玩前端(六)
    Blazor带我重玩前端(五)
    WSL2 配置及ubuntu18.04安装mysql8.0+
    用ThreadLocal来优化下代码吧
    基于canal的client-adapter数据同步必读指南
    「从零单排canal 07」 parser模块源码解析
    实用向—总结一些唯一ID生成方式
  • 原文地址:https://www.cnblogs.com/linzheng/p/1958618.html
Copyright © 2020-2023  润新知