runceel / ReactiveProperty

ReactiveProperty provides MVVM and asynchronous support features under Reactive Extensions. Target frameworks are .NET 6+, .NET Framework 4.7.2 and .NET Standard 2.0.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Collection. Init values.

FoxTes opened this issue · comments

Colleagues, hello.
Help is needed. I want to set and localize a collection with initial values, and then use the add new value command. I got the following code.

public ReactiveCollection<Project> Projects { get; }

Projects = Observable
   .FromAsync(() => _projectManager.GetAllAsync())
   .SelectMany(x => x)
   .ToReactiveCollection();
Projects = AddProject
   .Select(_ => ShowFolderBrowserDialog())
   .Where(x => x != null)
   .ToReactiveCollection();

It doesn't work because the default value is set first, and then the command is triggered. Tell me how best to implement this behavior. After looking at the examples, I realized that I need to use Concat. But in this case, I do not know how to apply it. Thanks!

ReactiveCollection<T> has Add method and AddOnScheduler method. I guess in your situation it works as well.

public ReactiveCollection<Project> Projects { get; }

Projects = Observable
   .FromAsync(() => _projectManager.GetAllAsync())
   .SelectMany(x => x)
   .ToReactiveCollection();
AddProject // I believe AddProject is ReactiveCommand  
   .Select(_ => ShowFolderBrowserDialog())
   .Where(x => x != null)
   .Subscribe(x => Projects.Add(x)); // If you want to do adding operation on UI thread, then please use AddOnScheduler method.

And I suggest calling Dispose method for all ReactiveProperty, ReactiveCommand and ReactiveCollection instances on end of lifecycle of ViewModel classes.

See: https://okazuki.jp/ReactiveProperty/features/Extension-methods.html#addto

I hope it helps for you.

Yes, that's exactly what I need. For some reason I forgot that there is an Add method, oh, this reactive approach.

By the way, is it okay that I'm asking such banal questions here? Maybe you have a community?

@FoxTes

By the way, is it okay that I'm asking such banal questions here? Maybe you have a community?

Sure. I don't have any community for ReactiveProperty. And I haven't checked FAQ sites such as stackoverflow. So, here is a best location for asking something about this library. If questions will be huge numbers, then I will announce a new place for that. But for now, I don't think somewhere for that is needed.