我已将未来日期,事件名称存储在db(sqlite)
中我正在从db检索日期,事件并使用listview发布它。
如何计算从数据库和当前日期检索的日期剩余天数。?
然后,我需要在列表视图中显示事件和总天数。
所以方法是:
DatabaseHelperClass.cs
public DaysLeft ReadDaysLeft(int daysid)
{
using (var dbConn = new SQLiteConnection(App.DB_Path))
{
var data = dbConn.Query<DaysLeft>("select * from DaysLeft where Id = " + daysid).FirstOrDefault();
System.Diagnostics.Debug.WriteLine("In ReadDaysLeft"+data);
return data;
}
}
public ObservableCollection<DaysLeft> ReadDaysLeft()
{
using (var dbConn = new SQLiteConnection(App.DB_Path))
{
List<DaysLeft> mycollection = dbConn.Table<DaysLeft>().ToList<DaysLeft>();
System.Diagnostics.Debug.WriteLine("In ReadDaysLeftCollection"+mycollection);
ObservableCollection<DaysLeft> dayslist = new ObservableCollection<DaysLeft>(mycollection);
System.Diagnostics.Debug.WriteLine("In ReadDaysLeftCollection" + dayslist);
return dayslist;
}
}
ReadALLData.cs
public class ReadAllData
{
DatabaseHelperClass dbHelper = new DatabaseHelperClass();
public ObservableCollection<DaysLeft> GetAllDays()
{
System.Diagnostics.Debug.WriteLine("In GetAllDays");
return dbHelper.ReadDaysLeft();
}
}
BasicPage.cs
private void AddDataToListView(object sender, RoutedEventArgs e)
{
ReadAllData alldata = new ReadAllData();
DB_DaysLeft = alldata.GetAllDays();
DaysLeftListView.ItemsSource = DB_DaysLeft.OrderByDescending(i => i.Id).ToList();
}
我根据您的建议编辑了..添加到两个对象,如
List<int> daysleft { get; set; }
string events;
private void AddDataToListView(object sender, RoutedEventArgs e)
{
daysleft = new List<int>();
ReadAllData alldata = new ReadAllData();
DB_DaysLeft = alldata.GetAllDays();
foreach (var Date in DB_DaysLeft)
{
DateTime dt = Convert.ToDateTime(Date);
TimeSpan diff = DateTime.Now - Date.Date;
int days = (int)Math.Abs(Math.Round(diff.TotalDays));
//TimeSpan durations= DateTime.Now.Subtract(dt).TotalDays();
daysleft.Add(days);
}
DaysLeftListView.ItemsSource = DB_DaysLeft.OrderByDescending(i => i.Id).ToList();
}
在BasicPage1.cs
中 private void DaysLeftListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int Select_Changed = 0;
if (DaysLeftListView.SelectedIndex !=-1)
{
tempclass selectedDay = DaysLeftListView.SelectedItem as tempclass;
Frame.Navigate(typeof(EditWindow), Select_Changed = selectedDay.id);
}
}
EditOrUpdateWindow.cs
//from previous page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Selected_DaysId = Int32.Parse(e.Parameter.ToString());
daysleft = dbHelp.ReadDaysLeftForEdit(Selected_DaysId);
var temp_date = daysleft.diff;
event_date.Date = DateTimeOffset.Parse(daysleft.diff.ToString());
event_text.Text = daysleft.myEvent;
// this.navigationHelper.OnNavigatedTo(e);
}
//To Save into db.
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
dayleftOfDaysLeft.Date = DateTime.Parse(event_date.Date.ToString());
dayleftOfDaysLeft.Events = event_text.Text;
dbHelp.UpdateDays(dayleftOfDaysLeft);
Frame.Navigate(typeof(BasicPage1));
}
请帮帮我。谢谢。
答案 0 :(得分:1)
它是这样的:
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.Now.AddDays(30);
int diff = date2.Subtract(date1).TotalDays();
将它绑定到Windows Phone 8.1你需要这样做:
在.cs文件中:
public class AllData
{
public int diff { get; set; }
public string myEvent {get;set;}
}
public class RootObject
{
public List<AllData> dataFromDb { get; set; }
//all your other code
}
在xaml中:
<ListBox ItemsSource="{Binding dataFromDb }">
<ListBox.ItemTemplate>
<DataTemplate >
<TextBlock Text="{Binding diff}" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding myEvent}" TextWrapping="Wrap" FontSize="18" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果你还没有完成它,你需要在下载数据之后为页面设置DataContext,就像这样(假设你在页面的代码隐藏文件中进行下载,例如MainPage.xaml.cs中):
RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);
this.DataContext = json;
特定于您的代码,您可以这样做:
public class AllData
{
public int diff { get; set; }
public string myEvent {get;set;}
}
public class YourClass{
List<AllData> daysLeft {get; set;}
private void AddDataToListView(object sender, RoutedEventArgs e)
{
daysleft = new List<AllData>();
ReadAllData alldata = new ReadAllData();
DB_DaysLeft = alldata.GetAllDays();
foreach (var Date in DB_DaysLeft)
{
AllData ad = new AllData();
DateTime dt = Convert.ToDateTime(Date);
TimeSpan diff = DateTime.Now - Date.Date;
int days = (int)Math.Abs(Math.Round(diff.TotalDays));
ad.diff = days;
ad.myEvent = events;
daysleft.Add(ad);
}
DaysLeftListView.ItemsSource = DB_DaysLeft.OrderByDescending(i => i.Id).ToList();
}
}