2013-07-04 91 views
1

標題是我的問題。我將在下面解釋。無需實例化訪問另一個類的非靜態方法

我工作的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. 
    } 
    } 

} 

我衷心希望我很清楚。如有需要請詢問詳情。

+1

如果您想訪問沒有對象的類方法,您需要使類爲'static'。 –

+0

我知道。但後來我需要更改太多的代碼。我不知道如何在xaml中聲明一個新的組合框爲靜態的,因爲datacontext是爲combobox設置的,它改變了我的porcessfile方法。 – Naresh

+0

是的。這不會工作,因爲我有綁定在MainWindow中設置,並通過processfile方法更改屬性。我必須將很多東西傳遞給新課堂。 – Naresh

回答

1

好的,這應該相當容易。你只需要在你的FileList類中聲明一個事件,該事件在你的Button_click方法中發送,併發送文件路徑並從MainWindow訂閱它,然後用你剛收到的參數調用你的processfile方法。

在您的文件清單類:

public event EventHandler<EventArgs<string>> PathReceived = delegate { }; 

在Button_click發佈此。

在你MainWindow類的cosntructor:

this.fileList.PathReceived = (o,args) => this.ProcessFile(args.Value); 

發佈代碼:

this.PathReceived(null, new EventArgs<string>(yourPath)); 

編輯: 我忘了向您提供EventArgs類(這是從我的一個老項目)。

public class EventArgs<T> : EventArgs 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="EventArgs{T}"/> class. 
    /// </summary> 
    /// <param name="value">The value.</param> 
    public EventArgs(T value) 
    { 
     Value = value; 
    } 

    /// <summary> 
    /// Gets the value. 
    /// </summary> 
    /// <value> 
    /// The value. 
    /// </value> 
    public T Value { get; private set; } 
} 
+0

我試着說你的話。它並不那麼簡單。說,我在MainWindow中獲得了路徑,這很容易。但後來我需要調用另一個將使用該路徑的方法。那麼它變得醜陋。我不是專家,如果你能詳細說明,可能是我缺少一些非常基本的東西。 – Naresh

+0

現在讓我知道這個工作。另外不要忘記,如果你不想實例化你的FileList類,你總是可以聲明靜態事件。 –

+0

靜態不是一個選項。我正在瀏覽你的代碼。 – Naresh

3

那麼,最簡​​單的解決方案是簡單地給你的文件列表窗口一個構造函數,它接受一個指向你的主窗口中的processfile方法的委託。 看這篇文章:http://www.codeproject.com/Articles/109000/C-Delegates-Step-by-Step

使它的統計不是解決方案 - 這將是一個非常醜陋的黑客,這會比委託造成更多的麻煩。

+0

我會通過鏈接。謝謝。 – Naresh

+0

是的,代表是要走的路。謝謝。我接受了下面的代碼,因爲這個人實際上編碼,閱讀代表 – Naresh

1

沒有爲你的應用程序的所有窗口的靜態方便的訪問屬性:

Application.Current.Windows

然後簡單地取第一個(或找出正確的,如果你有一個以上的),並轉換爲您的MainWindow類型。現在你有一個實例來調用你的方法。

0

雖然它是一個反模式的東西(因爲它類似於全局變量和持續狀態,這使得測試更加困難),你可以使用Singleton模式,在這裏:

public partial class MainWindow { 
    private static MainWindow instance = new MainWindow(); 

    public static MainWindow Instance { get { return instance; } } 

    private FileList fl = new fileList(); 

    private MainWindow() { 
    InitializeComponent(); 
    fl.show(); 
    } 
} 

你的文件列表然後可以使用MainWindow.Instance

但是,這具有部分隱藏兩個類之間的依賴關係的副作用。你真正想要做的是在構造函數中需要一個MainWindow實例到fileList。這使得依賴關係顯而易見,併爲使用框架打開了大門(這將幫助您實現可測試性)。

另外,C#約定是調用類FileList而不是fileList

+0

我試過了。沒有工作。實際上我有兩個filelist的窗口。我可以嘗試在filelist中構建mainwindows實例,但是再次,問題可能會出現相反的情況,我們只是在這種情況下翻轉硬幣。 – Naresh

+0

這是不是很清楚你在做什麼。聽起來你需要兩個類之間的雙向引用,而'static'只是一個解決方法。我推薦我在回答中提到的內容:喜歡使用靜態方式傳遞引用。 – ashes999

+0

是的。 ref是一種方式。我用代表,它爲我工作。非常感謝。 – Naresh

0

我這樣做是爲了讓一個方法在我的一個類中運行而不需要做整個變量設置。

string res = (new myClass()).methodInsideMyclass(); 
相關問題