簡體   English   中英

從另一個類訪問非靜態方法而無需實例化

[英]accessing non-static method from another class without instantiate

標題是我的問題。 我將在下面解釋。

我正在研究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.
     }
  }

} 

我衷心希望我清楚。 如果需要,請詢問詳細信息。

好吧,最簡單的解決方案是為Filelist窗口提供一個構造函數,該構造函數接受一個委托,該委托指向Mainwindows中的processfile方法。 查看這篇文章: http : //www.codeproject.com/Articles/109000/C-Delegates-Step-by-Step

使其成為靜態狀態不是解決方案-這將是一個非常丑陋的hack,比委托產生更多的麻煩。

應用程序中的所有窗口都有一個靜態的便捷訪問屬性:

Windows應用程序

然后只需選擇第一個(如果有多個,則找出合適的一個)並轉換為MainWindow類型。 現在,您有了一個實例來調用您的方法。

好的,這應該很容易。 您只需要在FileList類中聲明一個事件,該事件會在Button_click方法中觸發,該方法發送文件路徑並從MainWindow進行預訂,然后使用剛剛收到的參數調用processfile方法。

在您的FileList類中:

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

將此發布在您的Button_click中。

在cosntructor的MainWindow類中:

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

盡管這是一種反模式(因為它類似於全局變量並保持狀態,這使測試更加困難),但您可以在此處使用單例模式:

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

我這樣做是為了讓一種方法可以在我的一個類中運行,而無需進行整個變量設置。

string res = (new myClass ()).methodInsideMyclass ();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM