SampleUpdatingItemsPage.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Timers; 6 using Microsoft.CommandPalette.Extensions; 7 using Microsoft.CommandPalette.Extensions.Toolkit; 8 using Microsoft.VisualBasic; 9 10 namespace SamplePagesExtension; 11 12 public partial class SampleUpdatingItemsPage : ListPage 13 { 14 private readonly ListItem hourItem = new(new NoOpCommand()); 15 private readonly ListItem minuteItem = new(new NoOpCommand()); 16 private readonly ListItem secondItem = new(new NoOpCommand()); 17 private static Timer timer; 18 19 public SampleUpdatingItemsPage() 20 { 21 Name = "Open"; 22 Icon = new IconInfo("\uE72C"); 23 } 24 25 public override IListItem[] GetItems() 26 { 27 if (timer is null) 28 { 29 timer = new Timer(500); 30 timer.Elapsed += (object source, ElapsedEventArgs e) => 31 { 32 var current = DateAndTime.Now; 33 hourItem.Title = $"{current.Hour}"; 34 minuteItem.Title = $"{current.Minute}"; 35 secondItem.Title = $"{current.Second}"; 36 }; 37 timer.AutoReset = true; // Keep repeating 38 timer.Enabled = true; 39 } 40 41 return [hourItem, minuteItem, secondItem]; 42 } 43 }