Xamarin.Forms DependencyService Get()方法在PCL中返回NULL
我安装了最新的Xamarin Alpha,无法获得UWP和DependencyService的工作。Xamarin.Forms DependencyService Get()方法在PCL中返回NULL
在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视图模型(后点击按钮)失败返回为Get<IInterface1>()
呼吁两国UWP和Android一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);
}
}
我有样“UsingDependencyService”的工作Android,WinPhone因HyperV关闭而失败。
我错过了什么?
感谢您的任何帮助。
使用下面的包/版本:
- Prism.Unity.Forms 6.2.0.pre4
- Prism.Forms 6.1.0.pre4
- Xamarin.Forms 2.2.0.45
- 团结4.0.1
-
的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。得到();
什么,我错过了包括是,
Xamarin.Forms.DependencyService.Register<Xamarin.Forms.ImagePicker.IImagePickerService, Xamarin.Forms.ImagePicker.iOS.ImagePickerService>();
在AppDelegate中,FinishedLaunching方法。
参考:https://github.com/matheusneder/Xamarin.Forms.ImagePicker
尝试使接口公开吗? –
这将是一个简单的解决方案,但我用public取代了内部,但仍然失败。我今天会再看一遍,所以我可能会找到一些东西 –
你不应该需要Register方法调用,属性应该足够了。你可以发布代码与DependencyService.Get和点击处理? –