From 5aec064536de7ba678569f40c3985e3c8a6621a8 Mon Sep 17 00:00:00 2001 From: Adrien VAN DAMME Date: Sun, 27 Jan 2019 18:50:01 +0100 Subject: [PATCH] DashBoard update every 5sec --- ProjetTheAlone/View/DashBoard.xaml.cs | 53 +++++++++++++++++------- ProjetTheAlone/ViewModel/TextDefilant.cs | 33 ++++++++++----- ProjetTheAlone/ViewModel/VM_DashBoard.cs | 10 +++++ 3 files changed, 72 insertions(+), 24 deletions(-) diff --git a/ProjetTheAlone/View/DashBoard.xaml.cs b/ProjetTheAlone/View/DashBoard.xaml.cs index 29a3328..8d3884e 100644 --- a/ProjetTheAlone/View/DashBoard.xaml.cs +++ b/ProjetTheAlone/View/DashBoard.xaml.cs @@ -17,6 +17,8 @@ using System.Windows.Shapes; using ProjetTheAlone.Model; using ProjetTheAlone.Classes; using ProjetTheAlone.ViewModel; +using System.Threading; +using System.Timers; namespace ProjetTheAlone.View { @@ -27,6 +29,7 @@ namespace ProjetTheAlone.View public partial class DashBoard : Window { + public System.Timers.Timer aTimer; ObservableCollection cl = new ObservableCollection(); RepaModel rm = new RepaModel(); View.FicDetailEvent W_detailEvent = null; @@ -58,20 +61,27 @@ namespace ProjetTheAlone.View InitializeComponent(); //EventPasse2.DataContext = EventPasse1.DataContext = Epm; - - testQuand.DataContext = RepasAjd.DataContext = new RepaModel(DateTime.Now); - RepasDemain.DataContext = new RepaModel(DateTime.Now.AddDays(1)); - RepasApresDemain.DataContext = new RepaModel(DateTime.Now.AddDays(2)); - - dgEventPasse2.DataContext = new EventDGV_VM(DateTime.Now.AddDays(-3)); - dgEventPasse1.DataContext = new EventDGV_VM(DateTime.Now.AddDays(-2)); - dgEventPasse0.DataContext = new EventDGV_VM(DateTime.Now.AddDays(-1)); - dgEventFuture0.DataContext = new EventDGV_VM(DateTime.Now.AddDays(0)); - dgEventFuture1.DataContext = new EventDGV_VM(DateTime.Now.AddDays(1)); - dgEventFuture2.DataContext = new EventDGV_VM(DateTime.Now.AddDays(2)); - + setAllDataContext(); + SetTimer(5000); + aTimer.Enabled = true; + } + private void SetTimer(int timeMs) + { + // Create a timer with a two second interval. + aTimer = new System.Timers.Timer(timeMs); + // Hook up the Elapsed event for the timer. + aTimer.Elapsed += OnTimedEvent; + aTimer.AutoReset = true; } + private void OnTimedEvent(Object source, ElapsedEventArgs e) + { + wnd.Dispatcher.Invoke(() => + { + setAllDataContext(); + }); + + } private void wnd_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Accueil.Openaffichage = false; @@ -105,11 +115,13 @@ namespace ProjetTheAlone.View if (((DataGrid)sender).SelectedItems.Count == 0) return; C_T_event _eventSelected = ((C_T_event)((DataGrid)sender).SelectedItem); - if(W_detailEvent == null || W_detailEvent != null && W_detailEvent.IsClosed) + if (W_detailEvent == null || W_detailEvent != null && W_detailEvent.IsClosed) { W_detailEvent = new FicDetailEvent(_eventSelected); - + } + else + W_detailEvent.setDataContext(_eventSelected); if(!W_detailEvent.IsVisible) { W_detailEvent.Show(); @@ -117,6 +129,19 @@ namespace ProjetTheAlone.View W_detailEvent.Activate(); e.Handled = true; } + private void setAllDataContext() + { + RepasAjd.DataContext = new RepaModel(DateTime.Now); + RepasDemain.DataContext = new RepaModel(DateTime.Now.AddDays(1)); + RepasApresDemain.DataContext = new RepaModel(DateTime.Now.AddDays(2)); + + dgEventPasse2.DataContext = new EventDGV_VM(DateTime.Now.AddDays(-3)); + dgEventPasse1.DataContext = new EventDGV_VM(DateTime.Now.AddDays(-2)); + dgEventPasse0.DataContext = new EventDGV_VM(DateTime.Now.AddDays(-1)); + dgEventFuture0.DataContext = new EventDGV_VM(DateTime.Now.AddDays(0)); + dgEventFuture1.DataContext = new EventDGV_VM(DateTime.Now.AddDays(1)); + dgEventFuture2.DataContext = new EventDGV_VM(DateTime.Now.AddDays(2)); + } } public class DebugDummyConverter : IValueConverter { diff --git a/ProjetTheAlone/ViewModel/TextDefilant.cs b/ProjetTheAlone/ViewModel/TextDefilant.cs index b24c519..6202f29 100644 --- a/ProjetTheAlone/ViewModel/TextDefilant.cs +++ b/ProjetTheAlone/ViewModel/TextDefilant.cs @@ -14,11 +14,18 @@ namespace ProjetTheAlone.ViewModel { public class TextDefilant : BasePropriete { + + public event TickHandler Tick; + public EventArgs e = null; + public delegate void TickHandler(object o, EventArgs e); + + public Timer aTimer; public TextBlock textContainer; char[] textAfficherBuffer; - public string TextAfficher { set { fillBuffer(value); } get { return new string(textAfficherBuffer); } } + string textAfficher; + public string TextAfficher { set { fillBuffer(value); textAfficher = value; } get { return new string(textAfficherBuffer); } } public TextDefilant(TextBlock tb, string text, int timeMs) @@ -41,6 +48,7 @@ namespace ProjetTheAlone.ViewModel private void OnTimedEvent(Object source, ElapsedEventArgs e) { textAfficherBuffer = RotateChar(textAfficherBuffer); + Tick?.Invoke(this, null); OnPropertyChanged("TextAfficher"); } @@ -52,7 +60,7 @@ namespace ProjetTheAlone.ViewModel double WTB = textContainer.ActualWidth; //With of textBlock container double WT = MeasureString(text).Width; //With of Text double WS = MeasureString(" ").Width; //With of whiteSpace - int nbSp = (int)Math.Ceiling((WTB - WT) / WS); //number of space need to fill textBlock + int nbSp = (int)Math.Ceiling((WTB-WT) / WS); //number of space need to fill textBlock char[] spText = new string(' ', nbSp).ToCharArray(); this.textAfficherBuffer = new char[nbSp + text.ToCharArray().Length]; Array.Copy(text.ToCharArray(), 0, this.textAfficherBuffer, 0, text.Length); @@ -72,14 +80,19 @@ namespace ProjetTheAlone.ViewModel //https://stackoverflow.com/questions/9264398/how-to-calculate-wpf-textblock-width-for-its-known-font-size-and-characters private Size MeasureString(string candidate) { - var formattedText = new FormattedText( - candidate, - CultureInfo.CurrentCulture, - FlowDirection.LeftToRight, - new Typeface(this.textContainer.FontFamily, this.textContainer.FontStyle, this.textContainer.FontWeight, this.textContainer.FontStretch), - this.textContainer.FontSize, - Brushes.Black, - new NumberSubstitution(), (TextFormattingMode)1); + FormattedText formattedText = null; + this.textContainer.Dispatcher.Invoke(() => + { + formattedText = new FormattedText( + candidate, + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + new Typeface(this.textContainer.FontFamily, this.textContainer.FontStyle, this.textContainer.FontWeight, this.textContainer.FontStretch), + this.textContainer.FontSize, + Brushes.Black, + new NumberSubstitution(), (TextFormattingMode)1); + }); + return new Size(formattedText.WidthIncludingTrailingWhitespace, formattedText.Height); } diff --git a/ProjetTheAlone/ViewModel/VM_DashBoard.cs b/ProjetTheAlone/ViewModel/VM_DashBoard.cs index eec460a..cacda59 100644 --- a/ProjetTheAlone/ViewModel/VM_DashBoard.cs +++ b/ProjetTheAlone/ViewModel/VM_DashBoard.cs @@ -10,6 +10,7 @@ namespace ProjetTheAlone.ViewModel { public class VM_DashBoard : TextDefilant { + int tick = 0; public StringCollection Alerte { get @@ -29,6 +30,7 @@ namespace ProjetTheAlone.ViewModel public VM_DashBoard(TextBlock tb, string text, int timeMs) : base (tb,text, timeMs) { updateAlert(); + base.Tick += new TickHandler(onTick); } public void updateAlert() { @@ -45,5 +47,13 @@ namespace ProjetTheAlone.ViewModel } base.TextAfficher = alert; } + public void onTick(object o, EventArgs e) + { + if(++tick > base.TextAfficher.Length) + { + tick = 0; + updateAlert(); + } + } } }