繁体   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