[英]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.