从子应用程序共享属性到基础项目
我正在开发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; } } }
在AppSettings中
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中获取该值。
有什么方法可以实现这一点
namespace BaseLibrary { [Export("base",typeof(AppSettingsDefaultImpl))] class AppSettings : AppSettingsDefaultImpl { public override string getAppName() { return "ABC"; } } } namespace ChildApp1 { [Export("child",typeof(AppSettingsDefaultImpl))] class AppSettings : AppSettingsDefaultImpl { public override string getAppName() { return "ABC"; } } }
然后:
CompositionContainer container;// override MefBootstrapper AppSettingsDefaultImpl appSettings = container.GetExport("child");