先看效果图


##Get Start
  为了项目解耦,使用mvvmlight框架。MVVM设计模式请自行了解。
###1 新建项目
  新建一个MvvmLight(WPF)项目,删除其中无关文件夹:Design+Model。
###2 添加类
  添加NavigationService类,继承ViewModelBase和INavigationService。

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Views;
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.Media; namespace PageNavigation
{
public class NavigationService : ViewModelBase, INavigationService
{
private readonly Dictionary<string, Uri> _pagesByKey; private string _currentPageKey; public string CurrentPageKey
{
get
{
return _currentPageKey;
} private set
{
Set(() => CurrentPageKey, ref _currentPageKey, value);
}
} public NavigationService()
{
_pagesByKey = new Dictionary<string, Uri>();
} public void GoBack()
{
throw new NotImplementedException();
} public void NavigateTo(string pageKey)
{
NavigateTo(pageKey, "Next");
} public void NavigateTo(string pageKey, object parameter)
{
var frame = GetDescendantFromName(Application.Current.MainWindow, "MainFrame") as Frame; if (frame != null)
{ frame.Source = _pagesByKey[pageKey];
} CurrentPageKey = pageKey;
} public void Configure(string key, Uri pageType)
{
lock (_pagesByKey)
{
if (_pagesByKey.ContainsKey(key))
{
_pagesByKey[key] = pageType;
}
else
{
_pagesByKey.Add(key, pageType);
}
}
} public Uri getUri(string key)
{
if (_pagesByKey.ContainsKey(key))
{
return _pagesByKey[key];
} return null;
} private static FrameworkElement GetDescendantFromName(DependencyObject parent, string name)
{
if (parent == null)
{
for (int i = 0; i < 10; i++)
{ }
}
var count = VisualTreeHelper.GetChildrenCount(parent); if (count < 1)
{
return null;
} for (var i = 0; i < count; i++)
{
var frameworkElement = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
if (frameworkElement != null)
{
if (frameworkElement.Name == name)
{
return frameworkElement;
} frameworkElement = GetDescendantFromName(frameworkElement, name);
if (frameworkElement != null)
{
return frameworkElement;
}
}
}
return null;
}
}
}

新建views文件夹,在其中添加用户控件

FirstPage控件

<UserControl x:Class="PageNavigation.views.FirstPage"
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:PageNavigation.views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid Background="Green">
<Label Content="First" FontSize="40"/>
</Grid>
</UserControl>

SecondPage控件

<UserControl x:Class="PageNavigation.views.SecondPage"
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:PageNavigation.views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid Background="Pink">
<Label Content="Second" FontSize="40"/>
</Grid>
</UserControl>

ViewModel文件夹中添加FirstViewModel类,继承ViewModelBase

using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PageNavigation.ViewModel
{
public class FirstViewModel:ViewModelBase
{
}
}

ViewModel文件夹中添加SecondViewModel类,继承ViewModelBase

using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PageNavigation.ViewModel
{
public class SecondViewModel:ViewModelBase
{
}
}

3 修改类

主界面MainWindow.xaml类

	<Window x:Class="PageNavigation.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:ignore="http://www.galasoft.ch/ignore"
mc:Ignorable="d ignore"
Height="600"
Width="1000"
Title="MVVM Light Application"
DataContext="{Binding Main, Source={StaticResource Locator}}"> <Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources> <Grid x:Name="LayoutRoot" Width="950" Height="550">
<DockPanel Width="900" Height="500">
<StackPanel DockPanel.Dock="Left" Width="200">
<Button Height="50" Content="first" Command="{Binding FirstPageCommand}"></Button>
<Button Height="50" Content="second" Command="{Binding SecondPageCommand}"></Button>
</StackPanel>
<Grid DockPanel.Dock="Right" Width="600" Height="450" Background="Yellow">
<Frame x:Name="MainFrame" JournalOwnership="UsesParentJournal" Background="Transparent" Width="500" Height="400"/>
</Grid>
</DockPanel>
</Grid>
</Window>

ViewModelLocator类,在其中注册各ViewModel,初始化导航类

/*
In App.xaml:
<Application.Resources>
<vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:PageNavigation.ViewModel"
x:Key="Locator" />
</Application.Resources> In the View:
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/ using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using System;
namespace PageNavigation.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// <para>
/// See http://www.mvvmlight.net
/// </para>
/// </summary>
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<FirstViewModel>();
SimpleIoc.Default.Register<SecondViewModel>(); var navigationService = CreateNavigationService();
SimpleIoc.Default.Register<INavigationService>(() => navigationService);
} private static INavigationService CreateNavigationService()
{
var nav = new NavigationService(); var navigationService = new NavigationService();
navigationService.Configure("First", new Uri("/PageNavigation;component/views/FirstPage.xaml", UriKind.Relative));
navigationService.Configure("Second", new Uri("/PageNavigation;component/views/SecondPage.xaml", UriKind.Relative));
return navigationService;
} /// <summary>
/// Gets the Main property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
} public FirstViewModel First
{
get
{
return ServiceLocator.Current.GetInstance<FirstViewModel>();
}
} public SecondViewModel Second
{
get
{
return ServiceLocator.Current.GetInstance<SecondViewModel>();
}
} /// <summary>
/// Cleans up all the resources.
/// </summary>
public static void Cleanup()
{
}
}
}

MainViewModel类,其中定义了按钮的点击绑定命令,与view之间用消息通信,即Messenger.Default......

using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using System;
using System.Windows.Input; namespace PageNavigation.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// See http://www.mvvmlight.net
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{ public ICommand FirstPageCommand { get; set; } public ICommand SecondPageCommand { get; set; } public MainViewModel()
{
FirstPageCommand = new RelayCommand(First);
SecondPageCommand = new RelayCommand(Second);
} private void First()
{
var navigationService = ServiceLocator.Current.GetInstance<INavigationService>();
NavigationService service = (NavigationService)navigationService;
Uri sendUri = service.getUri("First");
Messenger.Default.Send<Uri>(sendUri, "FrameSourceName");
} private void Second()
{
var navigationService = ServiceLocator.Current.GetInstance<INavigationService>();
NavigationService service = (NavigationService)navigationService;
Uri sendUri = service.getUri("Second");
Messenger.Default.Send<Uri>(sendUri, "FrameSourceName");
} }
}

在MainWindow.xaml.cs定义消息接收以及消息处理

using System.Windows;
using PageNavigation.ViewModel;
using GalaSoft.MvvmLight.Messaging;
using System; namespace PageNavigation
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
InitializeComponent();
Messenger.Default.Register<Uri>(this, "FrameSourceName", ChangeFrameSource);
Closing += (s, e) => ViewModelLocator.Cleanup();
}
private void ChangeFrameSource(Uri uri)
{
this.MainFrame.Source = uri;
}
}
}

end

源码传送

contact me by email--  duwinter@163.com

桌面应用开发之WPF页面导航的更多相关文章

  1. 桌面应用开发之WPF动态背景

      因为项目需要,在WPF开发的桌面应用中,登陆页面需使用动态背景.由于没有前端开发人员,所以由半吊子的后端开发人员根据效果图写前端xaml.去掉页面上边框,抽离动态背景设置代码: <Windo ...

  2. 微信公众号开发之H5页面跳转到指定的小程序

    前言: 最近公司有一个这样的需要,需要从我们在现有的公众号H5页面中加一个跳转到第三方小程序的按钮.之前只知道小程序之间是可以相互跳转的,今天查阅了下微信开发文档原来现在H5网页也支持小程序之间的跳转 ...

  3. IOS开发之Bug--iOS7View被导航栏遮挡问题的解决

    在实际开发中,遇到在UITextView的frame等于当前控制器的View的frame的情况下,然后运行的时候,发现控制器的Frame的高度y值会从导航条的位置64变化到0. 导致UITextVie ...

  4. MEF 插件式开发之 WPF 初体验

    MEF 在 WPF 中的简单应用 MEF 的开发模式主要适用于插件化的业务场景中,C/S 和 B/S 中都有相应的使用场景,其中包括但不限于 ASP.NET MVC .ASP WebForms.WPF ...

  5. Electro桌面应用开发之HelloWorld

    简介 Electron (http://http://electron.atom.io‎)提供了一个使用Node.js进行桌面应用开发的环境. 本文介绍了一个基于Electron的HelloWorld ...

  6. WPF 页面导航

    <Button x:Name="btnReset" Click="btnReset_Click" Content="重 置" Grid ...

  7. WPF的页面导航

    工作中之前接触过的WPF程序一直是使用TabControl作不同页面间的切换,每个Tab负责独立的功能,清晰简捷,所以一直就没有动力研究WPF自带的页面导航.(虽然接触过使用页面导航的WPF项目,也并 ...

  8. WPF MvvmLight简单实例(1) 页面导航

    原文:WPF MvvmLight简单实例(1) 页面导航 实现了那些功能,先看看截图: 操作描述: 在程序运行后,点击“Load”按钮,页面会加载PageOne,点击PageOne页面中的“Next” ...

  9. DarkStone - 跨平台移动应用开发之 Flex 的崛起

    我的好友Ds 发布一个flex的消息.我帮忙转发 DarkStone - 跨平台移动应用开发之 Flex 的崛起 (2013-08-20 22:28:32)     此文章由 周戈 (DarkSton ...

随机推荐

  1. jquery中live is not a function的问题

    jquery中的live()方法在jquery1.9及以上的版本中已被废弃了,如果使用,会抛出TypeError: $(...).live is not a function错误. 解决方法: 之前的 ...

  2. PHP对接微信支付采坑

    第一次做PHP商城项目对接微信支付接口,踩了N次坑,这也不对,那也不对,搞了很久,查了一些资料,终于实现了支付功能,小小总结一下,万一下次遇到就不用到处找资料了. 微信扫码支付 前期准备: 1.微信公 ...

  3. SpringBoot +Jpa+ Hibernate+Mysql工程

    1 使用工具workspace-sts 3.9.5.RELEASE (1)新建一个SpringBoot 项目,选择加载项目需要的的组件.DevTools,JPA,Web,Mysql. Finish.  ...

  4. 使用List需要注意的点

    目录 1. 概述 2. Arrays.asList(); 2-1. 产生不可操作的集合 2-2. 需要传入对象数组而不是基本类型数组 3. arrayList.subList(); 3-1. subL ...

  5. python sqlparse 各种 token

    https://blog.csdn.net/qq_39607437/article/details/79620383 import sqlparse def look(statement): prin ...

  6. git hub 第一篇

    昨天跟着菜鸟教程进行操作,问题如下: 1.在git网站进行注册,名称和邮箱 2..忘了在开头建立本地仓库,后来又新建仓库,在仓库右键添加git bash here 3.出现了下边这个错误,参考了很多文 ...

  7. wiredtiger--初学数据恢复

    启动mongodb是failed,日志如下 1.解压wirdtiger包 tar -vxf wiredtiger-3.1.0.tar.bz2 -C /home/wiredtiger/ 2.安装snap ...

  8. idea常用快捷键及操作

    ctrl+j  ===== 智能提示 可用模版及关键字 ctrl+p ===== 显示方法可填入的参数 ctrl+space ===== 补全提示项目中可用的变量 ctrl+shift+j  ==== ...

  9. jQueryEasyUI学习笔记

    data-options 是jQuery Easyui的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写入到html中 data-options="region:'w ...

  10. 3. Go语言基本类型

    Go语言基本类型如下: bool string 数值类型 (int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, uint, f ...