using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace ApplicationLifecycle
{
public partial class MainPage : PhoneApplicationPage
{
//构造
public MainPage()
{
InitializeComponent();
}
private void Date_BindingValidationError(object sender, ValidationErrorEventArgs e)
{
e.Handled = true;
MessageBox.Show("Invalid date value.\nPlease try again", "Invalid date value or format", MessageBoxButton.OK);
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
//导航到页面SecondPage.xaml
NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Utils.ClearTravelReport(((App.Current.RootVisual as PhoneApplicationFrame).DataContext as TravelReportInfo));
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
//询问是否保存当前的数据
MessageBoxResult res = MessageBox.Show("Do you want to save your work before?", "You are exiting the application", MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
Utils.SaveTravelReport((App.Current.RootVisual as PhoneApplicationFrame).DataContext as TravelReportInfo,
"TravelReportInfo.dat", true);
else
Utils.ClearTravelReport((App.Current.RootVisual as PhoneApplicationFrame).DataContext as TravelReportInfo);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//追中导航的时间
Utils.Trace("Navigated To MainPage");
//检查页面的状态是否保存
if (State.ContainsKey("FocusedElement"))
{
Control focusedElement = this.FindName(State["FocusedElement"] as string) as Control;
if (null != focusedElement)
focusedElement.Focus();
}
TravelReportInfo travelReportInfo = ((App.Current.RootVisual as PhoneApplicationFrame).DataContext as TravelReportInfo);
if (State.ContainsKey("txtDestination"))
travelReportInfo.Destination = State["txtDestination"] as string;
if (State.ContainsKey("txtJustification"))
travelReportInfo.Justification = State["txtJustification"] as string;
if (State.ContainsKey("txtToDate"))
travelReportInfo.FirstDay = DateTime.Parse(State["txtToDate"] as string);
if (State.ContainsKey("txtFromDate"))
travelReportInfo.LastDay = DateTime.Parse(State["txtFromDate"] as string);
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
Utils.Trace("Navigated From MainPage");
//移走前一次的事件操作
if (State.ContainsKey("FocusedElement"))
State.Remove("FocusedElement");
//如果有焦点输入保存这个页面的状态
object obj = FocusManager.GetFocusedElement();
if (null != obj)
{
string focusedControl = (obj as FrameworkElement).Name;
State.Add("FocusedElement", focusedControl);
}
if (State.ContainsKey("txtDestination"))
State.Remove("txtDestination");
State.Add("txtDestination", txtDestination.Text);
if (State.ContainsKey("txtJustification"))
State.Remove("txtJustification");
State.Add("txtJustification", txtJustification.Text);
if (State.ContainsKey("txtFromDate"))
State.Remove("txtFromDate");
State.Add("txtFromDate", txtFromDate.Text);
if (State.ContainsKey("txtToDate"))
State.Remove("txtToDate");
State.Add("txtToDate", txtToDate.Text);
base.OnNavigatedFrom(e);
}
}
}
SecondPage.xaml
View Code
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace ApplicationLifecycle
{
public partial class SecondPage : PhoneApplicationPage
{
public SecondPage()
{
InitializeComponent();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
Utils.SaveTravelReport(
(App.Current.RootVisual as PhoneApplicationFrame).DataContext as TravelReportInfo,
"TravelReportInfo.dat",
false);
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Utils.ClearTravelReport(((App.Current.RootVisual as PhoneApplicationFrame).DataContext as TravelReportInfo));
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Utils.Trace("Navigated To SecondPage");
if (State.ContainsKey("FocusedElement"))
{
Control focusedElement = this.FindName(State["FocusedElement"] as string) as Control;
if (null != focusedElement)
focusedElement.Focus();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
Utils.Trace("Navigated From MainPage");
if (State.ContainsKey("FocusedElement"))
State.Remove("FocusedElement");
object obj = FocusManager.GetFocusedElement();
if (null != obj)
{
string focusedControl = (obj as FrameworkElement).Name;
State.Add("FocusedElement", focusedControl);
}
base.OnNavigatedFrom(e);
}
}
}
? DateTimeToStringConverter用来实现旅行开始和结束日期与用户界面(UI)空间之间的绑定,同时完成必需的SL数据转换。
? TravelReportInfo是一个模型类,用来表示一个旅程;它包含了描述旅行的数据域
? Utils正如它的命名一样,是一个泛型类,它被整个工程中各种各样的函数广泛使用。