2014-09-11 11 views
0

我想展示我的Windows 8應用程序的歷史,其中包括日期,時間,地板,區域,Longitutde和緯度。我嘗試了下面的代碼,但上面沒有輸出。使用Linq到SQL的Windows Phone

您可以在下面的鏈接中看到我想要顯示在我的應用程序上的圖像。但是當我運行我的程序時,我什麼都看不到。

我有三個類使用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

+0

你有你的觀點添加到您的問題嗎?在視圖中你創建了綁定到表? – James 2014-09-11 09:36:27

+0

不,我不知道任何關於視圖,以及如何創建綁定到桌子上。對於如此愚蠢,但我只是一個初學者。你可以幫助我或至少爲我提供鏈接,所以我可以瞭解有關視圖和綁定與我的代碼相關 – 2014-09-11 10:23:49

+0

你真的打算在手機上運行SQL Server嗎? – 2014-09-11 10:33:05

回答

1

鏈接試試這個:

在數據庫應當有一個主鍵否則會拋出一個異常和數據類型是TimeSpan不支持,所以你需要採取日期時間字符串爲完成下面:

[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(); 

      } 
     } 

我上面的東西來實現其正常工作

+0

謝謝你的幫助,但它仍然不工作,它在歷史頁面上什麼也沒有顯示。我所嘗試的是從NFC標籤獲取信息並將其存儲在本地數據庫中,然後在歷史記錄頁面中檢索它。 – 2014-09-11 10:10:13

+0

這將是數據綁定錯誤。我已經嘗試了上述數據,並且其工作良好。所以如果這個答案幫助了你,接受它以備將來參考。我認爲你應該在問題中編輯並將其添加到問題中而不是回答。我會是負面的標誌。請小心,請不要逐個增加您的查詢,讓它在一個問題中清楚問題 – 2014-09-11 10:16:21

+0

您能否建議我如何克服此綁定錯誤? – 2014-09-11 10:32:07