Xamarin显示项目模板获取休息模式

时间:2018-06-10 05:16:52

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

我正在使用contentPage上的按钮创建应用,当我点击该按钮时,它将移动到新的contentPage。但它遇到了一些问题。 我使用sqlite数据库 连接很好。

这是模型类

public class entries
{
    public entries()
    {

    }

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

    public string type
    { get; set; }

    public string sdex { get; set; }
    public int wlen { get; set; }

    public string defn
    { get; set; }


}

MainPage.xaml.cs中的按钮

public partial class MainPage : ContentPage
{
    public string word;
    public MainPage()
    {
        InitializeComponent();
    }



   //this is the button
    async void AllWordButton_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new AllWordPage());


    }

}

这是AllWordPage.xaml.cs

public partial class OrtherAppPage : ContentPage
{
    private SQLiteConnection conn;
    public entries entry;
    public OrtherAppPage()
    {
        InitializeComponent();
        conn = DependencyService.Get<ISQLite>().GetConnection();
        var data = (from word in conn.Table<entries>() select word);
        DataList.ItemsSource = data;
    }

}

这是AllWordPage.xaml

<ContentPage.Content>
    <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
        <Label Text="This is Other App Page" />
    </StackLayout>
    <StackLayout>
        <ListView x:Name="DataList">
            <ListView.ItemTemplate>
                <TextCell Text="{Binding Word}"></TextCell>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

当我点击按钮时。得到这个通知: 您的应用已进入中断状态,但没有要显示的代码,因为所有线程都在执行外部代码(通常是系统或框架代码)。

我怎样才能显示这个问题。请帮忙。非常感谢你

1 个答案:

答案 0 :(得分:1)

共享代码中存在一些问题:

  1. ContentPage只能包含一个孩子。 AllWordPage.xaml包含2 StackLayouts。这将在运行时抛出异常。
  2. 根据C# naming conventions entries,应将其命名为Entries
  3. 使用MVVM
  4. Entries应使用Table("Entries")属性进行修饰。
  5. 每个表应包含Id列,因此可以识别单个行。还有一些其他列可能会有所帮助,例如CreatedAtDeletedAtRowVersion等。
  6. SQLiteConnection应该被处置。
  7. 使用async方法不阻止UI线程 - (from word in conn.Table<entries>() select word) - &gt; conn.Table<entries>().ToListAsync()
  8. 考虑使用EntityFramework
  9. P.S。:Official documentation包含良好的代码示例和解释。从那里开始。