[英]Xamarin.Forms DependencyService Get() Method Returns Null in PCL
我安装了最新的Xamarin Alpha,无法使UWP和DependencyService工作。
在UWP项目中,我有:
Forms.Init(e);
// For release version, doc said debug will do this itself
DependencyService.Register<SpecialInterface>();
IInterface1 iinterface1 = DependencyService.Get<IInterface1>();
这确实找到了接口指针。
在PCL中,我有:
IInterface1 iinterface1 = DependencyService.Get<IInterface1>();
在PCL中包含的视图模型中(单击按钮后),它无法为UWP和Android上的Get<IInterface1>()
调用返回null
值。
这是UWP项目中包含的实现:
using PrismUnityApp2.UWP;
using SharedProject1;
using Xamarin.Forms;
[assembly: Dependency(typeof(SpecialInterface))]
namespace PrismUnityApp2.UWP
{
public class SpecialInterface : IInterface1
{
public SpecialInterface() { }
public int TestMethod(int i)
{
return i;
}
}
}
最后,接口在共享项目中定义如下:
namespace SharedProject1
{
internal interface IInterface1
{
int TestMethod(int i);
}
}
我有在Android中运行的示例“ UsingDependencyService”,WinPhone失败,因为HyperV已针对Android关闭。
我想念什么?
谢谢你的帮助。
使用以下软件包/版本:
Windows 10 UWP
public void OnNavigatedTo(NavigationParameters parameters) { if (parameters.ContainsKey("title")) Title = (string) parameters["title"] + " and Prism"; IInterface1 iinterface1 = DependencyService.Get<IInterface1>(); if (iinterface1 != null) { int value = iinterface1.TestMethod(1); Title += " ::: " + value; } }
DependencyService.Get<IInterface1>()
调用在PCL中返回null,因为它正在查找实际上多次定义的IInterface1
的不同版本。
因为IInterface1
位于PCL项目以及UWP和Android项目都引用的共享项目中,所以它基本上与在具有相同名称和签名的PCL和应用程序项目中分别创建接口相同。 但是,上级装配是不同的。
如果要将IInterface1
移到现有PCL或现有PCL和应用程序项目都引用的单独PCL中,则DependencyService
应该能够按预期定位特定于平台的实现SpecialInterface
。
如果有人在Xamarin,iOS上引用空结果,请执行以下操作: ImagePickerService
DependencyService.Get();
我错过的是
Xamarin.Forms.DependencyService.Register<Xamarin.Forms.ImagePicker.IImagePickerService, Xamarin.Forms.ImagePicker.iOS.ImagePickerService>();
在AppDelegate上,使用FinishedLaunching方法。
参考: https : //github.com/matheusneder/Xamarin.Forms.ImagePicker
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.