Wednesday, October 5, 2022
HomeiOS Developmentc# - DependencyService.Get() always returns null or says I don't have a...

c# – DependencyService.Get() always returns null or says I don’t have a default constructor defined depending on params

[ad_1]

I have read everything I can find and tried all of them,but cannot get past this null reference exception. Here is my code that is failing

 try
        {
            Analytics.TrackEvent("In iCloud.aspx.cs, BackUpClicked Date = " + sDate.Date + " autoId = " + Convert.ToInt32(Application.Current.Properties["autoId"]));
            var count = await DependencyService.Get<IMessage>().BackupToiCloud(Convert.ToInt32(Application.Current.Properties["autoId"]), sDate.Date.ToString("MMddyyyy"));
            EntMessage = count.ToString() + " Records Backed Up";
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex);
            Analytics.TrackEvent("Exception in BackUpClicked Method" + ex.ToString());
        }

I have read that I need to implement Dependency service for both platforms I have in my solution so I have the following line below in my AppDelegete.cs for iOS and for Android, I have the same line in my MainActivity.cs

Xamarin.Forms.DependencyService.Register<MessageService>();

In my iOS project, I have the MessageService defined as follows,

[assembly: Dependency(typeof(MileageManagerForms.iOS.MessageService))]
namespace MileageManagerForms.iOS
{
    public class MessageService : IMessage

and for the android project I have MessageService defined as follows,

[assembly: Dependency(typeof(MileageManagerForms.Droid.MessageService))]
namespace MileageManagerForms.Droid
{
    public class MessageService : IMessage

I am not quite clear what else I need to do. One thing I found was promoting this syntax to register, but when I did that, it told me I was missing the default constructor from MessageService which is bogus.

 Xamarin.Forms.DependencyService.Register<IMessage, MessageService>();

In looking at examples of code, it appeared that the registration of the dependency service had to happen under this line of code in the appDelegate and MainActivity,

LoadApplication(new App());

is this correct? I know there are a thousand posts specifically on this subject, but it you can see something I am doing wrong, I would greatly appreciate it. Thanks so much.

[ad_2]

Source link

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments