【问题标题】:How to display page from library project如何从库项目中显示页面
【发布时间】:2014-01-22 12:48:12
【问题描述】:

我有一个基础项目,它是 Windows Phone Class Library,这是我的图书馆项目,因为我有以下内容:

1) MainPage.xaml 2) AppSetting.cs

class AppSettings 
    {
        public virtual string getAppName()
        {
            return "AAA"
        }

        public virtual int getAppVersion()
        {
            return 1
        }
    }

我有一个子应用程序 - Windows Phone App

我如何引用了基础项目?右键,添加引用。

我这里也有 AppSettings :

using BaseProject;
class AppSettings : AppSettings
        {
            public override string getAppName()
            {
                return "ABC"
            }

            public override int getAppVersion()
            {
                return 20
            }
        }

现在,当我运行我的子应用程序时,它应该显示来自 Base Project 的 MainPage.xaml,并且它应该从子应用程序而不是从 BaseProject 获取设置。

如何为 Windows Phone 8 应用程序实现这一点?

编辑 1

好的,我可以使用以下代码从 Base 项目中显示 MainPage:

NavigationService.Navigate(new Uri("/BaseProject;component/MainPage.xaml", UriKind.Relative));

但是现在如何使用子项目的方法呢?这是在 AppSettings.cs

编辑 2

我如何显示这些值?我在 Base Project 的 MainPage.cs 中创建了一个 AppSettings 对象

AppSettings appSettings = new AppSettings();
            string appName = appSettings.getAppName();
            int appVersion = appSettings.getAppVersion();

            App_Name.Text = appName;
            App_Version.Text = "" + appVersion;

【问题讨论】:

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


    【解决方案1】:

    如果您想在引用库的代码中使用您描述为“子”库的方法,则需要避免创建循环引用依赖项。这意味着您的子库将需要实现在父库中定义的抽象/基类或接口,并将其实例化并将其分配给父库知道的容器。一种方法是在您的 Application 类中。例如:

    namespace BaseProject
    {
        public class MyBaseApplication : System.Windows.Application
        {
             // A container the parent library can access
             public BaseProject.AppSettings Settings { get; set; }
    
             public MyBaseApplication()
             {
                 this.Settings = new BaseProject.AppSettings();
             }
        }
    
        public class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage
        {
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                var myApp = System.Windows.Application.Current as MyBaseApplication;
    
                // Will contain an instance of ChildProject.AppSettings
                var mySettings = myApp.Settings;
    
                string appName = appSettings.getAppName();
                int appVersion = appSettings.getAppVersion();
    
                App_Name.Text = appName;
                App_Version.Text = "" + appVersion;
            }
        }
    }
    
    namespace ChildProject
    {
        public class AppSettings : BaseProject.AppSettings
        {
            // Derived implementation
        }
    
        public class MyChildApplication : BaseProject.MyBaseApplication
        {
            public MyChildApplication() : base()
            {
                 // Overwrite the default assignment from the parent class with a new assignment
                 this.Settings = new ChildProject.AppSettings();
            }
        }
    }
    

    【讨论】:

    • 这是 System.Windows.Application,本质上是您的 Windows Phone 应用程序。您可以将它分配给您的父库真正知道的任何内存位置。这只是一个例子。对于替代解决方案,您可以创建一个静态 AppSettingsManager 类,该类具有您将派生实例分配给的静态设置属性。唯一的要求是命名的内存位置必须是父库可以引用的位置。
    • 我有点困惑,请解释一下,哪个是子应用程序,哪个是库项目?
    • 我猜这里的 CoreLibrary 是子应用程序?如果是这样,问题是我有 2 个这样的不同应用程序,那么我不能使用 CoreLibrary,因为我的第二个子应用程序名称可能不同,我的意思是命名空间
    • 你太关注细节了。唯一的一点是,您需要在 BaseProject 库中的某个位置存储 AppSettings 的实例。 BaseProject 将创建 AppSettings 的默认实例,而您的 ChildProject 将从其自己的实现创建 AppSettings 的新实例并将其分配给该属性。我正在对代码进行编辑以进行演示。
    • 请稍等,我会尝试用你的代码让你,你能等我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    相关资源
    最近更新 更多