標題是我的問題。我將在下面解釋。無需實例化訪問另一個類的非靜態方法
我工作的wpf應用程序是vs2010。我有兩個窗口,一個是我的MainWindow,另一個是fileList窗口。在我的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.
}
}
}
我衷心希望我很清楚。如有需要請詢問詳情。
如果您想訪問沒有對象的類方法,您需要使類爲'static'。 –
我知道。但後來我需要更改太多的代碼。我不知道如何在xaml中聲明一個新的組合框爲靜態的,因爲datacontext是爲combobox設置的,它改變了我的porcessfile方法。 – Naresh
是的。這不會工作,因爲我有綁定在MainWindow中設置,並通過processfile方法更改屬性。我必須將很多東西傳遞給新課堂。 – Naresh