标题是我的问题。我将在下面解释。
我正在开发 wpf 应用程序是 vs2010。我有两个窗口,一个是我的主窗口,另一个是文件列表窗口。在我的 fileList 窗口中,我有一个文件列表,单击它应该加载文件。 onClick 方法在 fileList 类中实现。加载文件的功能在 MainWindow 分部类中实现。
我的 fileList 类在 MainWindow 类中实例化以显示窗口。我无法再次实例化 MainWidow。 MainWindow 中的函数(方法)不能声明为静态的,因为它使用了我不能(不知道如何)声明为静态的其他参数。
我在下面粘贴相关代码。请帮忙。
namespace test
{
public partial class MainWindow : Window
fileList fl = new fileList;
public MainWindow()
{
InitializeComponent();
fl.show();
}
public void porcessfile(string path)
{
//this method processes the the file at "path". It uses combobox and scrollviewer
//declared in xaml. I dont know how to declare static in xaml, else I will declare
//them static and change the whole method to static, so I can call it without
//instantiating. I tried making a nested-class, but then I can't access variable
//declared in MainWindow (parent) class. Or there is a way to do that?
}
}
和另一个类:
namespace test
{
public partial class fileList : Window
{
public fileList()
{
IntializeComponent();
}
private void Button_click(object sender, RoutedEventsArgs e)
{
//code that gets "path" on click, works fine.
processfile(string path); // what and how to do here.
}
}
}
我真诚地希望我是清楚的。如有需要请询问详情。
最佳答案
好吧,最简单的解决方案是简单地为您的 Filelist 窗口提供一个构造函数,该构造函数接受一个指向 Mainwindows 中的 processfile 方法的委托(delegate)。 看这篇文章:http://www.codeproject.com/Articles/109000/C-Delegates-Step-by-Step
让它成为 statis 不是解决方案 - 这将是一个非常丑陋的 hack,它会比委托(delegate)造成更多的麻烦。
关于c# - 在不实例化的情况下从另一个类访问非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17465713/