Windows phone app t9预测文本

时间:2015-12-16 08:19:32

标签: c# windows-phone

我正在使用Windows手机应用程序,这是一个拨号器,我对预测文本几乎没有问题。预测文本工作正常,但它是滞后和缓慢。我的代码是:

我在文本框的文本更改事件中添加了联系人搜索功能:

private void dialer_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {
            MainPage.DialerText = dialer.Text;
            contactSearch(MainPage.DialerText);


        }
        catch (Exception f)
        { 
            MessageBox.Show(f.Message); 
        }
    }

ContactSearch功能代码:

public void contactSearch(string str)
    {

        try
        {
            var digitMap = new Dictionary<int, string>() {
    { 1, "" },
    { 2, "[abcABC]" },
    { 3, "[defDEF]" },
    { 4, "[ghiGHI]" },
    { 5, "[jklJKL]" },
    { 6, "[mnoMNO]" },
    { 7, "[pqrsPQRS]" },
    { 8, "[tuvTUV]" },
    { 9, "[wxyzWXYZ]" },
    { 0, "" },
};

            var enteredDigits = str;
            var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
            var regexBuilder = new StringBuilder();
            foreach (var val in charsAsInts)
                regexBuilder.Append(digitMap[val]);

            var pattern = regexBuilder.ToString();
            //append a ".*" to the end of the regex to make it "StartsWith", beginning for "EndsWith", or both for "Contains";
            pattern = ".*" + pattern + ".*";

           SearchListbox.ItemsSource = listobj.FindAll(x => x.PhoneNumbers.Contains(str) | Regex.IsMatch(x.FirstName, pattern));

        }
        catch (Exception e)
        {
           // MessageBox.Show(e.Message);
        }
    }

此代码工作正常但滞后且缓慢。我需要让它更快。请提出一些改进建议。谢谢。

2 个答案:

答案 0 :(得分:1)

这些解决方案可以改善它:

1-不要将Try catch用于重复功能: 这可以轻松地降低性能,而不是尝试使用if else

2 - 使用异步任务: 它非常适合不阻止UI并避免延迟和崩溃,但为您的任务使用较短的延迟时间,例如50ms;

3-使用其他集合类型: 正如我在您的代码中看到的那样,您的字典键是从1开始定期启动的。 那么为什么不使用更多项目更快的数组或列表。 像这样:     string [] contacts = {“contact 1”,“contact 2”,“contact 3”,“contact 4”};

甚至是一个清单:

var SearchedItem=$(".expand").text();      

$.ajax({
    type: "POST",
    async: false,
    dataType: 'json',
    url: "/MyController/Index",
    contentType: 'application/json',
    // processData: false,
    data: JSON.stringify({ SearchedItem: SearchedItem}),
    success: function (data) {
        ...
    }
});

请注意,更改集合类型可能会仅在大量数据中提高性能。

答案 1 :(得分:0)

        public interface ICellT9
        {
            void Add(string a_name);

            List<string> GetNames(string a_number);
        }

        public class Cell : ICellT9
        {
            private Dictionary<int, Cell> m_nodeHolder;
            private List<string> m_nameList;

            public Cell()
            {
                m_nodeHolder = new Dictionary<int, Cell>();

                for (int i = 2; i < 10; i++)
                {
                    m_nodeHolder.Add(i, null);
                }
            }

            public void Add(string a_name)
            {
                Add(a_name, a_name);
            }

            private void Add(string a_name, string a_originalName)
            {
                if ((string.IsNullOrEmpty(a_name) == true) && (string.IsNullOrEmpty(a_originalName) == false))
                {
                    if (m_nameList == null)
                    {
                        m_nameList = new List<string>();
                    }

                    m_nameList.Add(a_originalName);
                }
                else
                {
                    int l_firstNumber = CharToNumber(a_name[0].ToString());

                    if (m_nodeHolder[l_firstNumber] == null)
                    {
                        m_nodeHolder[l_firstNumber] = new Cell();
                    }

                    m_nodeHolder[l_firstNumber].Add(a_name.Remove(0, 1), a_originalName);
                }
            }

            public List<string> GetNames(string a_number)
            {
                List<string> l_result = null;

                if (string.IsNullOrEmpty(a_number))
                {
                    return l_result;
                }

                int l_firstNumber = a_number[0] - '0';

                if (a_number.Length == 1)
                {
                    l_result = m_nodeHolder[l_firstNumber].m_nameList;
                }
                else if(m_nodeHolder[l_firstNumber] != null)
                {
                    l_result = m_nodeHolder[l_firstNumber].GetNames(a_number.Remove(0, 1));
                }

                return l_result;

            }

            private int CharToNumber(string c)
            {
                int l_result = 0;

                if (c == "a" || c == "b" || c == "c")
                {
                    l_result = 2;
                }
                else if (c == "d" || c == "e" || c == "f")
                {
                    l_result = 3;
                }
                else if (c == "g" || c == "h" || c == "i")
                {
                    l_result = 4;
                }
                else if (c == "j" || c == "k" || c == "l")
                {
                    l_result = 5;
                }
                else if (c == "m" || c == "n" || c == "o")
                {
                    l_result = 6;
                }
                else if (c == "p" || c == "q" || c == "r" || c == "s")
                {
                    l_result = 7;
                }
                else if (c == "t" || c == "u" || c == "v")
                {
                    l_result = 8;
                }
                else if (c == "w" || c == "x" || c == "y" || c == "z")
                {
                    l_result = 9;
                }

                return l_result;
            }
        }