从主窗口传递参数到页面,直接写页面的字段就可以,而从页面传值回主窗口使用委托。
使用委托事件传递参数的好处是接收数值的页面可以订阅,这样一些颜色改变或者信息等自动处理了。
最关键的部分,传递界面先声明委托,接收页面(我这里使用的是MainWindow)写一个订阅函数就可以了。
1 界面部分
1.1,主窗口,
1.1.1接收页面参数:一个contentcontrol用于加载两个页面,并使用两个button来切换页面,一个label接收来自页面的值;
1.1.2 传值到页面
两个触发传值事件,一个textbox输入值
下面时设计图
主窗口的代码
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="400">
<Grid>
<ContentControl x:Name="cnt1" Content="ContentControl" HorizontalAlignment="Left" Margin="140,90,0,0" VerticalAlignment="Top" Width="200" Height="200" BorderThickness="1"/>
<Button x:Name="btn1" Content="加载页1" HorizontalAlignment="Left" Margin="140,30,0,0" VerticalAlignment="Top" Width="85" Height="30" Click="btn1_Click" RenderTransformOrigin="1.235,-2.333"/>
<Button x:Name="btn2" Content="加载页2" HorizontalAlignment="Left" Margin="255,30,0,0" VerticalAlignment="Top" Width="85" Height="30" Click="btn2_Click"/>
<Label x:Name="lblMain" Content="来自页面的值" HorizontalAlignment="Left" Margin="35,195,0,0" VerticalAlignment="Top" Width="85" Height="30" />
<Button x:Name="btnTransPage1" Content="传值给页面1" HorizontalAlignment="Left" Margin="35,320,0,0" VerticalAlignment="Top" Width="100" Height="30" Click="btnTransPage1_Click" RenderTransformOrigin="1.235,-2.333" />
<Button x:Name="btnTransPage2" Content="传值给页面2" HorizontalAlignment="Left" Margin="35,370,0,0" VerticalAlignment="Top" Width="100" Height="30" Click="btnTransPage2_Click" RenderTransformOrigin="1.235,-2.333" />
<TextBox x:Name="tbMain2Page" HorizontalAlignment="Left" Margin="180,350,0,0" VerticalAlignment="Top" Width="80" Height="20" Text="TextBox" TextWrapping="Wrap" />
</Grid>
</Window>
1.2,新建两个page,每个page有一个button和textbox,触发传值回主页面,增加一个ltextblock接收主窗口的值
xaml语言两个page及其相似,这里就贴一个xaml
<Page x:Class="WpfApp1.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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200"
Title="Page1">
<Grid >
<Button x:Name="btnPage1" Content="传值给主窗口-1" HorizontalAlignment="Left" Margin="30,68,0,0" VerticalAlignment="Top" Width="95" Height="40" Click="btnPage1_Click" RenderTransformOrigin="0.526,0"/>
<TextBox x:Name="tb1" HorizontalAlignment="Left" Height="23" Margin="30,40,0,0" TextWrapping="Wrap" Text="Page1" VerticalAlignment="Top" Width="120"/>
<TextBlock x:Name="tbRecivePg" HorizontalAlignment="Left" Margin="30,160,0,0" TextWrapping="Wrap" Text="接收主窗口值" VerticalAlignment="Top" Width="120" Height="20"/>
</Grid>
</Page>
2,代码部分
2.1,在page里新建委托和按钮事件,下面的代码时page1的,page2类似
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
public delegate void SentValue(string value); //声明委托
public SentValue sentValue;
private void btnPage1_Click(object sender, RoutedEventArgs e)
{
sentValue(tb1.Text);
}
}
2.2,主窗口 MainWindow的代码
2.2.1 实例化page
Page1 p1 = new Page1();
Page2 p2 = new Page2();
2.2.2 在MainWindow加载时订阅委托函数
public MainWindow()
{
InitializeComponent();
//构造函数里加载订阅委托函数
p1.sentValue = GetValueFormPage;
p2.sentValue = GetValueFormPage;
}
2.2.3 将订阅值赋给label
public void GetValueFormPage(string value)
{//订阅函数-> 将页面的值赋给主窗口的label
lblMain.Content = value;
}
2.3 效果
3,主窗口传给page,直接赋值
private void btnTransPage1_Click(object sender, RoutedEventArgs e)
{
p1.tbRecivePg.Text = tbMain2Page.Text;
}
private void btnTransPage2_Click(object sender, RoutedEventArgs e)
{
p2.tbRecivePg.Text = tbMain2Page.Text;
}
效果
4.1 主窗口代码
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Page1 p1 = new Page1();
Page2 p2 = new Page2();
public MainWindow()
{
InitializeComponent();
//构造函数里加载订阅委托函数
p1.sentValue = GetValueFormPage;
p2.sentValue = GetValueFormPage;
}
public void GetValueFormPage(string value)
{//订阅函数-> 将页面的值赋给主窗口的label
lblMain.Content = value;
}
#region 从主窗口到界面直接写页面的字段
private void btnTransPage1_Click(object sender, RoutedEventArgs e)
{
p1.tbRecivePg.Text = tbMain2Page.Text;
}
private void btnTransPage2_Click(object sender, RoutedEventArgs e)
{
p2.tbRecivePg.Text = tbMain2Page.Text;
}
#endregion
/// <summary>
/// 加载页面1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (p1 == null)
{
p1 = new Page1();
}
cnt1.Content = new Frame()
{
Content = p1
};
}
/// <summary>
/// 加载页面2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn2_Click(object sender, RoutedEventArgs e)
{
if (p2 == null)
{
p2 = new Page2();
}
cnt1.Content = new Frame()
{
Content = p2
};
}
}
}
4.2 page代码
namespace WpfApp1
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
public delegate void SentValue(string value); //声明委托
public SentValue sentValue;
private void btnPage1_Click(object sender, RoutedEventArgs e)
{
sentValue(tb1.Text);
}
}
}
小结 示例传递了字符串,实际使用时也可以传递类和其他参数。