1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

XamarinAdvent Calendar 2020

Day 22

Xamarin Community Toolkit カタログ(MVVM編)

Last updated at Posted at 2020-12-22

この記事は、Xamarin Advent Calendar 2020の22日目の記事です。

はじめに

以下の記事の続きです。

ソースコード
XamarinCommunityToolkitCatalog

MVVM

MVVMのビューモデルで使うような機能です。

AsyncCommand

Taskの非同期処理を扱えるコマンドです。allowsMultipleExecutionsfalseにすれば、Taskの実行中は、CanExecutefalseを返すようになり、ボタンの2度押しを防げます。デフォルトではtrueです。onExceptionで例外発生時の処理を、continueOnCapturedContextで、Task実行時に設定するConfigureAwaitの引数を指定できます。

private IAsyncCommand _asyncCommand;
public IAsyncCommand AsyncCommand => _asyncCommand ??=
    new AsyncCommand(async () =>
    {
        await Task.Delay(1000);
    }, allowsMultipleExecutions: false);

AsyncValueCommand

ValueTaskの非同期処理を扱えるコマンドです。機能的には、AsyncCommandと同じです。

private IAsyncValueCommand<bool> _asyncValueCommand;
public IAsyncValueCommand<bool> AsyncValueCommand => _asyncValueCommand ??=
    new AsyncValueCommand<bool>(async shouldDelay =>
    {
        if (shouldDelay)
        {
            await Task.Delay(1000);
        }
    }, allowsMultipleExecutions: false);

ObservableObject

INotifyPropertyChangedを実装したクラスです。SetPropertyPropertyChangedの発火を行えます。PropertyChangedのハンドラーは、WeakReferenceで管理されます。

public class ObservableObjectPageViewModel : ObservableObject
{
    private int _count;
    public int Count
    {
        get => _count;
        set => SetProperty(ref _count, value);
    }

    private ICommand _countCommand;
    public ICommand CountCommand => _countCommand ??=
        new DelegateCommand(() => Count++);
}

ObservableRangeCollection

複数データの追加、削除、置き換えができるObservableCollectionです。

public class ObservableRangeCollectionPageViewModel
{
    public ObservableRangeCollection<int> Items { get; } =
        new ObservableRangeCollection<int>();

    private ICommand _addRangeCommand;
    public ICommand AddRangeCommand => _addRangeCommand ??=
        new DelegateCommand(() =>
        {
            Items.AddRange(Enumerable.Range(Items.Count, 5));
        });

    private ICommand _removeRangeCommand;
    public ICommand RemoveRangeCommand => _removeRangeCommand ??=
        new DelegateCommand(() =>
        {
            Items.RemoveRange(Items.TakeLast(5));
        });

    private ICommand _replaceRangeCommand;
    public ICommand ReplaceRangeCommand => _replaceRangeCommand ??=
        new DelegateCommand(() =>
        {
            Items.ReplaceRange(Enumerable.Range(0, 5));
        });
}

おわりに

とりあえずこれで最後です。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?