前言:
该技术前提是 已经掌握一定程度的mvvmlight for win8 技巧
实现思路:
通过Messenger 机制 动态发送导航参数 到 需要导航的page 完成导航
导航参数:
public class NavigateParameter { private string pageType; public string PageType { get { return pageType; } set { pageType = value; } } private object parameter; public object Parameter { get { return parameter; } set { parameter = value; } } }
在 OnNavigated 到首页的地方 加入以下方法 注册 该处理通道
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); this.prContent.IsIndeterminate = true; this.ContentFrame.Navigate(typeof(MainPage)); this.TopFrame.Navigate(typeof(VideosPage)); #region Messengers 消息处理中心 Messenger.Default.Register<NavigateParameter>(this, "MenuNavigate", async s => { var type = await Task.Run<Type>(() => { var fullName = "IntTourism.View." + s.PageType; return Type.GetType(fullName); }); if (null != s.Parameter) await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => this.ContentFrame.Navigate(type, s.Parameter)); else await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => this.ContentFrame.Navigate(type)); }); Messenger.Default.Register<NavigateParameter>(this, "TopFrameNavigate", async s => { var type = await Task.Run<Type>(() => { var fullName = "IntTourism.View." + s.PageType; return Type.GetType(fullName); }); await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { if (!TopFrame.CurrentSourcePageType.Equals(type)) if (null != s.Parameter) this.TopFrame.Navigate(type, s.Parameter); else this.TopFrame.Navigate(type); }); }); #endregion }
Unloaded 注销
private void LayoutAwarePage_Unloaded(object sender, RoutedEventArgs e) { Messenger.Default.Unregister<NavigateParameter>(this, "MenuNavigate"); Messenger.Default.Unregister<LogBean>(this, "LogError"); Messenger.Default.Unregister<LogBean>(this, "TopBusy"); Messenger.Default.Unregister<LogBean>(this, "ContentBusy"); Messenger.Default.Unregister<LogBean>(this, "TopFrame"); this.btnCancel.PointerPressed -= SendError; this.btnSend.PointerPressed -= SendError; }
以下为xaml 里 gridview 菜单的binding方式
注意:ItemClick 此处通过eventTocommand 将事件与 viewmodel中相应的命令进行了binding处理 请结合我上一篇文章 对EventTocomand这一概念进行理解
<common:LayoutAwarePage x:Class="IntTourism.MainPage" 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:i="using:IntTourism.Com.Transvalue.Tools" xmlns:ignore="http://www.ignore.com" xmlns:common="using:IntTourism.Common" xmlns:cmd="using:IntTourism" mc:Ignorable="d ignore" d:DesignHeight="1080" d:DesignWidth="1080" DataContext="{Binding Main, Source={StaticResource Locator}}" NavigationCacheMode="Required"> <Viewbox> <Grid Width="1080" Height="1080" x:Name="root"> <GridView HorizontalAlignment="Left" VerticalAlignment="Top" Width="1080" Height="1080" CanDragItems="false" IsRightTapEnabled="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" IsSwipeEnabled="False" x:Name="gv" ItemsSource="{Binding MenuItems}" SelectionMode="None" > <GridView.Transitions> <TransitionCollection> <EntranceThemeTransition/> </TransitionCollection> </GridView.Transitions> <GridView.ItemContainerTransitions> <TransitionCollection> <EntranceThemeTransition/> </TransitionCollection> </GridView.ItemContainerTransitions> <GridView.HeaderTransitions> <TransitionCollection> <EntranceThemeTransition/> </TransitionCollection> </GridView.HeaderTransitions> <GridView.ItemTemplate> <DataTemplate> <GridViewItem> <Image Height="280" Width="280" Source="{Binding IconUrl}"/> <i:EventToCommandCollection.Items> <i:EventToCommand Command="{Binding MenuCMD}" CommandParameter="{Binding NavigateParameter}" NavigateUrl="{Binding NavigateUrl}" Event="ItemClick"/> </i:EventToCommandCollection.Items> </GridViewItem> </DataTemplate> </GridView.ItemTemplate> <GridView.ItemsPanel> <ItemsPanelTemplate> <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0" ItemWidth="300" ItemHeight="300" MaximumRowsOrColumns="10"/> </ItemsPanelTemplate> </GridView.ItemsPanel> </GridView> </Grid> </Viewbox> </common:LayoutAwarePage>
下面这一段是ViewModel中对命令的处理,以及菜单的动态加载
using GalaSoft.MvvmLight; using IntTourism.Com.Transvalue.Service; using GalaSoft.MvvmLight.Command; using System.Diagnostics; using Windows.UI.Xaml.Controls; using System.Collections; using System.Collections.ObjectModel; using IntTourism.Com.Transvalue.Model; using GalaSoft.MvvmLight.Messaging; using System; using System.ComponentModel; using System.Collections.Generic; using IntTourism.Com.Transvalue.Bean; using IntTourism.Com.Transvalue.Tools; using IntTourism.Com.Transvalue.Const; using System.Threading.Tasks; namespace IntTourism.ViewModel { /// <summary> /// This class contains properties that the main View can data bind to. /// <para> /// See http://www.galasoft.ch/mvvm /// </para> /// </summary> public class MainViewModel : ViewModelBase { private readonly IMainPageSerivce _dataService; #region Command private RelayCommand<ExCommandParameter> _menuCMD; /// <summary> /// Gets the MenuCMD. /// </summary> public RelayCommand<ExCommandParameter> MenuCMD { get { return _menuCMD ?? (_menuCMD = new RelayCommand<ExCommandParameter>( ExecuteMenuCMD, CanExecuteMenuCMD)); } } private async void ExecuteMenuCMD(ExCommandParameter url) { await Task.Run(() => { var nav = new NavigateParameter(); nav.Parameter = url.Parameter; nav.PageType = url.NavigateUrl; if (null != url.NavigateUrl) { Messenger.Default.Send<bool>(true, "ContentBusy"); Messenger.Default.Send<NavigateParameter>(nav, "MenuNavigate"); } }); } private bool CanExecuteMenuCMD(ExCommandParameter url) { if (null != url && !"".Equals(url)) return true; else return false; } #endregion #region DataProperties #region MenuItems /// <summary> /// The <see cref="MenuItems" /> property's name. /// </summary> public const string MenuItemsPropertyName = "MenuItems"; private ObservableCollection<MenuItem> _menuItems; /// <summary> /// Sets and gets the MenuItems property. /// Changes to that property's value raise the PropertyChanged event. /// This property's value is broadcasted by the MessengerInstance when it changes. /// </summary> public ObservableCollection<MenuItem> MenuItems { get { return _menuItems; } set { if (_menuItems == value) { return; } RaisePropertyChanging(() => MenuItems); var oldValue = _menuItems; _menuItems = value; RaisePropertyChanged(() => MenuItems, oldValue, value, true); } } #endregion /// <summary> /// The <see cref="WelcomeTitle" /> property's name. /// </summary> public const string WelcomeTitlePropertyName = "WelcomeTitle"; private string _welcomeTitle; /// <summary> /// Sets and gets the WelcomeTitle property. /// Changes to that property's value raise the PropertyChanged event. /// This property's value is broadcasted by the MessengerInstance when it changes. /// </summary> public string WelcomeTitle { get { return _welcomeTitle; } set { if (_welcomeTitle == value) { return; } RaisePropertyChanging(WelcomeTitlePropertyName); var oldValue = _welcomeTitle; _welcomeTitle = value; RaisePropertyChanged(WelcomeTitlePropertyName, oldValue, value, true); } } #endregion /// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel(IMainPageSerivce dataService) { _dataService = dataService; InitPropertyChanged(); _dataService.GetItemData( async (items, error) => { if (await Pollute.ReportError(error, "GetItemData", ErrorDesc._E01)) { Messenger.Default.Send<bool>(false, "ContentBusy"); return; } var data = await items; foreach (var item in data) item.MenuCMD = MenuCMD; MenuItems = data; Messenger.Default.Send<bool>(false, "ContentBusy"); }); } #region PropertyChangeMethods /// <summary> /// 初始化相关方法 /// </summary> private void InitPropertyChanged() { PropertyChanging += PropertyChangingMethod; PropertyChanged += PropertyChangedMethod; } //private static Dictionary<Predicate<string>, Action> PropertyChangingActions; //private static Dictionary<Predicate<string>, Action> PropertyChangedActions; private void PropertyChangedMethod(object sender, PropertyChangedEventArgs e) { } private void PropertyChangingMethod(object sender, PropertyChangingEventArgs e) { } #endregion public override void Cleanup() { // Clean up if needed base.Cleanup(); } } }
菜单实例数据:
using System; using System.Collections.ObjectModel; using System.Threading.Tasks; using IntTourism.Com.Transvalue.Bean; using IntTourism.Com.Transvalue.Model; using IntTourism.Com.Transvalue.Service; namespace IntTourism.Com.Transvalue.Service.Impl { public class MainPageService : IMainPageSerivce { public void GetData(Action<DataItem, Exception> callback) { var item = new DataItem("Welcome to MVVM Light"); callback(item, null); } public void GetItemData(Action<Task<ObservableCollection<MenuItem>>, Exception> callback) { var result = Task.Run(() => { var items = new ObservableCollection<MenuItem>(); var menu1 = new MenuItem(); menu1.IconUrl = "../Assets/ICON/交通.png"; menu1.NavigateUrl = "IntMaps"; items.Add(menu1); var menu2 = new MenuItem(); menu2.IconUrl = "../Assets/ICON/娱乐.png"; menu2.NavigateUrl = "IntMaps"; menu2.NavigateParameter = "Play"; items.Add(menu2); var menu3 = new MenuItem(); menu3.IconUrl = "../Assets/ICON/景点.png"; menu3.NavigateUrl = "SceneViewGroup"; items.Add(menu3); var menu4 = new MenuItem(); menu4.IconUrl = "../Assets/ICON/购物.png"; menu4.NavigateUrl = "IntMaps"; menu4.NavigateParameter = "Buy"; items.Add(menu4); var menu5 = new MenuItem(); menu5.IconUrl = "../Assets/ICON/专题旅游.png"; menu5.NavigateUrl = "SceneViewGroup"; menu5.NavigateParameter = "Travel"; items.Add(menu5); var menu6 = new MenuItem(); menu6.IconUrl = "../Assets/ICON/实用信息.png"; items.Add(menu6); var menu7 = new MenuItem(); menu7.IconUrl = "../Assets/ICON/武汉之最.png"; items.Add(menu7); var menu8 = new MenuItem(); menu8.IconUrl = "../Assets/ICON/住宿.png"; menu8.NavigateUrl = "IntMaps"; menu8.NavigateParameter = "Rest"; items.Add(menu8); var menu9 = new MenuItem(); menu9.IconUrl = "../Assets/ICON/推荐线路.png"; items.Add(menu9); //var menu10 = new MenuItem(); //menu10.IconUrl = "../Assets/ICON/武汉介绍.png"; //items.Add(menu10); var menu11 = new MenuItem(); menu11.IconUrl = "../Assets/ICON/周边旅游.png"; menu11.NavigateUrl = "IntMaps"; menu11.NavigateParameter = "AllTravel"; items.Add(menu11); var menu12 = new MenuItem(); menu12.IconUrl = "../Assets/ICON/旅游动态.png"; items.Add(menu12); var menu13 = new MenuItem(); menu13.IconUrl = "../Assets/ICON/美图欣赏.png"; items.Add(menu13); var menu14 = new MenuItem(); menu14.IconUrl = "../Assets/ICON/天气预报.png"; items.Add(menu14); var menu15 = new MenuItem(); menu15.IconUrl = "../Assets/ICON/旅游咨询.png"; items.Add(menu15); var menu16 = new MenuItem(); menu16.IconUrl = "../Assets/ICON/美食.png"; menu16.NavigateUrl = "IntMaps"; menu16.NavigateParameter = "Eat"; items.Add(menu16); return items; }); callback(result, null); } } }
以上sample 数据 可以存入数据库中 进行动态配置,完成菜单的动态显示,降低了程序的耦合,增加了可配置性。