WPF 使用NavigationWindow实现MainWindow传参到Page

先参考的别人的代码:

WPF学习之页间导航与页间数据传递 

在WPF中可以非常简单的实现窗口页间的导航:这里有三种方法:

1、Page调用NavigationService.Navigate

新建项目,并新建一个NavigationWindow的窗体作为主窗体,并将Source指向page1.xaml

然后新建一个页面page1,(做为主窗体的主题内容。) 在page1上方一个button1

然后新建另一个页面page2,在page2上放一个button2,

在button1 的click事件中就可以写实现转向的方法:

Page2 p=new Page2();

this.NavigationService.Navigate(page2);

以上两行代码等价于

this.NavigationService.Navigate(new uil("page2.xaml",UriKind.Relative));

2、xaml中使用Hyperlink标签:

<Hyperlink NavigateUri="page2.xaml">

3、NavigationWindow的导航日志

返回上一页:this.NavigationService.GoBack

 向前下一页:this.NavigationService.GoForward

 

 已上是页面之间导航,同时在转向的同时最常见的就是要数据传递:

 一.通过NavigationService.Navigate传递参数

  1.首先new一个page对象:

          Page2 page2 = new Page2();

  2.通过NavigationService.Navigate传递参数

          this.NavigationService.Navigate(page2,"frompage1");

  3.在Page2,处理NavigationWindow的LoadCompleted事件

          this.NavigationService.LoadCompleted += new LoadCompletedEventHandler(page2_LoadCompleted);

  4.在page2_LoadCompleted方法里获取传递过来的参数       

          void page2_LoadCompleted(object sender,NavigationEventArgs e)

    {

      if(e.ExtraData != null)

      {

                     string args = e.ExtraData.toString();

      }

          }

 二、通过构造函数传递参数(最易使用)

  首先new一个page对象并用NavigationService.Navigate转向

    Page2 page2 = new Page2("frompage1");

    this.NavigationService.Navigate(page2);

     在page2中定义构造函数

    public Page2(string args)

    {

      string args2 = args;

    }

 

 三、通过Application.Properties的全局数据

   用实例或URI导航到页面

          Application.Properties["Args"] = "frompage2";

     Page2 page2 = new Page2();

          this.NavigationService.Navigate(page2);

   在page2页面检查参数值

              if(Application.Properties["Args"] != null)

     {

      string arg = Application.Properties["Args"];

     }

 

 

自己的需要,从MainWindow传参到Page页面

MainWindow:

<NavigationWindow x:Class="NavagationTest.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:NavagationTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
</NavigationWindow>

 

 

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 NavagationTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : NavigationWindow

    {

        int a = 10;
        public MainWindow()
        {
            InitializeComponent();
            //this.frmMain.Navigate(new Uri("WelcomePage.xaml", UriKind.Relative));
            WelcomePage welcomePage = new WelcomePage(a);

            this.NavigationService.Navigate(welcomePage);
        }
    }
}
 

 

Page页面:

<Page x:Class="NavagationTest.WelcomePage"
      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:NavagationTest"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="WelcomePage">

    <Grid Background="Pink">
        
    </Grid>
</Page>
 

 

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 NavagationTest
{
    /// <summary>
    /// WelcomePage.xaml 的交互逻辑
    /// </summary>
    /// 
    
    public partial class WelcomePage : Page
    {
        int rec = 0;
        public WelcomePage(int a)
        {
            InitializeComponent();
            rec = a;
            Console.WriteLine(rec);
        }
    }
}

 

这样Page页面就能接收到MainWIndow传来的参数
 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值