多平台库项目中的 DependencyService? [英] DependencyService in Multiplatform library project?

查看:38
本文介绍了多平台库项目中的 DependencyService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 DependencyService 以在

在共享项目中调用 Dependency.Get() 时,我得到空值.

iOS 内部的 Service 似乎没有注册.

我尝试像这样注册它:[assembly: Dependency(typeof(Service))]- 在 iOS 项目中的命名空间声明之上.我已经开始在 Xamarin.Forms 项目中工作.而且我还尝试在 iOS 项目的 Init 方法中调用 DependencyService.Register(); - 从 AppDelegate 中的 Xamarin.Forms iOS 项目调用它 - 用于测试库.

Shared 项目和 iOS 项目似乎没有共享相同的 DependencyService.

如何从具有 DependencyService 的共享项目调用 iOS 项目中特定于平台的代码?

编辑

我在 windows 上的 Visual Studio 中工作时收到 System.PlatformNotSupportedException - 尝试在网络标准库项目中使用 DependencyService.会不会是visual studio for mac 根本不抛出错误而是返回null?

解决方案

你可以通过移除共享项目并更改来轻松解决问题

MyMutliplatformLibraryProject.Run.Test();

MyXamarinFormsProject.Run.Test();

如果您尝试使用自定义控件创建 Nuget,您可以按照 本教程如何操作.

使用 .NET 标准,您不需要共享项目.我能看到的唯一障碍是您针对多个目标.为什么不从简单的开始,然后再调整呢?

长话短说.假设您想要一个适用于 Xamarin.Forms 应用程序的 NuGet,带有 iOS &安卓.您只需要 .NET 标准、iOS 和安卓dll.就这样.稍后您可以使用 nuspec 创建一个包

I am trying to set up DependencyService to call platform specific code in a Multiplatform project.

When calling Dependency.Get<IService>() in the Shared project I get null.

The Service inside iOS does not seem to register.

I have tried to register it like this: [assembly: Dependency(typeof(Service))]- on top of the namespace declaration inside the iOS project. Which I have gotten to work inside a Xamarin.Forms project. And I have also tried to call DependencyService.Register<Service>(); inside a Init method in the iOS project - calling it from a Xamarin.Forms iOS project in AppDelegate - made for testing the library.

The Shared project and iOS project does not seem to share the same DependencyService.

How can I call platform specific code in the iOS project from the Shared project with DependencyService?

Edit

I am getting System.PlatformNotSupportedException when working in visual studio on windows - trying to use DependencyService inside a net standard library project. Could it be that visual studio for mac simply doesn't throw error and returns null instead?

解决方案

You can easily solve the problem by removing Shared project and changing

MyMutliplatformLibraryProject.Run.Test();

to

MyXamarinFormsProject.Run.Test();

If you are trying to create a Nuget with custom controls you can follow this tutorial how to do it.

With .NET standard you don't need to have a shared project. The only obstacle I can see that you are targeting multiple targets. Why don't you start simple and later adjust it?

Long story short. Let's assume you want a NuGet for Xamarin.Forms app, with iOS & Android. You simply need .NET standard, iOS & Android dll. That's all. Later you can create a package with nuspec

这篇关于多平台库项目中的 DependencyService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆