【问题标题】:How to navigate to a xaml page from ResourceDictionary class (WPF)如何从 ResourceDictionary 类 (WPF) 导航到 xaml 页面
【发布时间】:2020-02-23 18:19:27
【问题描述】:

我有一个 ResourceDictionary 类如下:

  using System.Windows.Navigation;
    using static myApp.filesXAML.Login;

    namespace myApp.Clases
    {
        partial class functionsMenu : ResourceDictionary
        {
            private void imageCloseMyApp(object sender, MouseButtonEventArgs e)
            {
NavigationService.Navigate(new Uri("filesXAML/Login.xaml", UriKind.Relative));
            }
        }
    }

通过单击图像从图像调用 imageCloseMyApp 函数,我希望调用另一个页面。

并且在编译项目之前出现以下错误:

严重性代码描述项目文件行状态已删除错误 CS0120 字段、方法或对象需要对象引用 属性“NavigationService.Navigate (Uri)”不是静态的 myApp H:\pro\Visual_Studio\myApp\myApp\Classes\ FunctionsMenu.cs 35 活动

我已经搜索了互联网,并尝试了以下选项:

Login page = new Login();         
page.NavigationService.Navigate(new Uri("filesXAML/Login.xaml",UriKind.RelativeOrAbsolute));

// or

NavigationService nav = NavigationService.GetNavigationService(Application.Current.Windows[0].Parent);
nav.Navigate(new Uri("filesXAML/Login.xaml", UriKind.Relative));

但没有一个有效。

有什么建议吗?

【问题讨论】:

  • 您想使用什么NavigationService 进行导航?您的应用程序中是否有Frame
  • 这是一个页面。感谢您的来信
  • 什么是Page
  • 这是 MainPage 构造函数
  • 我不明白。请编辑您的问题并包含所有详细信息。您当前的问题没有多大意义。您的代码中没有设置NavigationService

标签: c# wpf resourcedictionary navigationservice


【解决方案1】:

我实现了如下:

 NavigationService nav = NavigationService.GetNavigationService((Grid)((Image)sender).Parent);
nav.Navigate(new Uri("filesXAML/Login.xaml", UriKind.Relative));

我引导自己理解以下线程:

How do you get the parent control from an event send by a control in the resources

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多