尝试使用我发现的此解决方案通过代码隐藏来实现 NavigationView 导航:https://xamlbrewer.wordpress.com/2021/07/06/navigating-in-a-winui-3-desktop-application/
我收到此编译错误:
错误是说我应该“投射”m_window;该示例未投射。为什么我需要投射?
我创建了一个界面:(INavigation.cs)
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MetricReporting.Interfaces
{
interface INavigation
{
NavigationViewItem GetCurrentNavigationViewItem();
List<NavigationViewItem> GetNavigationViewItems();
List<NavigationViewItem> GetNavigationViewItems(Type type);
List<NavigationViewItem> GetNavigationViewItems(Type type, string title);
void SetCurrentNavigationViewItem(NavigationViewItem item);
}
}
我创建了主窗口的部分类(MainWindow.xaml.Navigation.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MetricReporting
{
public partial class MainWindow : INavigation
{
public NavigationViewItem GetCurrentNavigationViewItem()
{
return NavigationView.SelectedItem as NavigationViewItem;
}
public List<NavigationViewItem> GetNavigationViewItems()
{
var result = new List<NavigationViewItem>();
var items = NavigationView.MenuItems.Select(i => (NavigationViewItem)i).ToList();
items.AddRange(NavigationView.FooterMenuItems.Select(i => (NavigationViewItem)i));
result.AddRange(items);
foreach (NavigationViewItem mainItem in items)
{
result.AddRange(mainItem.MenuItems.Select(i => (NavigationViewItem)i));
}
return result;
}
public List<NavigationViewItem> GetNavigationViewItems(Type type)
{
return GetNavigationViewItems().Where(i => i.Tag.ToString() == type.FullName).ToList();
}
public List<NavigationViewItem> GetNavigationViewItems(Type type, string title)
{
return GetNavigationViewItems(type).Where(ni => ni.Content.ToString() == title).ToList();
}
public void SetCurrentNavigationViewItem(NavigationViewItem item)
{
if (item == null)
{
return;
}
if (item.Tag == null)
{
return;
}
MasterContentFrame.Navigate(
Type.GetType(item.Tag.ToString()),
item.Content);
NavigationView.Header = item.Content;
NavigationView.SelectedItem = item;
}
}
}
我将此行添加到我的 App.xaml.cs 文件中:
public INavigation Navigation => m_window;
它模拟了文章中的线条:
private Shell shell;
public INavigation Navigation => shell;
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
shell = new Shell();
shell.Activate();
}
我应该能够像这样使用导航服务:
All parts of the code base can now easily access the lightweight Navigation Service:
(Application.Current as App).Navigation
为了清楚起见,这是我的 App.xaml.cs
using MetricReporting.Interfaces;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Navigation;
namespace MetricReporting
{
public partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
///
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}
private Window m_window;
public INavigation Navigation => m_window; //Compiler does not like this
//public INavigation Navigation => m_window; Compiler does not like this
}
}
m_window
是一个 Window
...所以它可能是 any Window
而不一定是你的 MainWindow
,或者任何实现 INavigation
的东西。这就是构建的所有信息。
你可以用以下方法修复它:
private MainWindow m_window;
您需要将
m_window
转换为 INavigation
才能编译代码:
public INavigation Navigation => m_window as INavigation;
或者按照 @rfmodulator (+1) 的建议直接将
m_window
字段定义为 INavigation
:
private MainWindow m_window;
我运行了相同的代码,错误是隐式继承了错误的接口。
在下面的行中,接口
INavigation
来自命名空间:namespace Microsoft.EntityFrameworkCore.Metadata
。
public INavigation Navigation => m_window;
只需直接指定您正在使用哪个接口,例如,如果您想使用您的实现:
public MetricReporting.Services.Interfaces.INavigation Navigation => m_window;
或更改您的界面名称,例如
IMainWindowsNavigation
。