Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 秒表MVVM Xamarin表格_C#_Visual Studio_Xamarin.forms - Fatal编程技术网

C# 秒表MVVM Xamarin表格

C# 秒表MVVM Xamarin表格,c#,visual-studio,xamarin.forms,C#,Visual Studio,Xamarin.forms,这是我第一次使用xamarin表单,我现在正在尝试为自己制作一个应用程序。在我的应用程序中,我在视图模型中设置了一个计时器,但秒数只会变为59秒,并自动重置为0。有人知道我如何解决这个问题,以及我如何在视图模型中设置停止按钮和重置按钮吗 public class TimerPageViewModel : ViewModelBase, INotifyPropertyChanged { Stopwatch stopwatch = new Stopwatch();

这是我第一次使用xamarin表单,我现在正在尝试为自己制作一个应用程序。在我的应用程序中,我在视图模型中设置了一个计时器,但秒数只会变为59秒,并自动重置为0。有人知道我如何解决这个问题,以及我如何在视图模型中设置停止按钮和重置按钮吗

    public class TimerPageViewModel : ViewModelBase, INotifyPropertyChanged
    {
        Stopwatch stopwatch = new Stopwatch();
        private string _stopWatchHours;
        private string _stopWatchMinutes;
        private string _stopWatchSeconds;
        public TimerPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            Title = "Timer";
            Start = new Command(OnStartTimerExecute);
            Stop = new Command(OnStop);
            Reset = new Command(onReset);
            StopWatchHours = stopwatch.Elapsed.Hours.ToString();
            StopWatchMinutes = stopwatch.Elapsed.Minutes.ToString();
            StopWatchSeconds = stopwatch.Elapsed.Seconds.ToString();
        }
        public string StopWatchHours
        {
            get { return _stopWatchHours; }
            set { _stopWatchHours = value; OnPropertyChanged("StopWatchHours"); }
        }
        public string StopWatchMinutes
        {
            get { return _stopWatchMinutes; }
            set { _stopWatchMinutes = value; OnPropertyChanged("StopWatchSeconds"); }
        }
        public string StopWatchSeconds
        {
            get { return _stopWatchSeconds; }
            set { _stopWatchSeconds = value; OnPropertyChanged("StopWatchSeconds"); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var changed = PropertyChanged;
            if (changed != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public ICommand Start { get; set; }
        public ICommand Stop { get; set; }
        public ICommand Reset { get; set; }
        private void OnStartTimerExecute()
        {
            stopwatch.Start();
            Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                StopWatchHours = stopwatch.Elapsed.Hours.ToString();
                StopWatchMinutes = stopwatch.Elapsed.Minutes.ToString();
                StopWatchSeconds = stopwatch.Elapsed.Seconds.ToString();
                return true;
            });
        }
        private void OnStop()
        {
            stopwatch.Stop();
            stopwatch = null;
        }
        private void onReset()
        {
            stopwatch.Reset();
        }
    }   

您正在对PropertyChanged(“秒表秒”)进行
而不是
OnPropertyChanged(“秒表分钟”)


“秒只到59,自动重置为0”-这不是秒表的正常行为吗?要启动/停止,您需要在虚拟机中定义命令并绑定它们-您似乎已经在这样做了,因此我不确定您的问题是什么。抱歉,我的意思是,在59秒之后,分钟仍然是0。通常在59秒后,分钟冲刺到1,但在这种情况下,它不会发生。天哪!我真蠢!非常感谢你!
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="http://prismlibrary.com"
             prism:ViewModelLocator.AutowireViewModel="True"
             NavigationPage.HasNavigationBar="false"
             x:Class="MyApp.Views.TimerPage"
             IconImageSource="timer_icon_2x.png"
             Title="{Binding Title}">

    <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <Label HorizontalOptions="Center" FontSize="45" TextColor="#00A8E8">
        <Label.FormattedText>
            <FormattedString>
                    <Span Text="{Binding StopWatchHours}"/>
                    <Span Text=":"/>
                    <Span Text="{Binding StopWatchMinutes}"/>
                    <Span Text=":"/>
                    <Span Text="{Binding StopWatchSeconds}"/>
                </FormattedString>
        </Label.FormattedText>
        </Label>
        <Button Command="{Binding Start}"/>
        <Button Command="{Binding Stop}"/>
        <Button Command="{Binding Reset}"/>
    </StackLayout>

</ContentPage>
public string StopWatchMinutes
    {
        get { return _stopWatchMinutes; }
        set { _stopWatchMinutes = value; 
              OnPropertyChanged("StopWatchSeconds");
        }
    }