Jochem
Jochem
CC#
Created by Jochem on 6/30/2023 in #help
✅ MAUI UI won't update after calling OnPropertyChanged from Command
Hey there, I am working on a MAUI application and came across a problem where the UI won't update after calling OnPropertyChanged I know for sure that function gets called because I tested that using a debugger and break points. I made a small test project that displays three buttons. Whenever a button is pressed, the color of the button should change from red to green. However this does not happen. I just can't seem to figure out why :(. I've pasted the code for my view model and view below.
4 replies
CC#
Created by Jochem on 6/23/2023 in #help
❔ MAUI Error when loading resources on loading page
I have a view that displays the Cards property as a CollectionView. The view model is populating the hand data member inside a asynchronous function. The problem is that with the code below, sometimes my program will crash on startup and sometimes it will not. When commenting out the single statement in my constructor, the program won't crash. I think there's something wrong with the way I load my resources. Is there a better approach for loading resources that are needed on a page load (using MVVM)?
public class GameViewModel : IGameViewModel
{
...

private readonly List<Card> hand = new();
public IReadOnlyCollection<Card> Hand => hand.AsReadOnly();

public GameViewModel(DeckOfCardsService service)
{
Task.Run(() => Task.FromResult(Initialize(service))).Wait();
}

private async Task Initialize(DeckOfCardsService service)
{
var deck = await service.GetDeckAsync(); // Makes a REST API request.

for (int i = 0; i < 5; ++i)
{
hand.Add(deck.Draw());
}

OnPropertyChanged("Hand");
}
}
public class GameViewModel : IGameViewModel
{
...

private readonly List<Card> hand = new();
public IReadOnlyCollection<Card> Hand => hand.AsReadOnly();

public GameViewModel(DeckOfCardsService service)
{
Task.Run(() => Task.FromResult(Initialize(service))).Wait();
}

private async Task Initialize(DeckOfCardsService service)
{
var deck = await service.GetDeckAsync(); // Makes a REST API request.

for (int i = 0; i < 5; ++i)
{
hand.Add(deck.Draw());
}

OnPropertyChanged("Hand");
}
}
The location where my IDE points me to when stopping the program.
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
2 replies
CC#
Created by Jochem on 6/17/2023 in #help
❔ HTTP request object creation / ignoring fields when deserializing JSON
Hey there. I am writing a program using the Deck of Cards API which can be found here: https://deckofcardsapi.com/. The API returns a response which contains JSON.
class Deck
{
public Deck() { ... }
public Card DrawCard() { ... }
}
class Deck
{
public Deck() { ... }
public Card DrawCard() { ... }
}
I have a class named "Deck", I first wanted "Deck" to load and deserialize itself in the constructor using the API but I don't really know if that's possible or a good idea. I am wondering if there are better alternatives or best practices for this problem.
class Card
{
public string Value;
public string Suit;
public string Image;
}
class Card
{
public string Value;
public string Suit;
public string Image;
}
I also have a second problem. Whenever I want to request a card using the API, the response contains many properties I don't need. Is there a way of only deserializing certain properties? Above you can see a class called "Card" and I only need "Value", "Suit", and "Image" but the JSON in the response contains much more as you can see below. Response when drawing card(s).
{
"success": true,
"deck_id": "kxozasf3edqu",
"cards": [
{
"code": "6H",
"image": "https://deckofcardsapi.com/static/img/6H.png",
"images": {
"svg": "https://deckofcardsapi.com/static/img/6H.svg",
"png": "https://deckofcardsapi.com/static/img/6H.png"
},
"value": "6",
"suit": "HEARTS"
},
{
"code": "5S",
"image": "https://deckofcardsapi.com/static/img/5S.png",
"images": {
"svg": "https://deckofcardsapi.com/static/img/5S.svg",
"png": "https://deckofcardsapi.com/static/img/5S.png"
},
"value": "5",
"suit": "SPADES"
}
],
"remaining": 50
}
{
"success": true,
"deck_id": "kxozasf3edqu",
"cards": [
{
"code": "6H",
"image": "https://deckofcardsapi.com/static/img/6H.png",
"images": {
"svg": "https://deckofcardsapi.com/static/img/6H.svg",
"png": "https://deckofcardsapi.com/static/img/6H.png"
},
"value": "6",
"suit": "HEARTS"
},
{
"code": "5S",
"image": "https://deckofcardsapi.com/static/img/5S.png",
"images": {
"svg": "https://deckofcardsapi.com/static/img/5S.svg",
"png": "https://deckofcardsapi.com/static/img/5S.png"
},
"value": "5",
"suit": "SPADES"
}
],
"remaining": 50
}
31 replies