全网营销型的网站,息壤网站模板,网店怎么做,提供网站建设设计外包背景#xff1a;使用ContentControl控件实现区域导航是有Mvvm框架的WPF都能使用的#xff0c;不限于Prism 主要是将ContenControl控件的Content内容在ViewModel中切换成不同的用户控件 下面是MainViewModel#xff1a;
private object body;public object Body
{get { retu…背景使用ContentControl控件实现区域导航是有Mvvm框架的WPF都能使用的不限于Prism 主要是将ContenControl控件的Content内容在ViewModel中切换成不同的用户控件 下面是MainViewModel
private object body;public object Body
{get { return body; }set { body value; RaisePropertyChanged(); }
}public DelegateCommandstring OpenCommand { get; set; }public MainWindowViewModel()
{OpenCommand new DelegateCommandstring(obj {Body obj switch{ViewA new ViewA(),ViewB new ViewB(),ViewC new ViewC(),_ Body};});
} 上面是有Mvvm框架就行了每次打开新的模块就创建一个用户控件对象 下面是使用Prism框架的导航实现会方便一些 1.首先在App.xaml.cs中注入用户控件的依赖 2.ContentControl中的Content修改为
ContentControl Grid.Row1 prism:RegionManager.RegionNameContentRegion /
3.MainWindowViewModel变成
public class MainWindowViewModel : BindableBase
{private readonly IRegionManager regionManager;public DelegateCommandstring OpenCommand { get; set; }public MainWindowViewModel(IRegionManager regionManager){OpenCommand new DelegateCommandstring(obj { regionManager.Regions[ContentRegion].RequestNavigate(obj); });this.regionManager regionManager;}
}
-- 也就是由创建用户控件变成调用依赖注入的用户控件 导航参数 在调用导航前设置导航参数请求导航的时候将导航参数传递过去
NavigationParameters keys new NavigationParameters();
keys.Add(Title, Hello);regionManager.Regions[ContentRegion].RequestNavigate(viewName, keys);
然后导航用户控件的ViewModel需要接口INavigationAware接口重写方法中都是带有参数NavigationContext的然后通过它获取导航参数就行
public void OnNavigatedTo(NavigationContext navigationContext)
{if (navigationContext.Parameters.ContainsKey(Title))Tile navigationContext.Parameters.GetValuestring(Title);
} 路由守卫 需要实现路由守卫需要将原本的INavigationAware接口换成IConfirmNavigationRequest重写的方法如果continuationCallback的值是true就给导航过去如果没有就不给导航
public void ConfirmNavigationRequest(NavigationContext navigationContext, Actionbool continuationCallback)
{bool result true;if (MessageBox.Show(请求导航, 温馨提示, MessageBoxButton.YesNo) MessageBoxResult.No){result false;}continuationCallback(result);
} 导航日志 private IRegionNavigationJournal journal;
// 打开区域的方法
private void OpenView(string viewName)
{NavigationParameters keys new NavigationParameters();keys.Add(Title, Hello);// 调用完区域就记录在日志中regionManager.Regions[ContentRegion].RequestNavigate(viewName, callBack {if (callBack.Cancelled){return;}journal callBack.Context.NavigationService.Journal;}, keys);
}//返回上一页方法让按钮绑定就行
private void back()
{if (journal.CanGoBack)journal.GoBack();
}