C# 如何向绑定wpf uwp c添加计时器#

C# 如何向绑定wpf uwp c添加计时器#,c#,json,timer,binding,win-universal-app,C#,Json,Timer,Binding,Win Universal App,如何为所有绑定对象添加计时器?我有一个自行车列表,其中包含两个属性(我从JSON获得属性): 使用myApi; 公共密封部分类自行车示例:第页 { 列表=新列表(); 公共自行车示例() { this.InitializeComponent(); //获取JSON 动态getList=myApi.Api.getList(); Newtonsoft.Json.Linq.JArray y=getList.list; 对于(int x=0;xx.EndTimeinTimeSpan); timertos

如何为所有绑定对象添加计时器?我有一个自行车列表,其中包含两个属性(我从JSON获得属性):

使用myApi;
公共密封部分类自行车示例:第页
{
列表=新列表();
公共自行车示例()
{
this.InitializeComponent();
//获取JSON
动态getList=myApi.Api.getList();
Newtonsoft.Json.Linq.JArray y=getList.list;
对于(int x=0;x
和xaml类似:

<ListView x:Name="BikesList" Margin="0,60,-360,-630">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="360" VerticalAlignment="Center">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding number}"/>

                        <TextBlock Grid.Column="1" Text="{Binding endtime}"/> //Here should be a timer mm:ss

                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

//这里应该有一个计时器mm:ss

我需要将ListView中的所有“endtime”属性设置为计时器。我试图使用Dispatchermer,但我不明白应该在何处以及如何使用Dispatchermer.Tick方法。以秒为单位获取Endtime属性。

我使用了MVVM概念。实现INotifyPropertyChanged for Bike,因为它必须每秒钟更新一次ui

    public class Bike:INotifyPropertyChanged
        {
            public string number { get; set; }
            public string endtime { get; set; }
            string _foramtedtime;
            public string FormattedEndTime
            {
                get
                {
                    return _foramtedtime;
                }
                set
                {

                    if(value!=_foramtedtime)
                    {
                        _foramtedtime = value;
                        OnPropertyChanged("FormattedEndTime");
                    }
                }
                }

            void OnPropertyChanged(string propertyName)
            {
                // the new Null-conditional Operators are thread-safe:
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }


            public TimeSpan EndTimeinTimeSpan;

            public event PropertyChangedEventHandler PropertyChanged;

            public Bike(string numb,string endtim)
            {
                number = numb;
                endtime = endtim;
                var seconds = int.Parse(endtime);
                FormattedEndTime=  string.Format("{0:00}:{1:00}",(seconds / 60) % 60, seconds % 60);
                EndTimeinTimeSpan = TimeSpan.Parse(string.Format("{0:00}:{1:00}:{2:00}", seconds / 3600, (seconds / 60) % 60, seconds % 60));
            }
        }
    public sealed partial class MainPage : Page
        {
               VM = new TestViewModel();
            DataContext = VM;
            this.Loaded += MainPage_Loaded;
                DispatcherTimer timertostoop;
                DispatcherTimer timer;
               public MainPage()
               {
                       TimeSpan time =( DataContext as TestViewModel).Bikes.Max(x => x.EndTimeinTimeSpan);
                timertostoop   = new DispatcherTimer() { Interval = time };
                timertostoop.Tick += Timertostoop_Tick;
                timer = new DispatcherTimer() { Interval =TimeSpan.FromSeconds(1) };
                timer.Tick += Timer_Tick;
               }
                 private void Timertostoop_Tick(object sender, object e)
            {
                if (timer != null && timer.IsEnabled)
                    timer.Stop();
                timertostoop.Stop();
            }

            private async void MainPage_Loaded(object sender, RoutedEventArgs e)
            {

                timer.Start();

            }
            private void Timer_Tick(object sender, object e)
            {
       if(!timertostoop.IsEnabled)
            timertostoop.Start();
              foreach(var bike in VM.Bikes)
                {
                    if(bike.EndTimeinTimeSpan>TimeSpan.FromSeconds(0))
                    {
                        bike.EndTimeinTimeSpan = bike.EndTimeinTimeSpan - TimeSpan.FromSeconds(1);
                        bike.FormattedEndTime = string.Format("{0:00}:{1:00}", Math.Floor((bike.EndTimeinTimeSpan.TotalSeconds/ 60) ), bike.EndTimeinTimeSpan.Seconds);
                    }
                }
            }

        }
  <ListView x:Name="BikesList" ItemsSource="{Binding Bikes}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="360" VerticalAlignment="Center">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding number}"/>

                        <TextBlock Grid.Column="1" Text="{Binding FormattedEndTime}"/> 

                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
公共类自行车:InotifyProperty已更改
{
公共字符串编号{get;set;}
公共字符串结束时间{get;set;}
字符串_,用于指定时间;
公共字符串FormattedEndTime
{
得到
{
返回时间为_;
}
设置
{
如果(值!=\u用于平均时间)
{
_foramtedtime=值;
OnPropertyChanged(“FormattedEndTime”);
}
}
}
void OnPropertyChanged(字符串propertyName)
{
//新的空条件运算符是线程安全的:
this.PropertyChanged?.Invoke(this,newpropertychangedeventargs(propertyName));
}
公共时间跨度EndTimeinTimeSpan;
公共事件属性更改事件处理程序属性更改;
公共自行车(字符串编号、字符串结束时间)
{
数字=麻木;
endtime=endtim;
var seconds=int.Parse(endtime);
FormattedEndTime=string.Format(“{0:00}:{1:00}”,(秒/60)%60,秒%60);
EndTimeinTimeSpan=TimeSpan.Parse(string.Format(“{0:00}:{1:00}:{2:00}”),seconds/3600,(seconds/60)%60,seconds%60));
}
}
公共密封部分类主页面:第页
{
VM=新的TestViewModel();
DataContext=VM;
this.Loaded+=主页面_Loaded;
调度员定时器;
调度定时器;
公共主页()
{
TimeSpan time=(DataContext作为TestViewModel.Bikes.Max(x=>x.EndTimeinTimeSpan);
timertostop=newdispatchermer(){Interval=time};
timertoop.Tick+=timertoop\u Tick;
timer=newdispatchermer(){Interval=TimeSpan.FromSeconds(1)};
timer.Tick+=定时器_Tick;
}
私有无效计时器停止勾选(对象发送方,对象e)
{
if(timer!=null&&timer.IsEnabled)
timer.Stop();
timertoop.Stop();
}
已加载专用异步void主页(对象发送方、路由目标)
{
timer.Start();
}
私有无效计时器(对象发送方,对象e)
{
如果(!TimerToop.IsEnabled)
timertoop.Start();
foreach(VM.Bikes中的变量bike)
{
if(bike.EndTimeinTimeSpan>TimeSpan.FromSeconds(0))
{
bike.EndTimeinTimeSpan=bike.EndTimeinTimeSpan-TimeSpan.fromses(1);
bike.FormattedEndTime=string.Format(“{0:00}:{1:00}”、Math.Floor((bike.EndTimeinTimeSpan.TotalSeconds/60))、bike.EndTimeinTimeSpan.Seconds);
}
}
}
}

您只能使用一个计时器,其间隔必须是所有endtime中的最高值。并递减所有endtime的值,直到它变为零。这就是你想要的吗?如果是这样的话,我可以把它寄出去。如果你把它寄出去就好了。谢谢。您也可以在总秒数中获得分钟数?JSON endtime属性类似:[endtime=“1210”],它是20分10秒。您如何转换它?
    public class Bike:INotifyPropertyChanged
        {
            public string number { get; set; }
            public string endtime { get; set; }
            string _foramtedtime;
            public string FormattedEndTime
            {
                get
                {
                    return _foramtedtime;
                }
                set
                {

                    if(value!=_foramtedtime)
                    {
                        _foramtedtime = value;
                        OnPropertyChanged("FormattedEndTime");
                    }
                }
                }

            void OnPropertyChanged(string propertyName)
            {
                // the new Null-conditional Operators are thread-safe:
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }


            public TimeSpan EndTimeinTimeSpan;

            public event PropertyChangedEventHandler PropertyChanged;

            public Bike(string numb,string endtim)
            {
                number = numb;
                endtime = endtim;
                var seconds = int.Parse(endtime);
                FormattedEndTime=  string.Format("{0:00}:{1:00}",(seconds / 60) % 60, seconds % 60);
                EndTimeinTimeSpan = TimeSpan.Parse(string.Format("{0:00}:{1:00}:{2:00}", seconds / 3600, (seconds / 60) % 60, seconds % 60));
            }
        }
    public sealed partial class MainPage : Page
        {
               VM = new TestViewModel();
            DataContext = VM;
            this.Loaded += MainPage_Loaded;
                DispatcherTimer timertostoop;
                DispatcherTimer timer;
               public MainPage()
               {
                       TimeSpan time =( DataContext as TestViewModel).Bikes.Max(x => x.EndTimeinTimeSpan);
                timertostoop   = new DispatcherTimer() { Interval = time };
                timertostoop.Tick += Timertostoop_Tick;
                timer = new DispatcherTimer() { Interval =TimeSpan.FromSeconds(1) };
                timer.Tick += Timer_Tick;
               }
                 private void Timertostoop_Tick(object sender, object e)
            {
                if (timer != null && timer.IsEnabled)
                    timer.Stop();
                timertostoop.Stop();
            }

            private async void MainPage_Loaded(object sender, RoutedEventArgs e)
            {

                timer.Start();

            }
            private void Timer_Tick(object sender, object e)
            {
       if(!timertostoop.IsEnabled)
            timertostoop.Start();
              foreach(var bike in VM.Bikes)
                {
                    if(bike.EndTimeinTimeSpan>TimeSpan.FromSeconds(0))
                    {
                        bike.EndTimeinTimeSpan = bike.EndTimeinTimeSpan - TimeSpan.FromSeconds(1);
                        bike.FormattedEndTime = string.Format("{0:00}:{1:00}", Math.Floor((bike.EndTimeinTimeSpan.TotalSeconds/ 60) ), bike.EndTimeinTimeSpan.Seconds);
                    }
                }
            }

        }
  <ListView x:Name="BikesList" ItemsSource="{Binding Bikes}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="360" VerticalAlignment="Center">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding number}"/>

                        <TextBlock Grid.Column="1" Text="{Binding FormattedEndTime}"/> 

                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>