locked
Binding DisptacherTimer in SampleSataSource to show Countdown RRS feed

  • Question

  • Is there a way to bind Dispatchtimer tick event  to one of  field in SampleDataItem in SampleDataSource.cs .

    I want to show countdowntimer  where subtitle is how in default  SampleDataItme of groupedview page.

    for example to calculate the time left for 12 DEC i am using and in subtitle field i am just putting function name, but it shows time left for 12 DEC on starting of application, after that it becomes static.

     public string CalculateTimeRemaining()
            {
                DateTime daysLeft = DateTime.Parse("12/12/2013 12:00:01 AM");
                DateTime startDate = DateTime.Now;
    
                //Calculate countdown timer.
                TimeSpan t = daysLeft - startDate;
                int Days = t.Days;
                int Hours = t.Hours;
                int totalH = Hours + (Days * 24); ;
                string countDown = totalH.ToString() + ":" + t.Minutes.ToString() + ":" + t.Seconds.ToString();
                return countDown;
            }

    Is there a way show that it shows countdown per second and change value every second in GroupedItemView?

    Wednesday, December 4, 2013 9:45 AM

Answers

  • You need to update the value of Subtitle in the myTimer_Tick function for every item. You don't need to rebind.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Anne Jing Monday, December 16, 2013 9:49 AM
    Monday, December 9, 2013 3:05 PM
    Moderator

All replies

  • Bind the field SampledataItem to a custom property which implements INotifyPropertyChanged, then use the tick event to change the value of that property.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Wednesday, December 4, 2013 3:46 PM
    Moderator
  • I tried   this in usual SampleDataSource.cs generated with gridapp template

      public class SampleDataItem : SampleDataCommon,INotifyPropertyChanged

          {  

    private string _subtitle = String.Empty;
            public string Subtitle
            {
                get { return this._subtitle ; }
                set {
                    _subtitle = value;
                    OnPropertyChanged("Subtitle");            
                }
            }

    sampledataitem

     group1.Items.Add(new SampleDataItem("Group-1-Item-3",
                    "ABC",
                    "CalculateTimeRemaining()",
                    "Assets/3.png",
                    "Item Description",
                    ITEM_CONTENT,
                    group1, ));

    SampleDataSource.cs is default file in GridApp template of Windwos8 and sampledataitem is generated by template.

    I tried

    DispatchTimer myTimer = new DispatchTimer();
    
     public MainPage()
        {
            this.InitializeComponent();
            myTimer.Interval = TimeSpan.FromSeconds(1);
            myTimer.Tick += myTimer_Tick;
            myTimer.Start(); 
        }
    
    private void myTimer_Tick(object sender, object e)
        {  var sampleDataGroups = SampleDataSource.GetGroups("AllGroups");       
           groupetItemViewSource.Source = sampleDataGroups   //groupetItemViewSource is CollectionViewSource
         }
    but it doesn't work 
    Wednesday, December 4, 2013 4:58 PM
  • You need to update the value of Subtitle in the myTimer_Tick function for every item. You don't need to rebind.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Anne Jing Monday, December 16, 2013 9:49 AM
    Monday, December 9, 2013 3:05 PM
    Moderator