简体   繁体   English

联系人SearchAsync需要很长时间

[英]Contacts SearchAsync takes a long time

Here is my problem: I am creating a WP7 application and need to list all contacts on the mobile device. 这是我的问题:我正在创建WP7应用程序,并且需要列出移动设备上的所有联系人。 I know there is the Contacts class with method SearchAsync and SearchCompleted event handler. 我知道有使用方法SearchAsyncSearchCompleted事件处理程序的Contacts类。

This is all working except for one detail; 除了一个细节,所有这些工作都是可行的。 when I am using the application on my phone, the search takes more than 12 seconds! 当我在手机上使用该应用程序时,搜索需要12秒钟以上! I am using data virtualization to make the UI draw quickly. 我正在使用数据虚拟化来快速绘制UI。 I have about 400 contacts in my phone. 我的手机中大约有400位联系人。 So the problem is, that SearchCompleted is fired after a long time :( 因此,问题在于,经过很长时间才触发SearchCompleted :(

Do you have any ideas how to improve this solution? 您对如何改进此解决方案有任何想法吗? Should I start inserting contacts in listbox by first letter ("a", "b"... but then that means I need to call SearchAsync repeatedly) and then how can I merge it? 我应该从第一个字母(“ a”,“ b” ...)开始在列表框中插入联系人,但是那意味着我需要反复调用SearchAsync,然后如何合并它?

Device: Samsung Omnia 7 设备:三星Omnia 7

ThreadPool.QueueUserWorkItem(result =>
{
     _cachingRunning = true;
     var contacts = new Contacts();
     contacts.SearchCompleted += contacts_SearchCompleted;
     contacts.SearchAsync(string.Empty, FilterKind.None, null);
});

This method is called almost 12 second after SearchAsync : SearchAsync之后将近12秒调用此方法:

private void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
            _phoneContacts = e.Results;
            Count = e.Results.Count();      

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    Cached = true;
                    _cachingRunning = false;
                    CachingChanged();
            });
 }

I store _phoneContacts and then use it for filtering; 我存储_phoneContacts ,然后将其用于过滤; accessing it by Index and Count during data virtualization on ListBox. 在ListBox上进行数据虚拟化时,通过索引和计数来访问它。

This method "works" with VirtualizingDataCollection (Telerik) and creates ViewModel item which is added to VirtualizingDataCollection . 此方法可与VirtualizingDataCollection (Telerik)“配合使用”并创建ViewModel项,该项将添加到VirtualizingDataCollection

public ObservableCollection<ExtendedContactModel> GetContactsRange(int startIndex, int count)
        {
            var collection = new ObservableCollection<ExtendedContactModel>();

            for (var i = startIndex; i < startIndex + count; i++)
            {
                var vo = ConvertToVO(_phoneContacts.ElementAt(i));
                var newContact = ConvertToExtendedContactModel(_phoneContacts.ElementAt(i), vo);

                collection.Add(newContact);
            }
            return collection;
        }

If this is a Mango device you have a couple of options: 如果这是芒果设备,则有两种选择:

1)Use a background task to push contact information into an application-specific data store. 1)使用后台任务将联系人信息推送到特定于应用程序的数据存储中。 The standard background task runs every 30 minutes and is allowed to take about 30 seconds to execute. 标准后台任务每30分钟运行一次,大约需要30秒才能执行。 More info on the background agent can be found here: Background Agents 可以在这里找到有关后台代理的更多信息: 后台代理

2)If the background agent is too scary you can do all this in-process. 2)如果后台代理太吓人,则可以在过程中完成所有这些操作。 When the user opens the app up a background thread can gather the contacts list and save them to an internal store. 当用户打开应用程序时,后台线程可以收集联系人列表并将其保存到内部存储中。

While you'd need to spend time managing the internal store of contacts, it allows you to control the contact list and will definitely improve the user experience as they'll think the contact search is very fast. 尽管您需要花费时间来管理联系人的内部存储,但它使您可以控制联系人列表,并且肯定会改善用户体验,因为他们认为联系人搜索非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM