0

我正在尝试显示我的 Windows 8 应用程序的历史记录,其中包括日期、时间、楼层、区域、经度和纬度。我尝试了下面的代码,但上面没有输出。

您可以在下面的链接中看到我想在我的应用程序上显示的图像。但是当我运行我的程序时,我什么也看不到。

我有三个类用于使用 linq to sql 检索数据库并通过它显示信息。主类是 History.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;   
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Text;
using System.Data.Linq;


namespace SmartParking
{

public partial class History : PhoneApplicationPage
{
    private readonly HistoryDataContext historylog; 
    public History()
    {
        InitializeComponent();
       // createDB();





    }

    public HistoryDataContext Log
    {
        get { return historylog; }
    }

    public void createDB()
    {
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            if (historylog.DatabaseExists() == false)
            {
                historylog.CreateDatabase();
                addDataDB();
            }
        }

    }

    public void addDataDB()
    {
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            HistoryDB hdb = new HistoryDB
            {
               // Date = DateTime.Today,
               // Time = DateTime.Now.TimeOfDay,
                Zone = Checkin.Zone_st,
                Floor = Checkin.Floor_st,
                location_latitude = Checkin.Latitud_do,
                location_longtitud = Checkin.Longtitude_do
            };
            historylog.history.InsertOnSubmit(hdb);
            historylog.SubmitChanges();
            GetHistoryLog();

        }
    }
    public IList<HistoryDB> GetHistoryLog()
    {
        IList<HistoryDB> HistoryList = null;
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            IQueryable<HistoryDB> query = from histoy in historylog.history select histoy;
            HistoryList = query.ToList();
        }
        return HistoryList ;
    }


}
}

下一个类是带有表和列的 HistoryDB.cs

[Table]
    public class HistoryDB 
{

    [Column(CanBeNull = false)]
    public DateTime Date
    { get; set; }

    [Column(CanBeNull = false)]
    public TimeSpan Time
    { get; set; }

    [Column(CanBeNull = false)]
    public String Zone
    { get; set; }

    [Column(CanBeNull = false)]
    public String Floor
    { get; set; }

    [Column(CanBeNull = false)]
    public double location_latitude
    { get; set; }

    [Column(CanBeNull = false)]
    public double location_longtitud
    { get; set; }

}

下一个类是 HistoryDataContext.cs。

public class HistoryDataContext:DataContext 
{
    public static string DBConnectionString = "Data Source=isostore:/History.sdf";
    public HistoryDataContext(string DBConnectionString)
        : base(DBConnectionString)
    {
}

   public Table<HistoryDB> history
    {
        get
        {
            return this.GetTable<HistoryDB>();
        }
    }


}

我正在尝试从 NFC 标签获取信息并将其存储在本地数据库中,然后在历史页面中检索它。下面的代码是从 nfc 标签中读取的,但我不知道如何再次从那里保存在本地数据库中并在历史页面中检索它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Networking.Proximity;
using NdefLibrary.Ndef;
using NdefLibraryWp.Ndef;
using Windows.Networking.Sockets; // needed for DataReader, DataWriter
using Windows.UI.Popups;
using Microsoft.Phone.UserData;
using System.Text;
using Windows.Phone.PersonalInformation;
using SmartParking.Resources;
using System.Diagnostics;


namespace SmartParking
{

public partial class Checkin : PhoneApplicationPage
{
    private ProximityDevice _device;
    private long _subscriptionIdNdef;

    public static double Latitud_do { get; set; }
    public static double Longtitude_do { get; set; }
    public static string Floor_st { get; set; }
    public static string Zone_st { get; set; }

    History store = new History();


    public Checkin()
    {
        InitializeProximityDevice();
        InitializeComponent();


    }

    private void SetLogStatus(string newStatus)
    {
        Dispatcher.BeginInvoke(() => { if (LogStatus != null) LogStatus.Text = newStatus; });
    }

    private void SetFloorStatus(string newStatus)
    {
        Dispatcher.BeginInvoke(() => { if (FloorStatus != null) FloorStatus.Text = newStatus; });
    }



    private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
    {
        MessageBox.Show(" ");
    }


    private void InitializeProximityDevice()
    {
        _device = Windows.Networking.Proximity.ProximityDevice.GetDefault();
        if (_device != null)
        {
            _subscriptionIdNdef = _device.SubscribeForMessage("NDEF", MessageReceivedHandler);

        }

    }


    private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
    {
        var rawMsg = message.Data.ToArray();
        var ndefMessage = NdefMessage.FromByteArray(rawMsg);


        ////// Loop over all records contained in the NDEF message
        foreach (NdefRecord record in ndefMessage)
        {

            if (NdefTextRecord.IsRecordType(record))
            {
                // Convert and extract URI info
                var textRecord = new NdefTextRecord(record);
                //var str = textRecord.Text;
                string[] str = textRecord.Text.Split('|');

                var latitude = str[2];
                Latitud_do = double.Parse(latitude);
                var longtitude = str[3];
                Longtitude_do = double.Parse(longtitude);
                var Floor_st = str[0];
                var Zone_st = str[1];


                SetLogStatus("Floor: " + Floor_st + " Zone: " + Zone_st );
                SetFloorStatus("Longitude: " + latitude + "   Longitude: " + longtitude);
                store.addDataDB();

            }
        }
      }
 }

}

表格的图片在下面的链接中

https://www.dropbox.com/s/g87yta6hegjstge/Untitled.png?dl=0

4

1 回答 1

1

尝试这个:

在数据库中应该有一个主键,否则它会引发异常并且不支持TimeSpan的数据类型,因此您需要将DateTimeString设置为如下所示:

[Table]
    public class HistoryDB
    {
        [Column(IsPrimaryKey = true)]
        public int Id { get; set; }

        [Column(CanBeNull = false)]
        public DateTime Date
        { get; set; }

        [Column(CanBeNull = false)]
        public DateTime Time
        { get; set; }

        [Column(CanBeNull = false)]
        public String Zone
        { get; set; }

        [Column(CanBeNull = false)]
        public String Floor
        { get; set; }

        [Column(CanBeNull = false)]
        public double location_latitude
        { get; set; }

        [Column(CanBeNull = false)]
        public double location_longtitud
        { get; set; }

    }

        public void addDataDB()
        {
            using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
            {
                HistoryDB hdb = new HistoryDB
                {
                    Id = 0,
                     Date = DateTime.Today,
                    Time = DateTime.Now,
                    Zone = "Zone",
                    Floor = "Floore",
                    location_latitude = 00.00,
                    location_longtitud = 00.00
                };
                historylog.history.InsertOnSubmit(hdb);
                historylog.SubmitChanges();
                GetHistoryLog();

            }
        }

我已经实施了上述事情及其正常工作

于 2014-09-11T09:35:29.343 回答