当一个类作为属性添加时,Xamarin应用程序崩溃

时间:2017-08-03 16:09:07

标签: c# sqlite xamarin xamarin.forms

我为这个名字道歉,如果我知道该怎么称呼,我会把它命名为更具体的东西。

我正在构建一个任务/ todo类型的应用程序,所以我有一个名为Objective的每个任务的类。我也使用SQLite数据库来保存数据。在App.xaml.cs方法的OnStart中,我打开了与数据库的连接并创建了一个Objective对象表。我并不担心这是崩溃的罪魁祸首,因为直到我向Objective添加了一个新字段,一切正常。但是,一旦我向Objective添加了Auxiliary类型的字段,应用程序就会在创建目标表时崩溃。辅助类用于保存有关每个目标的设置的信息,但目前它只有一个类型字符串列表。我已经确定要注释掉整个应用程序中使用的每个辅助实例,除了它的实例化。

代码的相关部分:

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new WelcomePage());
    }

    protected override void OnStart()
    {
        // Handle when your app starts
        var localDBManager = new LocalDBManager();
    }
}

LocalDBManager.cs

class LocalDBManager
{
    public static SQLiteAsyncConnection Connection;
    public static ObservableCollection<Objective> Objectives;
    public static ObservableCollection<Objective> IncompleteObjectives;
    public static ObservableCollection<Objective> CompleteObjectives;
    public static bool ObjectivesInitialized;

    public LocalDBManager()
    {
        Connection = DependencyService.Get<ISQLiteDb>().GetConnection();

        Constructor();
    }

    private async void Constructor()
    {
        await Connection.CreateTableAsync<Objective>();

        var objectives = await Connection.Table<Objective>().ToListAsync();
        Objectives = new ObservableCollection<Objective>(objectives);

        ObjectivesInitialized = true;

        GetIncompleteObjectives();
        GetCompleteObjectives();
    }

    public async void GetIncompleteObjectives()
    {
        if (!ObjectivesInitialized)
        {
            var objectives = await Connection.Table<Objective>().ToListAsync();
            Objectives = new ObservableCollection<Objective>(objectives);
            ObjectivesInitialized = true;
        }


        var incompleteObjectivesIEnumerable = Objectives.Where(o => o.Completed == false).OrderBy(o => o.Name);
        IncompleteObjectives = new ObservableCollection<Objective>(incompleteObjectivesIEnumerable);
    }

    public async void GetCompleteObjectives()
    {
        if (!ObjectivesInitialized)
        {
            var objectives = await Connection.Table<Objective>().ToListAsync();
            Objectives = new ObservableCollection<Objective>(objectives);
            ObjectivesInitialized = true;
        }

        var completeObjectivesIEnumerable = Objectives.Where(o => o.Completed).OrderBy(o => o.Name);
        CompleteObjectives = new ObservableCollection<Objective>(completeObjectivesIEnumerable);
    }
}

Objective.cs

public class Objective
{
    public static int ObjectiveID;

    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public string Name { get; set; }
    public bool Completed { get; set; }
    public string Details { get; set; }
    public bool Remind { get; set; }
    public DateTime RemindDate { get; set; }
    public TimeSpan RemindTime { get; set; }
    public bool Deadline { get; set; }
    public DateTime DeadlineDate { get; set; }
    public TimeSpan DeadlineTime { get; set; }
    public bool DeadlineEqualsRemind { get; set; }
    public string Aux { get; set; }
}

Auxiliary.cs

public class Auxiliary
{
    public List<string> Tags { get; set; }
}

可能有一个相当简单的解决方案,但它不是来找我。我对c#还是比较新的,所以任何帮助都会非常感激。

编辑:我意识到我没有包含例外

我从compliler获得的唯一信息是:“发生了未处理的异常。”通过反复测试,我发现辅助字段的实现会使应用程序崩溃。如果有任何方法可以从xamarin的异常中收集更多信息,我很乐意知道,因为我倾向于发现它们缺乏。

0 个答案:

没有答案