简体   繁体   English

如何从 ResourceDictionary 类 (WPF) 导航到 xaml 页面

[英]How to navigate to a xaml page from ResourceDictionary class (WPF)

I have a ResourceDictionary class as follows:我有一个 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)); } } }

The imageCloseMyApp function is invoked from an image by clicking on it and I wish to call another page. imageCloseMyApp 函数是通过单击图像从图像调用的,我希望调用另一个页面。

And I get the following error before compiling the project:在编译项目之前,我收到以下错误:

Severity Code Description Project File Line Status deleted Error CS0120 An object reference is required for the field, method or property 'NavigationService.Navigate (Uri)' not static myApp H:\\pro\\Visual_Studio\\myApp\\myApp\\Classes\\ FunctionsMenu.cs 35 Active严重性代码 描述 项目文件行状态已删除 错误 CS0120 字段、方法或属性“NavigationService.Navigate (Uri)”需要对象引用,而不是静态 myApp H:\\pro\\Visual_Studio\\myApp\\myApp\\Classes\\FunctionsMenu.cs 35积极的

I have searched the internet, and tried the following options:我在互联网上搜索过,并尝试了以下选项:

 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));

But none works.但没有一个有效。

Any suggestions or comments?有什么建议或意见吗?

I have achieved it as follows:我已经实现了如下:

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

I guide myself to understand the following thread:我引导自己理解以下线程:

How do you get the parent control from an event send by a control in the resources 如何从资源中的控件发送的事件中获取父控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM