覆盖 App.xaml.cs 中的方法值 [英] Overriding methods values in App.xaml.cs
问题描述
我正在开发 Windows 8 Phone 应用程序,这里有两件事,一个是库项目,另一个是普通应用程序,让我先解释一下我的代码:
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:
图书馆项目
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.
在子应用程序中
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
,这是每个项目的入口点.
Now in every Child Application we App.xaml.cs
which is the entry point of each project.
在这个 App.xaml.cs
文件中,我正在创建这个 Properties 的对象并调用 setValues 方法.
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 ?
我得到了 ASDF 并列出了亚当和夏娃作为输出
I get the ASDF and list with Adam and Eve as output
但我需要 ASDF 并列出 Kyla 和 Sophia 作为输出.
But i need ASDF and list with Kyla and Sophia as output.
如何实现?
编辑
我如何使用这些值:
在我的基地:
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.
你不能用经典的 C# 机制来做到这一点,因为你需要依赖注入.一些类似的东西:
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屋!