在App.xaml.cs重写方法值 [英] Overriding methods values in App.xaml.cs
问题描述
我的工作在Windows 8 Phone应用程序,我有2个东西在这里一个是库项目等是正常的应用程序,让我先解释一下我的代码:
< STRONG>在库项目
A级
{
公共静态字符串empName = ABC;
公共静态INT EMPID = 123;
公共虚拟目录<串GT; ListOfEmployees()
{
名单,LT;字符串> empList =新的List<串GT;
empList.Add(亚当);
empList.Add(夏娃);
返回empList;
}
}
我引用的库项目我的孩子的项目,我的孩子和库项目有两种不同的解决方案。
儿童应用
类属性:A
{
公共无效setValues方法(){
empName =ASDF
ListOfEmployees();
}
公众覆盖列表<串GT; ListOfEmployees()
{
名单,LT;字符串> empList =新的List<串GT;
empList.Add(凯拉);
empList.Add(索菲亚);
返回empList;
}
}
现在在每个孩子申请我们 App.xaml.cs
这是每个项目的入口点。
在此 App.xaml.cs
文件我创造了这样的一个对象属性和调用setValues方法。
什么我在这里看到的仅仅是静态变量的值被覆盖,但该方法不overridden.Why这样呢?我在做什么错在这里?
我得到的ASDF和列表与亚当和夏娃作为输出
不过,我需要ASDF和清单,凯拉和索菲亚作为输出。
如何实现这一点?
修改
我是多么使用这些值:
在我的基地:
类XYZ:A
{
//现在我可以得到empName作为WEEL的ListOfEmployees( )
字符串employeeName = NULL;
公共无效的bind()
{
employeeName = empName;
ListOfEmployees(); //这里是我总是得到亚当和夏娃,而不是凯拉和索菲亚
}
}
$ B $的bug b
现在我明白了,你想从你的项目中调用重写值的在的磁带库。
您不能做到这一点与传统的C#机制,你需要依赖注入。沿着这些路线的内容:
//库
公共接口IA
{
名单,LT;字符串> ListOfEmployees();
}
公共类ABASE:IA
{
公共虚拟目录<串GT; ListOfEmployees(){}
}
公共静态类库
{
私有静态IA _a;
公共静态IA一个
{
{返回_a = _a?新ABASE(); }
集合{_a =价值; }
}
}
//在你的应用程序
级属性:ABASE
{
公众覆盖列表<串> ; ListOfEmployees(){/ * ... * /}
}
Repository.A =新特性();
I am working on Windows 8 Phone application,I have 2 things here one is Library project and other is normal app,Let me first explain my code:
In Library project
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;
}
}
I have referenced the library project in my child project, My child and Library project are in 2 different solution.
In Child application
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;
}
}
Now in every Child Application we App.xaml.cs
which is the entry point of each project.
In this App.xaml.cs
file i am creating an object of this Properties and calling setValues method.
What i see here is only static variables values are overridden but the methods are not overridden.Why so ? am i doing anything wrong here ?
I get the ASDF and list with Adam and Eve as output
But i need ASDF and list with Kyla and Sophia as output.
How to achieve this ?
EDIT
How i am using these values:
In my Base :
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
}
}
Now I understand, you want to call overriden values from your project in your library.
You cannot do that with classical C# mechanisms, for that you need dependency injection. Something along these lines:
// 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();
这篇关于在App.xaml.cs重写方法值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!