Issue
I have the latest Xamarin Alpha installed and can't get UWP and DependencyService working.
In the UWP project I have:
Forms.Init(e);
// For release version, doc said debug will do this itself
DependencyService.Register<SpecialInterface>();
IInterface1 iinterface1 = DependencyService.Get<IInterface1>();
This does find the interface pointer.
In a PCL I have:
IInterface1 iinterface1 = DependencyService.Get<IInterface1>();
In a view model contained in the PCL (after a button click) it fails returning a null
value for the Get<IInterface1>()
call on both UWP and Android.
Here is the implementation contained in the UWP project:
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;
}
}
}
Finally, the Interface is defined in a Shared Project as follows:
namespace SharedProject1
{
internal interface IInterface1
{
int TestMethod(int i);
}
}
I have the sample "UsingDependencyService" working in Android, WinPhone fails because HyperV is off for Android.
What am I missing?
Thanks for any help.
Using following packages/versions:
- Prism.Unity.Forms 6.2.0.pre4
- Prism.Forms 6.1.0.pre4
- Xamarin.Forms 2.2.0.45
- Unity 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; } }
Solution
The DependencyService.Get<IInterface1>()
call is returning null in the PCL because it's looking for a different version of IInterface1
which is actually being defined multiple times.
Because IInterface1
is located in a Shared Project that's referenced by both the PCL project and the UWP & Android projects, it's basically the same as creating the interface separately in both the PCL and app projects with the same name and signature. The parent assembly is different however.
If you were to move IInterface1
into your existing PCL or a separate PCL that is referenced by both the existing PCL and your app projects, DependencyService
should be able to locate your platform specific implementation, SpecialInterface
, as expected.
Answered By - Dan Rigby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.