我安装了最新的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; } }
尝试使接口公开吗? –
这将是一个简单的解决方案,但我用public取代了内部,但仍然失败。我今天会再看一遍,所以我可能会找到一些东西 –
你不应该需要Register方法调用,属性应该足够了。你可以发布代码与DependencyService.Get和点击处理? –