-
Notifications
You must be signed in to change notification settings - Fork 167
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
ArgumentNullException error #3094
Comments
I'll give more details about this. I have my AvaloniaUI app using MVVM pattern, so i got 3 components, the View, the ViewModel and the Model Here is the View : <Grid RowDefinitions="Auto *">
<Button Content="Add" Command="{Binding AddPlayer}"/>
<ListBox Grid.Row="1" Items="{Binding Players}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:Player}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" />
<Label Content="{Binding Experience}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid> Here is the ViewModel : public class MainWindowViewModel : ReactiveObject
{
public ObservableCollection<Player> Players { get; } = new();
public void AddPlayer()
{
Players.Add(new Player { Name = "Unreal", Experience = 15.2 });
}
} And here is the Model : public partial class Player : IRealmObject
{
public string? Name { get; set; }
public double Experience { get; set; }
} I'm using Realm source generator so i implemented IRealmObject to my Model. Now if i remove the IRealmObject interface from my model, everything runs fine. Also, you can try by yourself with this Github Repo, just clone and run it. |
Thanks for the additional information! We'll investigate and get back to you
…On Mon, 14 Nov 2022, 14:15 Unreal, ***@***.***> wrote:
I'll give more details about this.
I have my AvaloniaUI <https://github.com/AvaloniaUI/Avalonia> app using
MVVM pattern, so i got 3 components, the *View*, the *ViewModel* and the
*Model*
Here is the *View* :
<Grid RowDefinitions="Auto *">
<Button Content="Add" Command="{Binding AddPlayer}"/>
<ListBox Grid.Row="1" Items="{Binding Players}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:Player}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" />
<Label Content="{Binding Experience}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Here is the *ViewModel* :
public class MainWindowViewModel : ReactiveObject
{
public ObservableCollection<Player> Players { get; } = new();
public void AddPlayer()
{
Players.Add(new Player { Name = "Unreal", Experience = 15.2 });
}
}
And here is the *Model* :
public partial class Player : IRealmObject
{
public string? Name { get; set; }
public double Experience { get; set; }
}
I'm using Realm source generator so i implemented *IRealmObject* to my
*Model*.
When i run this and then add a new *Player* instance to in my *ViewModel*
it throws System.ArgumentNullException: Value cannot be null. (Parameter
'source')
[image: realm_avalonia]
<https://user-images.githubusercontent.com/5130243/201681723-651b0011-9815-4dbd-a023-3b4b2c3a2057.gif>
Now if i remove the *IRealmObject* interface from my model, everything
runs fine.
[image: realm_avalonia_2]
<https://user-images.githubusercontent.com/5130243/201682170-5382fad1-e2c1-4f9e-a0d7-c885b354bf3e.gif>
Also, you can try by yourself with this Github Repo
<https://github.com/Unreal852/RealmAvalonia>, just clone and run it.
—
Reply to this email directly, view it on GitHub
<#3094 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABFADRZHBBLY7A5V7VP44YDWIJCP3ANCNFSM6AAAAAAR7UHJMI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
The same issue, if i go back to 10.17 and using RealObject instead of IReactObject is working good |
@Unreal852 and @goldindan I think I found the reason for the issue, and I'm working on a solution. I can also see that in your code you are using public class MainWindowViewModel : ReactiveObject
{
private Realm _realm;
public IQueryable<Player> Players { get; }
public MainWindowViewModel()
{
_realm = Realm.GetInstance();
Players = _realm.All<Player>();
}
public void AddPlayer()
{
_realm.Write(() =>
{
_realm.Add(new Player { Name = "Unreal", Experience = 15.2 });
});
}
} With this approach you don't need to worry about the Players = _realm.All<Player>().Where( p => p.Age > 25); This is the approach that we generally recommend developers, as it is the most idiomatic one, and developers don't need to worry about keeping collections updated. |
Hi, thanks for the quick fix. I didn't know we could bind directly on Realm collections this is nice. |
@Unreal852 just as an additional note, you can take look at our example project if you want to have some other examples of how to use bindings with Realm. The project uses Xamarin, but I think you can get the idea |
How can we fix the problem of binding an unmanaged IRealmObject to the view? |
@goldindan this will be fixed in the next version (the fix is in review in PR #3097) |
Discussed in #3093
Originally posted by Unreal852 November 13, 2022
Hello, i'm making an application with AvaloniaUI and i decided to use Realm as my database but when i bind my view to fields that belongs to an IRealmObject it throws ArgumentNullException. I created a separate project to replicate the issue https://github.com/Unreal852/RealmAvalonia
I did not create an issue because i don't know if i'm doing things wrong or not.
The text was updated successfully, but these errors were encountered: