Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin.Forms support? #3

Open
Lelelo1 opened this issue Jun 4, 2019 · 5 comments
Open

Xamarin.Forms support? #3

Lelelo1 opened this issue Jun 4, 2019 · 5 comments

Comments

@Lelelo1
Copy link

Lelelo1 commented Jun 4, 2019

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

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

@kekekeks
Copy link
Owner

kekekeks commented Jun 4, 2019

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
Copy link
Author

Lelelo1 commented Jun 6, 2019

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
Copy link
Author

Lelelo1 commented Jan 24, 2020

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
Copy link
Author

Lelelo1 commented Jan 26, 2020

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants