【问题标题】:Stopwatch MVVM Xamarin Forms秒表 MVVM Xamarin 表单
【发布时间】:2021-08-12 04:22:59
【问题描述】:

这是我第一次使用 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();
        }
    }   
<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>

【问题讨论】:

  • "秒只到 59 并自动重置为 0" - 这不是秒表的正常行为吗?要启动/停止,您需要在 VM 中定义命令并绑定它们——您似乎已经在这样做了,所以我不确定您的问题是什么。
  • 抱歉,我的意思是在我的 59 秒后分钟仍然为 0。通常在 59 秒后分钟冲刺到 1,但在这种情况下它不会发生。

标签: c# visual-studio xamarin.forms


【解决方案1】:

您正在提高 OnPropertyChanged("StopWatchSeconds"); 而不是 OnPropertyChanged("StopWatchMinutes");

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2018-10-20
    • 2018-05-22
    • 1970-01-01
    • 2020-08-04
    相关资源
    最近更新 更多