C
C#2y ago
Laxman

✅ .NET MAUI System.Reflection.TargetInvocationException

Good evening, I hope you are doing well. I have the following problem, I would like to decrypt a string with Newtonsoft, however, when I try this in the Constructor my app crashes (error) but when I try it in a RelayCommand it works but the CollectionView doesn't update. The Code:
public SelectedOverviewViewModel()
{
CharactersList.Add(new CharacterModel
{
Name="Hi"
});

List<CharacterModel> characters = new List<CharacterModel>();
characters = JsonConvert.DeserializeObject<List<CharacterModel>>(JSONstring); // Crashes here

foreach (CharacterModel character in characters)
{
CharactersList.Add(character);
}
}

[RelayCommand]
void Load()
{
List<CharacterModel> characters = new List<CharacterModel>();
characters = JsonConvert.DeserializeObject<List<CharacterModel>>(JSONstring);

foreach (CharacterModel character in characters)
{
CharactersList.Add(character);
}
}
public SelectedOverviewViewModel()
{
CharactersList.Add(new CharacterModel
{
Name="Hi"
});

List<CharacterModel> characters = new List<CharacterModel>();
characters = JsonConvert.DeserializeObject<List<CharacterModel>>(JSONstring); // Crashes here

foreach (CharacterModel character in characters)
{
CharactersList.Add(character);
}
}

[RelayCommand]
void Load()
{
List<CharacterModel> characters = new List<CharacterModel>();
characters = JsonConvert.DeserializeObject<List<CharacterModel>>(JSONstring);

foreach (CharacterModel character in characters)
{
CharactersList.Add(character);
}
}
I´m using: CommunityToolkit.MAUI (latest) CommunityToolkit.MVVM (latest) Newton.Json (latest)
11 Replies
Buddy
Buddy2y ago
What error? - I have an issue, I get an error. Is basically what you're saying right now. Please include further information such as what the precise issue is including the error itself.
Laxman
LaxmanOP2y ago
System.Reflection.TargetInvocationException when my ViewModel hits the Constructor method in following line:
characters = JsonConvert.DeserializeObject<List<CharacterModel>>(JSONstring);
characters = JsonConvert.DeserializeObject<List<CharacterModel>>(JSONstring);
But when I use the line above in an RelayCommand, there is no error, and the string gets parsed correctly. I'm trying to figure out how to use the deserialize line on Startup.
Buddy
Buddy2y ago
And what is the JSON string? as well as the code of CharacterModel You can use this $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Laxman
LaxmanOP2y ago
BlazeBin - onvdfttbamfo
A tool for sharing your source code with the world!
Laxman
LaxmanOP2y ago
BlazeBin - onvdfttbamfo
A tool for sharing your source code with the world!
Laxman
LaxmanOP2y ago
BlazeBin - onvdfttbamfo
A tool for sharing your source code with the world!
Laxman
LaxmanOP2y ago
Thx for trying to helping me
Buddy
Buddy2y ago
Try to use System.Text.Json instead of Newtonsoft, it's also the standard today. and faster
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Laxman
LaxmanOP2y ago
Hey, thx for your help, a made a Work around, thx for the Tipp, thx for all !!!!!
Want results from more Discord servers?
Add your server