Pregunta

Is there a way to access Windows.ApplicationModel.Contacts.ContactStore in ViewModel class?

following code is access able in code behind file i.e. .xaml.cs but not in ViewModel which is in shared part, below line in viewmodel.cs says Udefined ContactStore, missing using directive or assembly reference. But I can access Contacts and ContactManager from ViewModel.

ContactStore contactStore = await ContactManager.RequestStoreAsync();
¿Fue útil?

Solución 2

Got the solution! as shared project is for both windows app and windows phone. Previously I thought that ContactStore is for both but checking the msdn it appeared that it is available only for windows phone 8.1 only. So I have apply Windows phone directive to in my shared project. here is how

    #if WINDOWS_PHONE_APP

     ContactStore contactStore = await ContactManager.RequestStoreAsync();

        IReadOnlyList<Contact> contacts = null;
        // Find all contacts
            contacts = await contactStore.FindContactsAsync();

            foreach (var item in contacts)
            {
                if (!string.IsNullOrEmpty(item.FirstName) && !string.IsNullOrEmpty(item.LastName))
                {
                    var acontact = new Contact() { Name = item.FirstName + " " + item.MiddleName + " " + item.LastName, };
                    if (item.Thumbnail != null)
                    {
                        var thumnailStream = await item.Thumbnail.OpenReadAsync();
                        BitmapImage thumbImage = new BitmapImage();
                        thumbImage.SetSource(thumnailStream);
                    }
                    myContactsList.Add(acontact);
                }
            }
      #else
        //do windows phone logic here
      #endif

Otros consejos

Usually you want to to wrap that in some sort of RepositoryService. This could be IContactManagerService. Which will be passed into the viewmodel or injected via DI. The implimentation of the service would contain async methods for retrieving contacts and has a dependency on your ContactManager class.

this makes the ContractStore exchangeable because it is decoupled.

HTH

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top