我是这里的新手和编程人员。我正在制作一个只能一次显示一周的节目。我有几个星期的约会,我的问题是:有没有办法只显示你当前所在周的“约会”?
答案 0 :(得分:0)
到目前为止我有这个:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var googleCal = new GoogleClass();
foreach (var googleEvent in googleCal.GoogleEvents)
{
if ((googleEvent.StartDate.Value.DayOfWeek - googleEvent.StartDate.Value.DayOfWeek)
{
listBox.Items.Add(googleEvent.Title + " " + googleEvent.StartDate);
}
}
}
}
public class GoogleEvents
{
public string Title { get; set; }
public DateTime? StartDate { get; set; }
}
class GoogleClass
{
public List<GoogleEvents> GoogleEvents = new List<GoogleEvents>();
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API .NET Quickstart";
public GoogleClass()
{
UserCredential credential;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
GoogleEvents.Add(new GoogleEvents {Title = eventItem.Summary, StartDate = eventItem.Start.DateTime});
}
}
}
}
答案 1 :(得分:0)
我已经弄明白了
var googleCal = new GoogleClass();
var ugedag = DateTime.Now.DayOfWeek;
var getMondays = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(6));
var getTuesdays = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(5));
var getWednesday = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(4));
var getThursday = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(3));
var getFriday = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(2));
var getSaturday = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(1));
var getSunday = googleCal.GoogleEvents.Where(e => e.StartDate < DateTime.Now.AddDays(0));
foreach (var googleCalGoogleEvent in googleCal.GoogleEvents)
{
listBox.Items.Add(googleCalGoogleEvent.Title);
}
if (ugedag == DayOfWeek.Monday)
{
foreach (var events in getMondays)
{
NewMethod(events);
}
}
if (ugedag == DayOfWeek.Tuesday)
{
foreach (var events in getTuesdays)
{
NewMethod(events);
}
}
if (ugedag == DayOfWeek.Wednesday)
{
foreach (var events in getWednesday)
{
NewMethod(events);
}
}
if (ugedag == DayOfWeek.Thursday)
{
foreach (var events in getThursday)
{
NewMethod(events);
}
}
if (ugedag == DayOfWeek.Friday)
{
foreach (var events in getFriday)
{
NewMethod(events);
}
}
if (ugedag == DayOfWeek.Saturday)
{
foreach (var events in getSaturday)
{
NewMethod(events);
}
}
if (ugedag == DayOfWeek.Sunday)
{
foreach (var events in getSunday)
{
NewMethod(events);
}
}
}
private void NewMethod(GoogleEvents events)
{
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Monday)
{
Monday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Tuesday)
{
Tuesday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Wednesday)
{
Wednsday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Thursday)
{
Thuesday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Friday)
{
Friday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Saturday)
{
Saturday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
if (events.StartDate.Value.DayOfWeek == DayOfWeek.Sunday)
{
Sunday.Items.Add($"{events.Title} \n{events.StartDate:dd-MM HH:MM}-{events.EndDate:HH:MM}");
}
}