Matthew Pans
Matthew Pans

Reputation: 839

Partial method 'MyContactsService.GetContactsAsync()' must have an implementation part because it has accessibility modifiers

I am getting below error when try to rebuild the project in release mode.

Severity Code Description Project File Line Suppression State Error (active) CS8795 Partial method 'MyContactsService.GetContactsAsync()' must have an implementation part because it has accessibility modifiers. ContactsDemo (net8.0-maccatalyst), ContactsDemo (net8.0-windows10.0.19041.0)

Because of the above error I can't create the APK file.

I am using a service to fetch the local phone book contacts and that class causes this error. Check the below class:

namespace ContactsDemo.Service
{
    public partial class MyContactsService
    {
        public partial List<MyContact> GetContactsAsync();
    }
}

We have added the service classes on both Android and iOS platforms.

Android Service

namespace ContactsDemo.Service
{
    public partial class MyContactsService 
    {
        const string ThumbnailPrefix = "thumb";
        List<MyContact> contactList;
        public partial List<MyContact> GetContactsAsync()
        {
            FillContacts();
            return contactList;
        }

        void FillContacts()
        {
            var uri = ContactsContract.Contacts.ContentUri;

            string[] projection = {
                ContactsContract.Contacts.InterfaceConsts.Id,
                ContactsContract.Contacts.InterfaceConsts.DisplayName,
                ContactsContract.Contacts.InterfaceConsts.PhotoId,
            };


            var ctx = Android.App.Application.Context;

            var cursor = ctx.ApplicationContext.ContentResolver.Query(uri, new string[]
{
                        ContactsContract.Contacts.InterfaceConsts.Id,
                        ContactsContract.Contacts.InterfaceConsts.DisplayName,
                        ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri
}, null, null, $"{ContactsContract.Contacts.InterfaceConsts.DisplayName} ASC");

            contactList = new List<MyContact>();

            if (cursor.MoveToFirst())
            {
                do
                {
                    var contact = CreateContact(cursor, ctx);
                    if (!string.IsNullOrWhiteSpace(contact.Name))
                    {
                        contactList.Add(contact);
                    }
                } while (cursor.MoveToNext());
            }
        }

        MyContact CreateContact(ICursor cursor, Context ctx)
        {
            var contactId = GetString(cursor, ContactsContract.Contacts.InterfaceConsts.Id);

            var numbers = GetNumbers(ctx, contactId);
            var emails = GetEmails(ctx, contactId);

            var uri = GetString(cursor, ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri);
            string path = null;
            if (!string.IsNullOrEmpty(uri))
            {
                try
                {
                    using (var stream = Android.App.Application.Context.ContentResolver.OpenInputStream(Android.Net.Uri.Parse(uri)))
                    {
                        path = Path.Combine(Path.GetTempPath(), $"{ThumbnailPrefix}-{Guid.NewGuid()}");
                        using (var fstream = new FileStream(path, FileMode.Create))
                        {
                            stream.CopyTo(fstream);
                            fstream.Close();
                        }

                        stream.Close();
                    }


                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                }

            }
            var contact = new MyContact
            {
                Name = GetString(cursor, ContactsContract.Contacts.InterfaceConsts.DisplayName),
                Emails = emails,
                Image = path,
                PhoneNumbers = numbers,
            };

            return contact;
        }

        string[] GetNumbers(Context ctx, string contactId)
        {
            var key = ContactsContract.CommonDataKinds.Phone.Number;

            var cursor = ctx.ApplicationContext.ContentResolver.Query(
                ContactsContract.CommonDataKinds.Phone.ContentUri,
                null,
                ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + " = ?",
                new[] { contactId },
                null
            );

            return ReadCursorItems(cursor, key)?.ToArray();
        }

        string[] GetEmails(Context ctx, string contactId)
        {
            var key = ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data;

            var cursor = ctx.ApplicationContext.ContentResolver.Query(
                ContactsContract.CommonDataKinds.Email.ContentUri,
                null,
                ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId + " = ?",
                new[] { contactId },
                null);

            return ReadCursorItems(cursor, key)?.ToArray();
        }

        IEnumerable<string> ReadCursorItems(ICursor cursor, string key)
        {
            while (cursor.MoveToNext())
            {
                var value = GetString(cursor, key);
                yield return value;
            }

            cursor.Close();
        }

        string GetString(ICursor cursor, string key)
        {
            return cursor.GetString(cursor.GetColumnIndex(key));
        }

    }
}

iOS Service

namespace ContactsDemo.Service
{
    public partial class MyContactsService
    {
        const string ThumbnailPrefix = "thumb";

        List<MyContact> contacts = new List<MyContact>();

        public partial List<MyContact> GetContactsAsync()
        {
            NSError error = null;
            var keysToFetch = new[] { CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses, CNContactKey.ImageDataAvailable, CNContactKey.ThumbnailImageData };

            var request = new CNContactFetchRequest(keysToFetch: keysToFetch);
            request.SortOrder = CNContactSortOrder.GivenName;

            using (var store = new CNContactStore())
            {
                var result = store.EnumerateContacts(request, out error, new CNContactStoreListContactsHandler((CNContact c, ref bool stop) =>
                {

                    string path = null;
                    if (c.ImageDataAvailable)
                    {
                        path = path = Path.Combine(Path.GetTempPath(), $"{ThumbnailPrefix}-{Guid.NewGuid()}");

                        if (!File.Exists(path))
                        {
                            var imageData = c.ThumbnailImageData;
                            imageData?.Save(path, true);


                        }
                    }

                    var contact = new MyContact()
                    {
                        Name = string.IsNullOrEmpty(c.FamilyName) ? c.GivenName : $"{c.GivenName} {c.FamilyName}",
                        Image = path,
                        PhoneNumbers = c.PhoneNumbers?.Select(p => p?.Value?.StringValue).ToArray(),
                        Emails = c.EmailAddresses?.Select(p => p?.Value?.ToString()).ToArray(),

                    };

                    if (!string.IsNullOrWhiteSpace(contact.Name))
                    {
                        contacts.Add(contact);
                    }
                }));
            }

            return contacts;
        }
    }
}

I am able to run the app in debug mode with this error. But in release mode can't rebuild the app and can't create the APK file.

I have created a demo project for reproducing this issue.

Update

I have updated my VS current and preview version to the latest but still gets the same error. My VS versions:

  1. Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.11.0 Preview 2.0

  2. Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.10.2

Upvotes: 1

Views: 129

Answers (1)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13889

After I reviewed your code, I found that you have just implemented the partial class MyContactService.cs on Android and iOS platform.

And the TargetFramework specified in the .csproj is as follows:            

<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>

<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks> 

But the compilation principle is that the partial class needs to be implemented for each TargetFramework specified in the .csproj file.

So, you can try to remove other platforms on .csproj file.

For example, if I commented out other platforms except android platform and iOS platform, the error could be resolved.       

<PropertyGroup>
            <TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>

            <!--<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>-->

            <!--<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks> -->

Upvotes: 1

Related Questions