Windows.UI.Xaml.Controls.Frame'在win10通用应用中使用mvvm-light导航
本文关键字:应用 导航 mvvm-light win10 Controls Xaml UI Frame Windows | 更新日期: 2023-09-27 18:08:58
我在新的windows 10通用应用程序c#/XAML上遇到以下错误:
System类型的异常。在GalaSoft.MvvmLight.Platform.dll中发生InvalidCastException,但未在用户代码中处理附加信息:无法将"类型的对象强制转换为'Windows.UI.Xaml.Controls.Frame'类型。
在我的页面视图模型中使用以下导航命令:
_navigationService.NavigateTo(ViewModelLocator.MedicineBoxPageKey);
我试图有一个汉堡包菜单风格的导航(见这个例子)。应用程序的一个例子,如何做到这一点)到:
1-在我所有的页面上共享一个方便的解决方案。上面提到的示例使用AppShell Page作为应用程序的根,而不是Frame,它封装了导航菜单和后退按钮的一些行为。那太好了。
2-使用MVVM-Light导航服务来方便地处理视图模型中的所有导航。
下面是App.xml.Cs如何初始化shell页面: AppShell shell = Window.Current.Content as AppShell;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (shell == null)
{
// Create a a AppShell to act as the navigation context and navigate to the first page
shell = new AppShell();
// Set the default language
shell.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
shell.AppFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
}
// Place our app shell in the current Window
Window.Current.Content = shell;
if (shell.AppFrame.Content == null)
{
// When the navigation stack isn't restored, navigate to the first page
// suppressing the initial entrance animation.
shell.AppFrame.Navigate(typeof(MedicinesStorePage), e.Arguments, new Windows.UI.Xaml.Media.Animation.SuppressNavigationTransitionInfo());
}
// Ensure the current window is active
Window.Current.Activate();
下面是AppShell的类定义:
public sealed partial class AppShell : Page
{
public static AppShell Current = null;
public AppShell()
{
this.InitializeComponent();
}
}
从我到目前为止所尝试的,mvvm-light导航服务只有在Frame被使用到应用程序的根目录并注意到一个页面时才有效(否则我们会得到这个投射错误)。然而,使用框架似乎也不是一个选项,因为正如示例应用程序所说:
使用Page作为应用程序的根提供了一个设计时的体验,并确保当它在移动设备上运行时,应用程序的内容不会出现在可见的系统状态栏下默认为透明背景。它还将考虑软件的存在导航按钮(如果它们出现在设备上)。应用程序可以通过切换到UseCoreWindow来选择退出。
我还试图从mvvm-light导航服务覆盖navigationTo方法,但错误似乎在我捕捉到它之前就发生了。
有没有人有一个解决方案,使用mvvm-light导航服务和一个shell页面作为应用程序根(管理汉堡包菜单等)?
非常感谢!
我和Laurent Bugnion谈过,他建议我实现自己的导航服务,由他来处理导航。为此,我制作了一个PageNavigationService,它实现了MVVM Light的INavigationService接口。
public class PageNavigationService : INavigationService
{
/// <summary>
/// The key that is returned by the <see cref="CurrentPageKey" /> property
/// when the current Page is the root page.
/// </summary>
public const string RootPageKey = "-- ROOT --";
/// <summary>
/// The key that is returned by the <see cref="CurrentPageKey" /> property
/// when the current Page is not found.
/// This can be the case when the navigation wasn't managed by this NavigationService,
/// for example when it is directly triggered in the code behind, and the
/// NavigationService was not configured for this page type.
/// </summary>
public const string UnknownPageKey = "-- UNKNOWN --";
private readonly Dictionary<string, Type> _pagesByKey = new Dictionary<string, Type>();
/// <summary>
/// The key corresponding to the currently displayed page.
/// </summary>
public string CurrentPageKey
{
get
{
lock (_pagesByKey)
{
var frame = ((AppShell) Window.Current.Content).AppFrame;
if (frame.BackStackDepth == 0)
{
return RootPageKey;
}
if (frame.Content == null)
{
return UnknownPageKey;
}
var currentType = frame.Content.GetType();
if (_pagesByKey.All(p => p.Value != currentType))
{
return UnknownPageKey;
}
var item = _pagesByKey.FirstOrDefault(
i => i.Value == currentType);
return item.Key;
}
}
}
/// <summary>
/// If possible, discards the current page and displays the previous page
/// on the navigation stack.
/// </summary>
public void GoBack()
{
var frame = ((Frame) Window.Current.Content);
if (frame.CanGoBack)
{
frame.GoBack();
}
}
/// <summary>
/// Displays a new page corresponding to the given key.
/// Make sure to call the <see cref="Configure" />
/// method first.
/// </summary>
/// <param name="pageKey">
/// The key corresponding to the page
/// that should be displayed.
/// </param>
/// <exception cref="ArgumentException">
/// When this method is called for
/// a key that has not been configured earlier.
/// </exception>
public void NavigateTo(string pageKey)
{
NavigateTo(pageKey, null);
}
/// <summary>
/// Displays a new page corresponding to the given key,
/// and passes a parameter to the new page.
/// Make sure to call the <see cref="Configure" />
/// method first.
/// </summary>
/// <param name="pageKey">
/// The key corresponding to the page
/// that should be displayed.
/// </param>
/// <param name="parameter">
/// The parameter that should be passed
/// to the new page.
/// </param>
/// <exception cref="ArgumentException">
/// When this method is called for
/// a key that has not been configured earlier.
/// </exception>
public void NavigateTo(string pageKey, object parameter)
{
lock (_pagesByKey)
{
if (!_pagesByKey.ContainsKey(pageKey))
{
throw new ArgumentException(
string.Format(
"No such page: {0}. Did you forget to call NavigationService.Configure?",
pageKey),
"pageKey");
}
var shell = ((AppShell) Window.Current.Content);
shell.AppFrame.Navigate(_pagesByKey[pageKey], parameter);
}
}
/// <summary>
/// Adds a key/page pair to the navigation service.
/// </summary>
/// <param name="key">
/// The key that will be used later
/// in the <see cref="NavigateTo(string)" /> or <see cref="NavigateTo(string, object)" /> methods.
/// </param>
/// <param name="pageType">The type of the page corresponding to the key.</param>
public void Configure(string key, Type pageType)
{
lock (_pagesByKey)
{
if (_pagesByKey.ContainsKey(key))
{
throw new ArgumentException("This key is already used: " + key);
}
if (_pagesByKey.Any(p => p.Value == pageType))
{
throw new ArgumentException(
"This type is already configured with key " + _pagesByKey.First(p => p.Value == pageType).Key);
}
_pagesByKey.Add(
key,
pageType);
}
}
}
基本上是他的实现的副本。但是我不是解析到一个Frame,而是解析到一个AppShell,并使用AppFrame属性来导航。
我把这个放到我的ViewModelLocator。而不是:
var navigationService = new NavigationService();
我将使用:
var navigationService = new PageNavigationService();
编辑:我注意到在NavMenuListView中有一个例外,当你使用新的navigationservice导航后使用背键,因为所选的项目是空的。我通过调整SetSelectedItem方法并在转换后的for循环中添加nullcheck来修复它:
public void SetSelectedItem(ListViewItem item)
{
var index = -1;
if (item != null)
{
index = IndexFromContainer(item);
}
for (var i = 0; i < Items.Count; i++)
{
var lvi = (ListViewItem) ContainerFromIndex(i);
if(lvi == null) continue;
if (i != index)
{
lvi.IsSelected = false;
}
else if (i == index)
{
lvi.IsSelected = true;
}
}
}
但是可能有比这更优雅的解决方案