kekekeks / NObservable

MobX like observable state management library with Blazor support
MIT License
68 stars 7 forks source link

Xamarin.Forms support? #3

Open Lelelo1 opened 5 years ago

Lelelo1 commented 5 years ago

Any idea on how to get this working for Xamarin Forms?

Is it "as simple as" replicating what is in NObservable.Blazor?

kekekeks commented 5 years ago

Xamarin Forms uses INotifyPropertyChanged to observe changes for each Binding instance individually. So integration with Xamarin.Forms (and any other MVVM-friendly XAML framework such as WPF, UWP and AvaloniaUI) would be emitting INPC notifications for observable and computed properties. While being doable, it would be different from Blazor integration that uses functional virtual DOM approach.

Lelelo1 commented 5 years ago

Yes that might be the case.

I have also noticed I can't make partial classes Observable. I think InitializeComponent(); has to run in constructor first before applying Observable. This can be done with decorate() method in the mobx js library. The error I get is a System.InvalidProgramException with message: Invalid IL in MyClass:.cctor (): IL_0000: ldarg.0. The same error also occur when marking a property Observable inside a partial class.

Lelelo1 commented 4 years ago

I am currently using the following:

public class Bindable : INotifyPropertyChanged
{
    protected Bindable()
    {
        var properties = GetType().GetRuntimeProperties();
        foreach(var p in properties)
        {
            Observe.Autorun(() =>
            {
                var run = p.GetValue(this);
                OnPropertyChanged(p.Name);
            });
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then all viewmodels or models can trigger ui changes:

public class Person : Bindable
{
   [Observable]
   public string AvatarImg { get; set; }
   [Observable]
   public bool IsPresent { get; set; }

   public string Id { get; set; }
}

And setting binding context and using binding in either XAML or C# should work like normal.

Lelelo1 commented 4 years ago

When trying to build a custom control that is instantiated in with xaml though ...

Attempting to JIT compile method 'void ScrollCollectionViewTest.ScrollCollectionView:.cctor ()' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

[Observable]
public class ScrollCollectionView : ScrollView
{
    public ScrollCollectionView()
    {

    }
}
kekekeks commented 4 years ago

Please, decompile that class using ILSpy and/or monodis. NObservable shouldn't be injecting any static constructors, so it might be something else.