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

Chapter 9 Local Data

calling Dispatcher.GetForCurrentThread().Dispatch you are queuing up an action to be performed once the UI thread is no longer busy. .NET MAUI does handle a lot of dispatching for you when you trigger updates in bindings, but there are times when you need to make sure that you are updating things on the UI thread.

If you run your code now, you can create a new board and view it once saved. If you then close and reopen the application, you will see that the board you created is now shown for you. Providing an experience like this can go a long way to an enjoyable user experience (UX) as they are returning to where they were previously.

Checking if a Key Exists in Preferences

There can be times when you are unable to supply a suitable default value to the Get method in order to know whether a value has been set, for example using a Boolean. false is a valid value and therefore the

default value would not be able to distinguish whether it was set as false or the default value of false. In this scenario, you can make use of the ContainsKey method. So instead of writing

var lastUsedBoardId = preferences.Get("LastUsedBoardId", -1);

you could have first checked whether the key existed, like

if (preferences.ContainsKey("LastUsedBoardId"))

{

// Perform your logic

}

290