从 WPF 中的页面内调用公共 MainWindow 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20661443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call a public MainWindow function from within a Page in WPF
提问by Harry Boy
I have a WPF Application that has a main window and several pages that I navigate to like so:
我有一个 WPF 应用程序,它有一个主窗口和几个我导航到的页面:
e.g From one page to another I use:
例如从一页到另一页我使用:
NavigationService.Navigate(new MyPage1());
And from my main window to show a page I use:
从我的主窗口显示我使用的页面:
_mainFrame.Navigate(new PageHome());
I have a public function on my MainWindow that I want to call from a within page.
我的 MainWindow 上有一个公共函数,我想从页面内调用它。
How do I do this??
我该怎么做呢??
回答by SnareHanger
You shouldn't call the method directly.
您不应该直接调用该方法。
What you should do is raise an event in your page and have the MainWindow subscribe to the event. That event handler can then call the method in question.
您应该做的是在您的页面中引发一个事件并让 MainWindow 订阅该事件。然后该事件处理程序可以调用有问题的方法。
In PageHome:
在主页中:
public event EventHandler SomethingHappened;
private void MakeSomethingHappen(EventArgs e){
if(SomethingHappened != null){
SomethingHappened(this, e);
}
}
In MainWindow:
在主窗口中:
pageHome.SomethingHappened += new EventHandler(pageHome_SomethingHappened);
void pageHome_SomethingHappened(object sender, EventArgs e){
MyMethod();
}
回答by sheraz yousaf
The following solution worked for me to call a MainWindow function from another page;
以下解决方案对我有用,可以从另一个页面调用 MainWindow 函数;
((MainWindow)System.Windows.Application.Current.MainWindow).FunctionName(params);
回答by Vishwaram Sankaran
Also there is one more technique using Registry this will be the perfect way to call a fucntion of one class from other from other class(required for classes split in multiple projects).
还有一种使用 Registry 的技术,这将是从其他类中调用一个类的功能的完美方式(需要在多个项目中拆分类)。
Make the public functions to be part of an Interface
interface ISomeInterface { void RequierdMethod();} public partial class RequiedImplementer: Window, ISomeInterface { void RequiredMethod() { } }Registry.RegisterInstance<ISomeInterface >(new RequiedImplementer()); //Initialize all interfaces and their corresponding in a common class.Call the apt functions anywhere in ur application like below
Registry.GetService<ISomeInterface>().RequiredMethod();
使公共函数成为接口的一部分
interface ISomeInterface { void RequierdMethod();} public partial class RequiedImplementer: Window, ISomeInterface { void RequiredMethod() { } }Registry.RegisterInstance<ISomeInterface >(new RequiedImplementer()); //Initialize all interfaces and their corresponding in a common class.在您的应用程序中的任何位置调用 apt 函数,如下所示
Registry.GetService<ISomeInterface>().RequiredMethod();
Here register class is custom created which just holds the instance and returns whenever neccessary. Interfaces should be referenced by all classes. This solution is more valid when you want interop by multiple projects.
这里的 register 类是自定义创建的,它只保存实例并在需要时返回。所有类都应该引用接口。当您希望通过多个项目进行互操作时,此解决方案更有效。
回答by csteinmueller
Although I would prefer the technique with events and delegates, I show another solution.
虽然我更喜欢使用事件和委托的技术,但我展示了另一种解决方案。
var mainWnd = Application.Current.MainWindow as MainWindow;
if(mainWnd != null)
mainWnd.Navigate(new MyPage1());

