XamarinForms例外

时间:2018-06-03 08:39:01

标签: xaml xamarin xamarin.forms xamarin.ios xamarin.android

我创建了一个带数据库连接的搜索栏。当我运行它。它有一个例外。我使用之前创建的数据库。

模型类条目

public class entries
{
    public entries()
    {

    }

    public entries(string word)
    {
        this.word = word;
    }
    public entries(string word, string wordtype, string definition)
    {
        this.word = word;
        this.wordtype = wordtype;
        this.definition = definition;
    }   
    public string word
    { get; set; }

    public string wordtype
    { get; set; }

    public string definition
    { get; set; }

    public List<string> GetWord { get; set; }
}

类DatabaseManager:

public class DatabaseManager
{
    SQLiteConnection dbConnection;
    public DatabaseManager()
    {
        dbConnection = DependencyService.Get<ISQLite>().GetConnection();
    }



    public List<string> GetWord()
    {
        return dbConnection.Query<string>("Select word From [entries]").ToList();
    }


}

MainPage.xaml.cs:

    DatabaseManager dbManager = new DatabaseManager();
 private void MySearchBar_SearchButtonPressed(object sender, EventArgs e)
        {

        }

        private void MySearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            var keyword = MySearchBar.Text;
            if(keyword.Length >= 1) { 
                var suggestions = dbManager.GetWord().Where(c => c.ToLower().Contains(keyword.ToLower()));

                SuggestionListView.ItemsSource = suggestions;
                SuggestionListView.IsVisible = true;
            }
            else
            {
                SuggestionListView.IsVisible = false;
            }
        }

这是例外:

  

System.MissingMethodException:类型为“System.String”的构造函数不是   找到。

请帮助。非常感谢。

1 个答案:

答案 0 :(得分:0)

您可能已为“条目”表创建了一个实体,例如它是

[Table (“entries”)]
public class Entries
{
    …

    [Column (“word”)]
    public string Word;

    …
}

然后更改行

dbConnection.Query<string>("Select word From [entries]").ToList();

dbConnection.Table<Entries>().Select(x => x.Word).ToList();

这将取消创建一个额外课程的要求,如上面评论中的寿司所述。

此外,如果任何单词包含NullReferenceException,则以下行会在c.ToLower()处抛出Null

dbManager.GetWord().Where(c => c.ToLower().Contains(keyword.ToLower()));

所以要摆脱这个,选择没有空值的结果,如

dbConnection.Table<Entries>().Where(x => x.Word != null).Select(x => x.Word).ToList();