首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >8应用程序开发

8应用程序开发
EN

Stack Overflow用户
提问于 2014-10-30 05:49:36
回答 1查看 214关注 0票数 0

我正在使用sqlite数据库开发一个windows应用程序,我已经将所有的值存储在SmartParking.DLL!SmartParking.App.RootFrame_NavigationFailed(object中,但是当我运行该应用程序时,它的结果是debugger.break上的错误,如"> debugger.break发件人,System.Windows.Navigation.NavigationFailedEventArgs e)第133行C#“。

下面是我处理的所有3个类,用于将值存储在数据库中并输出到history.xaml中

代码语言:javascript
复制
 <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Smart Parking" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="History" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="ListData">
            <ListBox.ItemTemplate>
                <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                            <TextBlock x:Name=  "DateTxt"  Text="{Binding Date}"   TextWrapping="Wrap" />
                            <TextBlock x:Name=  "TimeTxt"  Text="{Binding Time}"  TextWrapping="Wrap" />
                            <TextBlock x:Name=  "ZoneTxt"  Text="{Binding Zone}"  TextWrapping="Wrap"/>
                            <TextBlock x:Name=  "FloorTxt" Text="{Binding Floor}" TextWrapping="Wrap"/>
                            <TextBlock x:Name=  "LatTxt"   Text="{Binding location_latitude}" TextWrapping="Wrap"  />
                            <TextBlock x:Name=  "LongTxt"  Text="{Binding location_longitude}" TextWrapping="Wrap" />
                        </StackPanel>
                 </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox> 
    </Grid>
</Grid>

下面是执行所有主要功能的dbhelper

代码语言:javascript
复制
public class DbHelper
{

    SQLiteConnection dbConn;

    public async Task<bool> onCreate(string DB_PATH)
    {
        try
        {
            if (!CheckFileExists(DB_PATH).Result)
            {
                using (dbConn = new SQLiteConnection(DB_PATH))
                {
                    dbConn.CreateTable<historyTableSQlite>();
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }


    private async Task<bool> CheckFileExists(string fileName)
    {
        try
        {
            var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
            return true;
        }
        catch
        {
            return false;
        }
    }

    //retrieve all list from the database
    public ObservableCollection<historyTableSQlite> ReadHistory()
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            List<historyTableSQlite> myCollection = dbConn.Table<historyTableSQlite>().ToList<historyTableSQlite>();
            ObservableCollection<historyTableSQlite> HistoryList = new ObservableCollection<historyTableSQlite>(myCollection);
            return HistoryList;
        }
    }

    // Insert the new info in the histrorytablesqlite table. 
    public void Insert(historyTableSQlite newcontact)
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            dbConn.RunInTransaction(() =>
            {
                dbConn.Insert(newcontact);
            });
        }
    }



}    

以下是history.xaml.cs代码文件

代码语言:javascript
复制
 public partial class History : PhoneApplicationPage
{





    // string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
    ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>();

    public History()
    {

        InitializeComponent();
        AddInfo();
        ReadHistoryList_Loaded();
    }



    public void ReadHistoryList_Loaded()
    {
        ReadAllContactsList dbhistory = new ReadAllContactsList();
        DB_HistoryList = dbhistory.GetAllHistory();//Get all DB contacts
        ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList();//Latest contact ID can Display first

    }


    public void AddInfo()
    {
    //   ListData.Items.Add(new historyTableSQlite{
    //   Date = DateTime.Now.ToShortDateString(),
    //   Time = DateTime.Now.ToShortTimeString(),
    //   Zone = "PST",
    //   Floor = "10th Floor",
    //  latitude = 35.45112,
    //  longtitude = -115.42622
    //});

        DbHelper Db_helper = new DbHelper();
        Db_helper.Insert((new historyTableSQlite {
            Date = DateTime.Now.ToShortDateString(),
             Time = DateTime.Now.ToShortTimeString(),
             Zone = "PST",
              Floor = "10th Floor",
             latitude = 35.45112,
            longtitude = -115.42622
        }));

    }

}最后一个类是我将所有的值都弱化的类

代码语言:javascript
复制
ublic class historyTableSQlite : INotifyPropertyChanged
{
    [SQLite.PrimaryKey, SQLite.AutoIncrement]

    public int Id { get; set; }
    private int idvalue;

    private string dateValue = string.Empty;

    public string Date {
        get { return this.dateValue; }
        set
        {
            if (value != this.dateValue)
            {
                this.dateValue = value;
                NotifyPropertyChanged("Date");
            }
        }
    }


    private string timeValue = string.Empty;
    public string Time
    {
        get { return this.timeValue; }
        set
        {
            if (value != this.timeValue)
            {
                this.timeValue = value;
                NotifyPropertyChanged("Time");
            }
        }
    }

    private string floorValue = string.Empty;
    public string Floor
    {
        get { return this.floorValue; }
        set
        {
            if (value != this.floorValue)
            {
                this.floorValue = value;
                NotifyPropertyChanged("Floor");
            }
        }
    }

    public string zoneValue;
    public string Zone
    {
        get { return this.zoneValue; }
        set
        {
            if (value != this.zoneValue)
            {
                this.zoneValue = value;
                NotifyPropertyChanged("Zone");
            }
        }
    }

    private double latValue;
    public double latitude
    {
        get { return latValue; }
        set
        {
            if (value != this.latValue)
            {
                this.latValue = value;
                NotifyPropertyChanged("Latitude");
            }
        }
    }

    private double lonValue;
    public double longtitude
    {
        get { return this.lonValue; }
        set
        {
            if (value != this.lonValue)
            {
                this.lonValue = value;
                NotifyPropertyChanged("Longitude");
            }
        }
    }

   // public string isMarkPoint { get; set; }

    public historyTableSQlite()
    {

    }

    public historyTableSQlite(string date,string time,string floor,string zone,double lat,double lng)
    {
        Date = date;
        Time = time;
        Floor = floor;
        Zone = zone;
        latitude = lat;
        longtitude = lng;
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-30 08:16:04

从历史构造函数中删除AddInfo()和ReadHistoryList_Loaded(),并像下面这样覆盖OnNavigatedTo()。

代码语言:javascript
复制
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
                AddInfo();
                ReadHistoryList_Loaded();
    }

如果页面构造函数花费了几百毫秒以上的时间来创建页面窗口,那么它就会假设某些地方出错了。因此,最好不要在构造函数中添加任何内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26645947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档