App.xaml
<Application x:Class="WPF属性和资源.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Page1.xaml" Startup="Application_Startup_1">
<Application.Resources>
<SolidColorBrush x:Key="BackgroundColor">PowderBlue</SolidColorBrush>
</Application.Resources>
</Application>
App.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WPF属性和资源
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
private void Application_Startup_1(object sender, StartupEventArgs e)
{
this.Properties["userInfo"] = new User();
}
}
public class User
{
public string UserName;
public string Email;
}
}
Page1.xaml
<Page x:Class="WPF属性和资源.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="主页面" WindowTitle="主页面">
<Grid>
<Button x:Name="ButtonView" Content="查看" HorizontalAlignment="Left" Margin="118,219,0,0" VerticalAlignment="Top" Width="75" Click="ButtonView_Click"/>
<Label Content="用户名:" HorizontalAlignment="Left" Margin="62,97,0,0" VerticalAlignment="Top"/>
<Label Content="Email:" HorizontalAlignment="Left" Margin="62,143,0,0" VerticalAlignment="Top"/>
<Label x:Name="LabelMsg" Content="用户信息" HorizontalAlignment="Left" Margin="127,57,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="textBoxUserName" HorizontalAlignment="Left" Height="23" Margin="127,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="textBoxEmail" HorizontalAlignment="Left" Height="23" Margin="127,146,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
Page1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPF属性和资源
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
SolidColorBrush brush1 = (SolidColorBrush)Application.Current.TryFindResource("BackgroundColor");
this.Background = brush1;
}
private void ButtonView_Click(object sender, RoutedEventArgs e)
{
if (this.textBoxUserName.Text == "")
{
this.LabelMsg.Content = "请输入用户名!!";
return;
}
if (this.textBoxEmail.Text == "")
{
this.LabelMsg.Content = "请输入邮箱地址!!";
return;
}
User user = (User)Application.Current.Properties["userInfo"];
user.UserName = this.textBoxUserName.Text;
user.Email = this.textBoxEmail.Text;
Page2 page = new Page2();
this.NavigationService.Navigate(page);
}
}
}
Page2.xaml
<Page x:Class="WPF属性和资源.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="查看用户信息" WindowTitle="查看用户信息">
<Grid>
<Label x:Name="LabelUserInfo" Content="Label" HorizontalAlignment="Left" Margin="119,109,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.649,0.499"/>
</Grid>
</Page>
Page2.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPF属性和资源
{
/// <summary>
/// Page2.xaml 的交互逻辑
/// </summary>
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
SolidColorBrush brush = (SolidColorBrush)Application.Current.FindResource("BackgroundColor");
this.Background = brush;
User user = (User)Application.Current.Properties["userInfo"];
string userInfo = "从“" + Application.Current.MainWindow.Title + "”提交的用户信息" + Environment.NewLine;
userInfo += "用户名:" + user.UserName + Environment.NewLine;
userInfo += "Email:" + user.Email;
this.LabelUserInfo.Content = userInfo;
}
}
}