C# WinUI 3 获取 CS0053 错误不一致的可访问性:属性 类型 'INavigation' 的可访问性低于 属性 'App.Navigation'

C# WinUI 3 Get CS0053 error Inconsistent accessibility: property type 'INavigation' is less accessible than property 'App.Navigation'

尝试使用此解决方案通过代码隐藏实现 NavigationView 导航,我发现: https://xamlbrewer.wordpress.com/2021/07/06/navigating-in-a-winui-3-desktop-application/ 我不知道我做错了什么。 :-(。 我是 C# 新手,WinUI。 'App.Navigation'在哪里?????

解决方案概要:

  1. 所有与导航相关的代码都在 Shell window、
  2. 的部分 class 中实现
  3. 封装在
  4. 的接口中
  5. 通过 App 实例暴露给
  6. 不同的 XAML 页。

我创建了一个接口:(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);
    }
}

我创建了主要 window (MainWindow.xaml.Navigation.cs)

的部分 class
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:

using MetricReporting.Interfaces;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace MetricReporting
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    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>
        /// 

        // ** this is what I added per the web article ** 
        public INavigation Navigation => (INavigation)m_window;
        //public INavigation Navigation => m_window;   Compiler does not like this

        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            m_window = new MainWindow();
            m_window.Activate();
        }

        private Window m_window;
    }
}

这是我的 MainWindow.xaml.cs 以防有帮助。 NavigationView 代码运行良好,我可以通过导航菜单导航到所有页面 UI。 :-)。我只需要能够从后面的代码中导航。很抱歉冗长 post.

    using MetricReporting.Pages;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Diagnostics;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace MetricReporting
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.Title = "EMB Metric Reporting Tool";
            MasterContentFrame.Navigate(typeof(pageHome));
            MasterNavigation.Header = "Home";
        }

        private void MasterNavigation_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            if (args.IsSettingsInvoked)
            {
                MasterContentFrame.Navigate(typeof(pageSettings));
            }
            else
            {
                // find NavigationViewItem with Content that equals InvokedItem
                NavigationViewItem item = sender.MenuItems.OfType<NavigationViewItem>().First(x => (string)x.Content == (string)args.InvokedItem);
                NavView_Navigate(item);
            }
        }

        private void NavView_Navigate(NavigationViewItem item)
        {
            switch (item.Tag)
            {
                case "home":
                    MasterContentFrame.Navigate(typeof(pageHome));
                    MasterNavigation.Header = item.Content;
                    break;

                case "datacollection":
                    MasterContentFrame.Navigate(typeof(pageDataCollection));
                    MasterNavigation.Header = item.Content;
                    break;

                case "collectdata":
                    MasterContentFrame.Navigate(typeof(PageCollectMetricData));
                    MasterNavigation.Header = item.Content;
                    break;

                case "goals":
                    MasterContentFrame.Navigate(typeof(pageGoals));
                    MasterNavigation.Header = item.Content;
                    break;

                case "approvals":
                    MasterContentFrame.Navigate(typeof(pageComments));
                    MasterNavigation.Header = item.Content;
                    break;

                case "reports":
                    MasterContentFrame.Navigate(typeof(pageReports));
                    MasterNavigation.Header = item.Content;
                    break;

                case "admin":
                    MasterContentFrame.Navigate(typeof(pageAdmin));
                    MasterNavigation.Header = item.Content;
                    break;

                case "metricstaging":
                    MasterContentFrame.Navigate(typeof(pageMetricStaging));
                    MasterNavigation.Header = item.Content;
                    break;

                case "refmetricmain":
                    MasterContentFrame.Navigate(typeof(pageRefMetricMain));
                    MasterNavigation.Header = item.Content;
                    break;
            }
        }

        private async void DisplayCommentDialog()
        {
            ContentDialog testDialog = new ContentDialog()
            {
                Title = "Main Window Loaded",
                Content = "Main Window Loaded",
                CloseButtonText = "Ok"
            };
            await testDialog.ShowAsync();
        }
    }
}

错误信息告诉你 INavigation 必须是 public:

public interface INavigation
{
    NavigationViewItem GetCurrentNavigationViewItem();

    List<NavigationViewItem> GetNavigationViewItems();

    List<NavigationViewItem> GetNavigationViewItems(Type type);

    List<NavigationViewItem> GetNavigationViewItems(Type type, string title);

    void SetCurrentNavigationViewItem(NavigationViewItem item);
}

如果您将其定义为 interface INavigation { ... }(不带 public 关键字),则默认为 internal,不能用作 return 类型的 public 成员.

所以在代码中将 interface INavigation 更改为 public interface INavigation