private void SurfaceButton_Click_1(object sender, RoutedEventArgs e) { //Scrollt die SurfaceListBox auf den heutigen Eintrag try { MensaMenu.ScrollIntoView(MensaMenu.Items[todayID]); } catch {} }
public void getMensaMenu() { //Lädt die Speisekarte der Mensa per HttpWebRequest und verarbeitet den HTML-Inhalt zu passenden strings, die in SurfaceListBoxItems dargestellt werden. //Einblenden der Lade-Animation this.Dispatcher.Invoke(new Action(delegate { loadAnim.Visibility = Visibility.Visible; })); int i = 0; try{ HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.studentenwerk-muenchen.de/mensa/speiseplan/speiseplan_431_-de.html#heute"); //HttpWebRequest myRequest = // (HttpWebRequest)WebRequest.Create("http://www.studentenwerk-muenchen.de/mensa/speiseplan/speiseplan_411_-de.html#heute"); myRequest.Method = "GET"; myRequest.ContentType = "application/x-www-form-urlencoded"; WebResponse response = myRequest.GetResponse(); //Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"), false); String responsestring = reader.ReadToEnd(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(responsestring); HtmlNodeCollection menunodes = doc.DocumentNode.SelectNodes("//table[@class='menu']"); timer.Interval = TimeSpan.FromHours(1); this.Dispatcher.Invoke(new Action(delegate { //Löschen der bestehenden MensaMenu-Einträge MensaMenu.Items.Clear(); })); //Erstellen der neuen Einträge foreach (HtmlNode currentNode in menunodes) { this.Dispatcher.Invoke(new Action(delegate { StackPanel panel = new StackPanel(); string date = ""; string dish = ""; string desc = ""; string type = ""; date = currentNode.SelectSingleNode(".//strong").InnerText.Trim(); panel.FlowDirection = FlowDirection.LeftToRight; Grid grid = new Grid(); grid.IsHitTestVisible = false; Rectangle rectangle = new Rectangle(); //StackPanel panel = new StackPanel(); Grid grid2 = new Grid(); rectangle.Fill = Brushes.Gray; if (date.Contains(DateTime.Now.ToShortDateString())) { //MessageBox.Show(i.ToString()); todayID = i; rectangle.Fill = Brushes.DarkGray; } //rectangle.RadiusX = 10; //rectangle.RadiusY = 10; //rectangle.Opacity = 0; rectangle.Margin = new Thickness(0); //DropShadowEffect effect = new DropShadowEffect(); //effect.BlurRadius = 20; //rectangle.Effect = effect; grid.Children.Add(rectangle); panel.HorizontalAlignment = HorizontalAlignment.Center; grid2.Children.Add(panel); TextBlock dateLabel = new TextBlock(); grid.Children.Add(grid2); grid2.Margin = new Thickness(10); dateLabel.Foreground = Brushes.Black; MensaMenu.Items.Add(grid); dateLabel.FontSize = 20; dateLabel.FontWeight = FontWeights.DemiBold; dateLabel.Width = 250; if (!date.Equals("")) { panel.Children.Add(dateLabel); Border border1 = new Border(); border1.Height = 10; panel.Children.Add(border1); dateLabel.Text = date; } //Html-Parsing, Füllen der strings des aktuellen Menüeintrags mit den entsprechenden Werten foreach (HtmlNode dishNode in currentNode.SelectNodes(".//tr")) { //try //{ if (dishNode.SelectSingleNode("./td[@class='gericht']") != null) { dish = dishNode.SelectSingleNode("./td[@class='gericht']").InnerText.Trim(); } //} //catch { } //try //{ if (dishNode.SelectSingleNode("./td[@class='beschreibung']/span[@style='float:left']") != null) { desc = dishNode.SelectSingleNode("./td[@class='beschreibung']/span[@style='float:left']").InnerText.Trim(); } //} //catch { } //try //{ if (dishNode.SelectSingleNode("./td[@class='beschreibung']/span[@class='fleisch']") != null) { type = dishNode.SelectSingleNode("./td[@class='beschreibung']/span[@class='fleisch']").InnerText.Replace(" ", "").Trim(); } //} //catch { } //try //{ if (dishNode.SelectSingleNode("./td[@class='beschreibung']/span[@class='fleischlos']") != null) { type = dishNode.SelectSingleNode("./td[@class='beschreibung']/span[@class='fleischlos']").InnerText.Replace(" ", "").Trim(); } //} //catch { } //Definition der Schriftelemente des Menüeintrags TextBlock dishLabel = new TextBlock(); TextBlock descLabel = new TextBlock(); TextBlock typeLabel = new TextBlock(); dishLabel.Text = dish; descLabel.Text = desc; typeLabel.Text = type; dishLabel.FontSize = 15; dishLabel.Width = 250; descLabel.FontSize = 15; descLabel.FontWeight = FontWeights.DemiBold; typeLabel.FontSize = 15; typeLabel.Width = 250; descLabel.Width = 250; descLabel.TextWrapping = TextWrapping.Wrap; descLabel.Foreground = Brushes.Black; dishLabel.Foreground = Brushes.WhiteSmoke; typeLabel.Foreground = Brushes.WhiteSmoke; if (!dish.Equals("")) { panel.Children.Add(dishLabel); Border border2 = new Border(); border2.Height = 5; panel.Children.Add(border2); } if (!desc.Equals("")) { panel.Children.Add(descLabel); Border border3 = new Border(); border3.Height = 2.5; panel.Children.Add(border3); } if (!type.Equals("")) { panel.Children.Add(typeLabel); Border border4 = new Border(); border4.Height = 10; panel.Children.Add(border4); } } if (MensaMenu.Items.Count > 0) { //Scrollt die SurfaceListBox auf den heutigen Eintrag MensaMenu.ScrollIntoView(MensaMenu.Items[todayID]); } })); i = i + 1; } } catch { } //Ausblenden der Lade-Animation this.Dispatcher.Invoke(new Action(delegate { loadAnim.Visibility = Visibility.Hidden; })); }