摘要:[WPF 筆記] Page 間傳遞資料
//[MainWindow.xaml]
//[MainWindow.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PagePassData
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
//[App.xaml]
//[page1.xaml]
(the answer is .)
Guess
//[page1.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PagePassData
{
///
/// Interaction logic for Page1.xaml
///
public partial class Page1 : Page
{
int answer = (new Random()).Next();
public Page1()
{
InitializeComponent();
answerBox.Text = answer.ToString();
}
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Page2 page2 = new Page2();
page2.Answer = answer;
page2.Guess = int.Parse(guessBox.Text);
NavigationService.Navigate(page2);
}
}
}
//[page2.xaml]
You guessed:
Try Again
//[page2.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PagePassData
{
///
/// Interaction logic for Page2.xaml
///
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
}
int answer;
int guess;
public int Answer
{
get{return answer;}
set{answer=value;}
}
public int Guess
{
get { return guess; }
set { guess = value; }
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
guessblock.Text = Guess.ToString();
if (answer == guess) { resultBox.Text = "You Win!"; }
else if (answer > guess) { resultBox.Text = "lower"; }
else { resultBox.Text = "higher"; }
}
}
}