c# NavigationWindow + Page的简单使用

首先是application入口文件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace MytApp
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
    }
}

App.xaml

<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyApp"
             StartupUri="MainWindow.xaml">
    <!--StartupUri 配置默认窗体-->
    <Application.Resources>
         
    </Application.Resources>
</Application>

主窗体 MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
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;
using MyApp.ui;

namespace MyApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : NavigationWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            //导航完成呈现界面之前 我们可以在这个事件里处理参数的传递
            NavigationService.LoadCompleted += NavigationService_LoadCompleted;
            //导航的进度事件 一次导航触发多次
            NavigationService.NavigationProgress += NavigationService_NavigationProgress;
            //开始导航
            NavigationService.Navigating += NavigationService_Navigating;
            //导航正常完成
            NavigationService.Navigated += NavigationService_Navigated;
            //停止了导航  导航没有完成
            NavigationService.NavigationStopped += NavigationService_NavigationStopped;
        }

        private void NavigationService_NavigationStopped(object sender, NavigationEventArgs e)
        {
            Debug.WriteLine("导航异常终止");
        }

        private void NavigationService_Navigated(object sender, NavigationEventArgs e)
        {
            Debug.WriteLine("导航正常完成");
        }

        private void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            Debug.WriteLine("开始导航");
        }

        private void NavigationService_NavigationProgress(object sender, NavigationProgressEventArgs e)
        {
            Debug.WriteLine("加载进度:" + e.BytesRead + " " + e.MaxBytes);
        }

        private void NavigationWindow_Loaded(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Maximized;
            WindowStyle = WindowStyle.None;
            ResizeMode = ResizeMode.NoResize;
        }

        private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
        {
            if (e.Content is UnitPage UnitPage)
            {
                if (e.ExtraData != null)
                {
                    UnitPage.Path = e.ExtraData.ToString();
                }
            }
            else if (e.Content is ImportPage ImportPage)
            {
                if (e.ExtraData != null)
                {
                    ImportPage.Path = e.ExtraData.ToString();
                }
            }
        }

        private void NavigationWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            NavigationService.LoadCompleted -= NavigationService_LoadCompleted;
            NavigationService.NavigationProgress -= NavigationService_NavigationProgress;
            NavigationService.Navigating -= NavigationService_Navigating;
            NavigationService.Navigated -= NavigationService_Navigated;
            NavigationService.NavigationStopped -= NavigationService_NavigationStopped;
        }
    }
}
NavigationService_LoadCompleted 路由跳转传参需要依赖这里处理

NavigationWindow_Unloaded 窗体卸载清理注册的事件

MianWindow.xaml

<NavigationWindow x:Class="MyApp.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:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Source="ui/HomePage.xaml"
        Loaded="NavigationWindow_Loaded"
        Unloaded="NavigationWindow_Unloaded"
        ShowsNavigationUI="False">

<!--ShowsNavigationUI 配置是否显示导航向前向后的按钮-->
<!--Source 配置默认显示的Page-->
    
</NavigationWindow>

 HomePage.xaml.cs

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace MyApp.ui
{
    /// <summary>
    /// HomePage.xaml 的交互逻辑
    /// </summary>
    public partial class HomePage : Page
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            //跳转页面并传递参数  这里为了演示就传递一个字符串 可以传递对象
            NavigationService.Navigate(new Uri("ui/UnitPage.xaml", UriKind.Relative), "参数", true);
               
        }

        private void ExitBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("确认要退出系统吗?", "退出确认", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                Environment.Exit(0);
            }
        }
    }
}

HomePage.xaml

<Page x:Class="MyApp.ui.HomePage"
      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:res="clr-namespace:MyApp.Properties"
      xmlns:local="clr-namespace:MyApp"
      mc:Ignorable="d" 
      d:DesignHeight="1080" d:DesignWidth="1920"
      Title="HomePage"
      KeepAlive="True">
  <!--KeepAlive 页面缓存 一定要为true 否则页面返回时原来的参数会没有值,导致页面显示不正确-->

    <Page.Background>
        <!--pack://siteoforigin:,,,/res/home.jpg   打包后的根目录/res/home.jpg-->
        <ImageBrush ImageSource="pack://siteoforigin:,,,/res/home.jpg"></ImageBrush>
    </Page.Background>

    <Grid>
        <Viewbox>
            <Canvas Width="1920" Height="1080">
                <StackPanel Orientation="Vertical" Canvas.Left="0" Canvas.Top="0" Canvas.Right="0" Canvas.Bottom="0">
                    <Button Click="Button_Click" BorderBrush="Transparent" Background="Transparent" Width="500" Height="500">
                     跳转测试
                    </Button>
                </StackPanel>


                 <!--退出按钮  我这里主窗体使用了全屏 并且不显示边框 所以自己定义退出按钮-->
                <Button x:Name="ExitBtn" Click="ExitBtn_Click" Canvas.Right="16" Canvas.Top="16">
                    <Button.Template>
                        <ControlTemplate>
                            <Image Stretch="Fill">
                                <Image.Source>
                                    <Binding Source="{x:Static res:Resources.close}">
                                        <Binding.Converter>
                                            <local:ResourceToSourceConverter />
                                        </Binding.Converter>
                                    </Binding>
                                </Image.Source>
                            </Image>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </Canvas>
        </Viewbox>
    </Grid>
</Page>
UnitPage.xaml.cs
using MyApp.bean;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;

namespace MyApp.ui
{
    /// <summary>
    /// UnitPage.xaml 的交互逻辑
    /// </summary>
    public partial class UnitPage : Page
    {

        public string Path { set; get; }

        public UnitPage()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("传递的参数:" + Path);
        }
    }
}

UnitPage.xaml

button 添加 Command="{x:Static NavigationCommands.BrowseBack}" 绑定返回操作

还可以使用绑定事件 在事件中 调用 NavigationService.GoBack() 返回

<Page x:Class="MyApp.ui.UnitPage"
      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:c="clr-namespace:CalcBinding;assembly=CalcBinding"
      xmlns:local="clr-namespace:MyApp"
      xmlns:res="clr-namespace:MeyApp.Properties"
      Loaded="Page_Loaded"
      mc:Ignorable="d" 
      d:DesignHeight="1080" d:DesignWidth="1920"
      Title="UnitPage"
      KeepAlive="True">

    <!-- 设置背景 -->
    <Page.Background>
        <ImageBrush>
            <ImageBrush.ImageSource>
                <Binding Source="{x:Static res:Resources.bg}">
                    <Binding.Converter>
                        <local:ResourceToSourceConverter />
                    </Binding.Converter>
                </Binding>
            </ImageBrush.ImageSource>
        </ImageBrush>
    </Page.Background>

    <Grid>
        <Viewbox Stretch="Fill" Panel.ZIndex="300">
                <Canvas Panel.ZIndex="600">
                    <!--Command 直接绑定返回指令
                    -->
                    <Button Canvas.Right="66" Canvas.Top="50" Panel.ZIndex="600" Width="100" Background="Transparent" 
                            BorderBrush="Transparent" Command="{x:Static NavigationCommands.BrowseBack}">
                        <Image>
                            <Image.Source>
                                <Binding Source="{x:Static res:Resources.back_icon}">
                                    <Binding.Converter>
                                        <local:ResourceToSourceConverter />
                                    </Binding.Converter>
                                </Binding>
                            </Image.Source>
                        </Image>
                    </Button>
                </Canvas>

            </Grid>
        </Viewbox>
    </Grid>
</Page>


原文地址:https://www.cnblogs.com/rchao/p/15271038.html