【问题标题】:Accessing ContactStore in ViewModel in mvvm light windows phone 8.1 universal app在 mvvm light windows phone 8.1 通用应用程序中访问 ViewModel 中的 ContactStore
【发布时间】:2014-06-16 16:38:10
【问题描述】:

有没有办法在 ViewModel 类中访问 Windows.ApplicationModel.Contacts.ContactStore?

以下代码可以在文件后面的代码中访问,即 .xaml.cs,但不能在共享部分中的 ViewModel 中访问,viewmodel.cs 的下一行显示 Udefined ContactStore,缺少使用指令或程序集引用。但我可以从 ViewModel 访问 Contacts 和 ContactManager。

ContactStore contactStore = await ContactManager.RequestStoreAsync();

【问题讨论】:

    标签: windows-phone-8 mvvm windows-phone mvvm-light windows-phone-8.1


    【解决方案1】:

    通常你想用某种RepositoryService 包装它。这可能是IContactManagerService。它将被传递到viewmodel 或通过 DI 注入。 该服务的实现将包含用于检索联系人的异步方法,并且依赖于您的 ContactManager 类。

    这使得ContractStore 可以交换,因为它是解耦的。

    HTH

    【讨论】:

      【解决方案2】:

      找到解决方案!因为共享项目适用于 Windows 应用程序和 Windows Phone。以前我认为ContactStore 两者都适用,但检查msdn 似乎它仅适用于windows phone 8.1。所以我在我的共享项目中应用了 Windows phone 指令。这是怎么回事

          #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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-12
        • 1970-01-01
        相关资源
        最近更新 更多