簡體   English   中英

App.xaml.cs中的重寫方法值

[英]Overriding methods values in App.xaml.cs

我正在Windows 8 Phone應用程序上工作,這里有兩件事,一個是Library項目,另一個是正常的應用程序,讓我先解釋一下我的代碼:

在圖書館項目中

    class A
    {
      public static string empName ="ABC";
      public static int empID = 123;

      public virtual List<string> ListOfEmployees()
      {
           List<string> empList = new List<string>
           empList.Add("Adam");
           empList.Add("Eve");
           return empList;
      }

}

我在我的孩子項目中引用了圖書館項目,我的孩子和圖書館項目處於兩種不同的解決方案中。

在兒童申請中

class Properties : A
{

 public void setValues(){
       empName ="ASDF"
       ListOfEmployees();
}
  public override List<string> ListOfEmployees()
          {
               List<string> empList = new List<string>
               empList.Add("Kyla");
               empList.Add("Sophia");
               return empList;
          }
      }

現在,在每個子應用程序中,我們都是App.xaml.cs ,這是每個項目的入口。

在此App.xaml.cs文件中,我正在創建此Properties and calling setValues method.的對象Properties and calling setValues method.

我在這里看到的只是靜態變量值被覆蓋,但方法沒有被覆蓋。為什么呢? 我在這里做錯什么了嗎?

我得到了ASDF並列出了Adam和Eve作為輸出

但是我需要ASDF並列出Kyla和Sophia作為輸出。

如何實現呢?

編輯

我如何使用這些值:

在我的基地:

class XYZ : A

    {
      // now i can get empName as weel as the ListOfEmployees()
       string employeeName = null;

       public void bind()
       {
         employeeName = empName ; 
         ListOfEmployees(); // here is the bug where i always get Adam and Eve and not the Kyla and sophia
       }
    }

現在我了解了,您想從庫中的項目調用替代值。

您不能使用經典的C#機制來做到這一點,因為您需要依賴注入。 遵循以下原則:

// library
public interface IA
{
    List<string> ListOfEmployees();
}

public class ABase : IA
{
    public virtual List<string> ListOfEmployees() {}
}


public static class Repository
{
    private static IA _a;

    public static IA A
    {
        get { return _a = _a ?? new ABase(); }
        set { _a = value; }
    }
}

// in your app

class Properties : ABase
{
    public override List<string> ListOfEmployees() { /* ... */ }
}

Repository.A = new Properties();

將override關鍵字更改為new,您將獲得想要的行為。 請查看此鏈接 ,以獲取有關何時使用哪種鏈接的更多信息。

暫無
暫無

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

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