Divis.AsyncObservableCollection 1.0.0.3

Suggested Alternatives

Divis.DarkHelpers

dotnet add package Divis.AsyncObservableCollection --version 1.0.0.3
NuGet\Install-Package Divis.AsyncObservableCollection -Version 1.0.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Divis.AsyncObservableCollection" Version="1.0.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Divis.AsyncObservableCollection --version 1.0.0.3
#r "nuget: Divis.AsyncObservableCollection, 1.0.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Divis.AsyncObservableCollection as a Cake Addin
#addin nuget:?package=Divis.AsyncObservableCollection&version=1.0.0.3

// Install Divis.AsyncObservableCollection as a Cake Tool
#tool nuget:?package=Divis.AsyncObservableCollection&version=1.0.0.3

How to use it?

Create the collection

Create a collection of type AsyncObservableCollection<T> somewhere in your code (your view model, for example).

public class MainViewModel
{
    public AsyncObservableCollection<string> Items { get; set; } = new AsyncObservableCollection<string>();
}

Initialize the collection

Before using it, you need to initialize the collection from each platform specific project. In my example, I have a WPF .NET Framework project and a Xamarin.Forms .NET Standard project. I'll initialize the collections for my view model from each platform, before I start using the view model.

The collection implements the IAsyncObservableCollection interface, so you can find all of them with reflection (see the InitAsyncCollectionsAutomatically method) or initialize them manually (see the InitAsyncCollections method).

WPF project

I have this method in the view (WPF Window) that is associated with the view model. It get's called from the constructor of the view.

public MainWindow()
{
    InitializeComponent();

    _model = new MainViewModel();
    this.DataContext = _model;

    //call the initialize method for async collections
    InitAsyncCollectionsAutomatically(_model);
}

/// <summary>
/// Initializes all properties that implement the IAsyncObservableCollection interface
/// </summary>
/// <param name="model">The object, that holds the collections</param>
private void InitAsyncCollectionsAutomatically(object model)
{
    //select all properties that implement IAsyncObservableCollection
    var collections = model.GetType().GetProperties()
    .Where(a => typeof(IAsyncObservableCollection).IsAssignableFrom(a.PropertyType))
    .Select(a => a.GetValue(model) as IAsyncObservableCollection);
    
    //initialize them all
    InitAsyncCollections(collections);
}

/// <summary>
/// Initializes an enumerable of IAsyncObservableCollection
/// </summary>
/// <param name="collections">The collections that need to be initialized</param>
private void InitAsyncCollections(IEnumerable<IAsyncObservableCollection> collections)
{
    foreach (var collection in collections)
    {
        //initialize the collection
        collection.Init(action => Dispatcher?.Invoke(() => action?.Invoke()));
    }
}

Xamarin.Forms project

And the exact same methods (with a Xamarin.Forms dispatcher) is in my view (Xamarin.Forms ContentPage), and it get's called from the constructor of the view, as well.

public MainPage()
{
    InitializeComponent();

    _model = new MainViewModel();
    this.BindingContext = _model;

    //call the initialize method for async collections
    InitAsyncCollectionsAutomatically(_model);
}

/// <summary>
/// Initializes all properties that implement the IAsyncObservableCollection interface
/// </summary>
/// <param name="model">The object, that holds the collections</param>
private void InitAsyncCollectionsAutomatically(object model)
{
    //select all properties that implement IAsyncObservableCollection
    var collections = model.GetType().GetProperties()
    .Where(a => typeof(IAsyncObservableCollection).IsAssignableFrom(a.PropertyType))
    .Select(a => a.GetValue(model) as IAsyncObservableCollection);

    //initialize them all
    InitAsyncCollections(collections);
}

/// <summary>
/// Initializes an enumerable of IAsyncObservableCollection
/// </summary>
/// <param name="collections">The collections that need to be initialized</param>
private void InitAsyncCollections(IEnumerable<IAsyncObservableCollection> collections)
{
    foreach (var collection in collections)
    {
        //initialize the collection
        collection.Init(action => Device.BeginInvokeOnMainThread(() => action?.Invoke()));
    }
}

And now, use it

Now you can do things like this, freely.

await Task.Run(() =>
{
    Items.Clear();
    Items.Add("Item 1");     
    Items.AddRange(new []
    {
        "Item 2", 
        "Item 3", 
    });         
});
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0.3 731 4/28/2020
1.0.0.2 558 12/2/2019
1.0.0.1 585 12/2/2019