从子应用程序共享属性到基础项目

时间:2014-01-23 06:55:47

标签: c# visual-studio-2012 windows-8 windows-phone-8

我正在开发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中获取该值。

有什么方法可以实现这个

1 个答案:

答案 0 :(得分:1)

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");
相关问题