Share Properties from Child app to Base project

2019-01-29 08:17发布

I am Working on Windows Phone app Development: i have a requirement here.

i have 3 project,

1) BaseLibrary project - library project i.e., Phone Class Library project

2) ChildApp1 - Winodws Phone app

3) ChildApp2 - Windows Phone app

Now in my BaseLibrary i have all the .xaml and .cs files.In my child apps i have one .cs file

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.

In BaseLibrary :

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;
        }
    }
}

In AppSettingsDefaultImpl :

namespace BaseLibrary
{
    public class AppSettingsDefaultImpl
    {
        public virtual String getAppName()
        {
            return "XYZ";
        }

        public virtual int getAppVersion()
        {
             return 1;
        }
    }
}

In AppSettings

namespace BaseLibrary
{
    class AppSettings : AppSettingsDefaultImpl
    {
        public virtual string getAppName()
        {
            return getAppName();
        }

        public virtual int getAppVersion()
        {
            return getAppVersion();
        }
    }
}

In my child projects:

namespace ChildApp1
{
    using BaseLibrary;
    class AppSettings : AppSettingsDefaultImpl
    {
        public override string getAppName()
        {
            return "ABC";
        }

    }
}

Now i when i run my child project i am navigating it to MainPage.xaml in BaseLibrary project.

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;

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 ?

Output i am getting :

1, XYZ

Output want i want is :

1,ABC

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

1条回答
甜甜的少女心
2楼-- · 2019-01-29 09:15
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";
            }

        }
}

Then:

CompositionContainer container;// override MefBootstrapper

AppSettingsDefaultImpl appSettings = 
    container.GetExport<AppSettingsDefaultImpl>("child");
查看更多
登录 后发表回答