简体   繁体   English

如何访问 Class 库中的“导入器”类? (.NET MAUI/C#)

[英]How do I access the "importer" classes in a Class Library? (.NET MAUI/C#)

I have developed a .NET MAUI app for my company.我为我的公司开发了一个 .NET MAUI 应用程序。 To do so, I created some "Core" classes in the project that helped me develop the project.为此,我在项目中创建了一些“核心”类来帮助我开发项目。

For example, I created a "Navigation" class that handled page navigation in my project.例如,我在我的项目中创建了一个“导航”class 来处理页面导航。

I have been asked to develop a second app, and I wanted to reuse some of the "Core" classes, since they would be useful in this project as well.我被要求开发第二个应用程序,我想重用一些“核心”类,因为它们在这个项目中也很有用。

I created a .NET MAUI Class Library (to prevent just copypasting the classes).我创建了一个 .NET MAUI Class 库(以防止只是复制粘贴类)。 The problem is that I want to access some of the classes in the project that is going to import my class library.问题是我想访问项目中要导入我的 class 库的一些类。

Following with the above "Navigation" example, it would work like this if the class is in the App1 project.按照上面的“导航”示例,如果 class 在 App1 项目中,它将像这样工作。

destinationNavPage = new NavigationPage(new WhateverPage());
App.Current.MainPage = destinationNavPage;

This works fine.这工作正常。 It changes the active page to a new specified page.它将活动页面更改为新的指定页面。

However, you can't do this in a class library, since both "WhateverPage" and "App" are in the App namespace, and the class library does not know they exist.但是,您不能在 class 库中执行此操作,因为“WhateverPage”和“App”都在 App 命名空间中,并且 class 库不知道它们存在。

Since the "WhateverPage" is going to be a passed argument to the function and all default MAUI projects instantiate the "App" class, I can be completely sure these will both exist in the destination app.由于“WhateverPage”将成为 function 的传递参数,并且所有默认 MAUI 项目都会实例化“App”class,因此我可以完全确定它们都将存在于目标应用程序中。

The problem is, how do I access these classes in the project/namespace that is going to import my class library, from the class library?问题是,如何从 class 库导入我的 class 库的项目/命名空间中的这些类?

Thanks in advance.提前致谢。

By definition, a class library does not, and cannot, know anything about the client project that uses it.根据定义,class 库不知道也不可能知道有关使用它的客户端项目的任何信息。

The solution is to create something in the library, that the client calls during app startup , to tell the library what it needs to know.解决方案是在库中创建一些东西,客户端在应用程序启动期间调用,告诉库它需要知道什么。


SOLUTION 1: Define an Interface解决方案 1:定义接口

In OO languages, one simple mechanism to do so is to define an interface in the library, that the client project should implement.在 OO 语言中,这样做的一种简单机制是在库中定义一个接口,客户端项目应该实现该接口。

Define in your library:在您的库中定义:

namespace MyLibrary;
public interface ICallMe
{
    void TheCall(object aParameter);
}
public class StartMe
{
    static ICallMe Callback;
    public static void Init(ICallMe callback)
    {
        Callback = callback;
    }

    // Usage:
    Callback.TheCall(...);

Define in your main project:在您的主项目中定义:

class MyCallMe : MyLibrary.ICallMe
{
    public void TheCall(object aParameter)
    {
        ...
    }
}

In your app startup:在您的应用启动中:

MyLibrary.StartMe.Init(new MyCallMe());

If you want to get at functionality that can't be re-defined to implement MyLibrary.ICallMe , then make a wrapper class.如果您想获得无法重新定义以实现MyLibrary.ICallMe的功能,请制作一个包装器 class。 I'll show how to set App.Current.MainPage .我将展示如何设置App.Current.MainPage

public interface ICallMe
{
    void SetCurrentPage(Page newPage);
}

    // Usage (assumes "Callback" defined above):
    Callback.SetCurrentPage(...);

Wrapper class in main app project:主应用程序项目中的包装器 class:

class MyLibraryCallbacks: MyLibrary.ICallMe
{
    public void SetCurrentPage(Page newPage)
    {
        App.Current.MainPage = newPage;
    }

Modify as needed.根据需要进行修改。


SOLUTION 2: Dependency Injection解决方案 2:依赖注入

TBD: Maybe there is a way to apply Maui's Dependency Injection to the library. TBD:也许有一种方法可以将 Maui 的依赖注入应用到库中。

If so, then during app startup you'd have code setting up "MyLibraryCallbacks" as the implementor of "MyLibrary.ICallMe".如果是这样,那么在应用程序启动期间,您将有代码将“MyLibraryCallbacks”设置为“MyLibrary.ICallMe”的实现者。

I don't know if this is possible.我不知道这是否可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM