Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 4 An Architecture to Suit You

return false;

}

backingField = value;

OnPropertyChanged(propertyName);

return true;

}

The SetProperty method does the following:

•\

Allows you to call it from a property setter, passing in

 

the field and value being set

•\

Checks whether the value is different from the backing

 

field, basically determining whether the property has

 

really changed

•\

If it has changed, it fires the PropertyChanged event

 

using your new OnPropertyChanged method

•\

Returns a Boolean indicating whether the value did

 

really change. This can be really useful when needing

 

to update other properties or commands!

This concludes the base view model implementation. Let’s proceed to using it as the base for the ClockWidgetViewModel to really appreciate the value it is providing.

Adding ClockWidgetViewModel

Let’s add a new class file into your ViewModels folder as you did for the

BaseViewModel.cs file. Call this file ClockWidgetViewModel and modify the contents to the following:

using System;

94

Chapter 4 An Architecture to Suit You

using System.ComponentModel;

namespace WidgetBoard.ViewModels;

public class ClockWidgetViewModel : BaseViewModel, IWidgetViewModel

{

private readonly Scheduler scheduler = new(); private DateTime time;

public DateTime Time

{

get => time;

set => SetProperty(ref time, value);

}

public int Position { get; set; }

public string Type => "Clock";

public ClockWidgetViewModel()

{

SetTime(DateTime.Now);

}

public void SetTime(DateTime dateTime)

{

Time = dateTime;

scheduler.ScheduleAction(

TimeSpan.FromSeconds(1),

() => SetTime(DateTime.Now));

}

}

95