2016-09-20 160 views
0

我是新來的,在編程方面。我正在製作一個只顯示一週的節目。我有幾個星期的約會,我的問題是:有沒有辦法只顯示你當前所在星期的「約會」?WPF日曆周

+1

請分享你已經嘗試過,並在那裏你卡住了。 StackOverflow是幫助你,而不是爲你做的工作。另外請閱讀[問] – uruloke

+0

我已經在谷歌和YouTube上搜索了任何有關於我的問題後可能有任何問題。 –

+1

更像是以編程的方式分享你所做的事情,而不是使用谷歌搜索或觀看YouTube視頻。 –

回答

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}"); 
     } 
    } 
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}); 


      } 
     } 
    } 
} 
+0

我不知道爲什麼它不會允許我的公開課上的代碼 –

+0

但這一天og本週沒有工作,因爲我想它 –