从子应用程序共享属性到基础项目 [英] Share Properties from Child app to Base project
问题描述
我正在从事 Windows Phone 应用开发:我在这里有一个要求.
I am Working on Windows Phone app Development: i have a requirement here.
我有 3 个项目,
1) BaseLibrary 项目 - 库项目,即电话类库项目
1) BaseLibrary project - library project i.e., Phone Class Library project
2) ChildApp1 - Winodws 手机应用
2) ChildApp1 - Winodws Phone app
3) ChildApp2 - Windows Phone 应用
3) ChildApp2 - Windows Phone app
现在在我的 BaseLibrary 中,我拥有所有 .xaml 和 .cs 文件.在我的子应用中,我有一个 .cs 文件
Now in my BaseLibrary i have all the .xaml and .cs files.In my child apps i have one .cs file
这里的想法是我的两个子项目 UI 完全相同,所以我在库项目中添加了所有常见的东西并将其引用到我的子应用程序中.
Idea behind here is that both my child project UI is exactly same, so i have added all the common stuff in a library project and referenced it to my child apps.
现在,我的子应用几乎没有变化.
Now, i have few changes in my child apps.
在 BaseLibrary 中:
In BaseLibrary :
MainPage.xaml 文件;
MainPage.xaml file;
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 中:
In 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.
Now i when i run my child project i am navigating it to MainPage.xaml in BaseLibrary project.
但是在 .xaml 文件中我有这个:
But in the .xaml file i have this:
AppSettings appSettings = new AppSettings();
string appName = appSettings.getAppName();
int appVersion = appSettings.getAppVersion();
App_Name.Text = appName;
App_Version.Text = "" + appVersion;
其中 AppSettings 是 BaseLibrary 的实例,因此它将从 BaseLibrary 的 AppSettings 中的方法中获取值,但我希望它从子项目中获取实例并显示我在那里拥有的值,如何才能做吗?
where AppSettings is an instance of BaseLibrary, so it will get the values from the methods which are in the AppSettings of BaseLibrary, but i want it to take the instance from child project and display the values which i have there, how can do it ?
我得到的输出:
1, XYZ
我想要的输出是:
1,ABC
如果该属性未在子应用中定义,则应采用 BaseLibrary 中的值.
If the property is not defined in the child app it should take the value from BaseLibrary.
有什么办法可以做到这一点
Is there any way that i can acheive this
推荐答案
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<AppSettingsDefaultImpl>("child");
这篇关于从子应用程序共享属性到基础项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!