【发布时间】:2014-01-23 06:55:46
【问题描述】:
我正在开发 Windows Phone 应用程序:我有一个需求。
我有 3 个项目,
1) BaseLibrary 项目 - 库项目即电话类库项目
2) ChildApp1 - Winodws 电话应用程序
3) ChildApp2 - Windows Phone 应用程序
现在在我的 BaseLibrary 中,我拥有所有 .xaml 和 .cs 文件。在我的子应用程序中,我拥有一个 .cs 文件
这里的想法是我的两个子项目 UI 完全相同,因此我在库项目中添加了所有常见的东西并将其引用到我的子应用程序中。
现在,我的子应用几乎没有变化。
在 BaseLibrary 中:
MainPage.xaml 文件;
namespace BaseLibrary
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
AppSettings appSettings = new AppSettings();
string appName = appSettings.getAppName();
int appVersion = appSettings.getAppVersion();
App_Name.Text = appName;
App_Version.Text = "" + appVersion;
}
}
}
在 AppSettingsDefaultImpl 中:
namespace BaseLibrary
{
public class AppSettingsDefaultImpl
{
public virtual String getAppName()
{
return "XYZ";
}
public virtual int getAppVersion()
{
return 1;
}
}
}
在应用设置中
namespace BaseLibrary
{
class AppSettings : AppSettingsDefaultImpl
{
public virtual string getAppName()
{
return getAppName();
}
public virtual int getAppVersion()
{
return getAppVersion();
}
}
}
在我的子项目中:
namespace ChildApp1
{
using BaseLibrary;
class AppSettings : AppSettingsDefaultImpl
{
public override string getAppName()
{
return "ABC";
}
}
}
现在,当我运行我的子项目时,我将其导航到 BaseLibrary 项目中的 MainPage.xaml。
但在 .xaml 文件中我有这个:
AppSettings appSettings = new AppSettings();
string appName = appSettings.getAppName();
int appVersion = appSettings.getAppVersion();
App_Name.Text = appName;
App_Version.Text = "" + appVersion;
其中 AppSettings 是 BaseLibrary 的一个实例,因此它将从 BaseLibrary 的 AppSettings 中的方法中获取值,但我希望它从子项目中获取实例并显示我在那里拥有的值,如何做吗?
我得到的输出:
1,XYZ
我想要的输出是:
1,ABC
如果该属性未在子应用中定义,则应从 BaseLibrary 中获取值。
有什么方法可以实现这个
【问题讨论】:
-
我建议使用 MEF 或 Unity 或其他容器在 BaseLibrary 项目中注入适当的 AppSettings 类。
-
@RezaArab 你能指导我吗,因为我是新手
-
MEF:总而言之,您必须将
ChildApp1.AppSettings注册为AppSettingsDefaultImpl类型并带有[Import(typeof(AppSettingsDefaultImpl))]属性,而不是new AppSettings()使用container.GetExport<AppSettingsDefaultImpl>() -
对不起,你能给个样品吗?我完全糊涂了,
标签: c# visual-studio-2012 windows-8 windows-phone-8