DapperDeer
DapperDeer
CC#
Created by DapperDeer on 12/27/2023 in #help
Dependency Properties and When to use ObservableCollections
Hello, I'm trying to figure out how best to architect my little Pokedex application that I'm making and would appreciate some guidance/assistance. In my MainWindow code-behind, it's getting a service that pulls all the Pokemon information from an API that I'm calling and storing it in an ObservableCollection, which the PokemonDetails is binding to. In the PokemonDetails code-behind, I have a Dependency Property but something about this binding is not working. I don't think I'm doing the Dependency Property correctly, and I'm not sure if using an ObservableCollection in my MainWindow.xaml.cs is necessary. My thought process was that the collection will be updated, notify the PropertyChanged event (which I don't actually subscribe it to anything, maybe that's the issue?), which would notify the PokemonDetails UserControl, update its dep property, and display the list correctly. Code is in replies to this thread.
6 replies
CC#
Created by DapperDeer on 12/19/2023 in #help
EntityFramework Property Binding
I have class Pokemon:
[JsonPropertyName("Name")]
public string Name { get; set; }

[Key]
[JsonPropertyName("Pokedex Number")]
public int PokedexNumber { get; set; }

[ForeignKey(nameof(PokeType.Id))]
[JsonPropertyName("Type")]
public PokeType Type { get; set; }
[JsonPropertyName("Name")]
public string Name { get; set; }

[Key]
[JsonPropertyName("Pokedex Number")]
public int PokedexNumber { get; set; }

[ForeignKey(nameof(PokeType.Id))]
[JsonPropertyName("Type")]
public PokeType Type { get; set; }
Where PokeType is:
[Key]
public int Id { get; set; }
[JsonPropertyName("TypeSlotOne")]
public Types SlotOne { get; set; } = Types.None;
[JsonPropertyName("TypeSlotTwo")]
public Types SlotTwo { get; set; } = Types.None;
[Key]
public int Id { get; set; }
[JsonPropertyName("TypeSlotOne")]
public Types SlotOne { get; set; } = Types.None;
[JsonPropertyName("TypeSlotTwo")]
public Types SlotTwo { get; set; } = Types.None;
and I am getting a list of Pokemon from an external API, converting them to my own classes, and storing them in a Sqlite db where my DbContext is as follows:
public DbSet<Pokemon> Pokemon { get; set; }
public DbSet<PokeType> PokeTypes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PokeType>()
.Property(e => e.SlotOne)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

modelBuilder.Entity<PokeType>()
.Property(e => e.SlotTwo)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

base.OnModelCreating(modelBuilder);
}
public DbSet<Pokemon> Pokemon { get; set; }
public DbSet<PokeType> PokeTypes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PokeType>()
.Property(e => e.SlotOne)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

modelBuilder.Entity<PokeType>()
.Property(e => e.SlotTwo)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

base.OnModelCreating(modelBuilder);
}
When I am accessing the cached data, my Pokemon aren't filling in their Types and I don't know how to map the properties. EF has also made Pokemon and PokeType into two separate tables, which doesn't necessarily seem like a major issue, I just don't know how to workaround it. Some StackOverflow answers I've seen that may help have a .Map method that I don't seem to have - maybe I'm missing a dependency?
8 replies
CC#
Created by DapperDeer on 11/17/2022 in #help
❔ Single UDP Broadcaster to Multiple Clients
I'm trying to have a single UDP Broadcast to many clients. This is what I have working so far. Ideally, I'd like to be able to spin up multiple instances of the Listener and have them receive the message. That is, my Broadcaster sends messages, a single listener can connect and receive, but any additional listeners throws an exception. Broadcaster:
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
... // encodedMessage is created here
while (true)
{
Console.WriteLine($"Broadcasting encoded message:\n {JsonSerializer.Serialize(broadcastMessage)}");
udpClient.Send(encodedMessage, ep);
}
}
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
... // encodedMessage is created here
while (true)
{
Console.WriteLine($"Broadcasting encoded message:\n {JsonSerializer.Serialize(broadcastMessage)}");
udpClient.Send(encodedMessage, ep);
}
}
Listener:
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
this._udpClient.Client.Bind(ep);
while (true)
{
var message = udpClient.Receive(ref ep);
Console.WriteLine($"Received: {Encoding.Utf8.GetString(message)}");
}
}
using (var udpClient = new UdpClient { ExclusiveAddressUse = false, EnableBroadcast = true})
{
var ep = new IPEndPoint(IPAddress.Parse(this._options.BroadcastAddress), this._options.BroadcastPort);
this._udpClient.Client.Bind(ep);
while (true)
{
var message = udpClient.Receive(ref ep);
Console.WriteLine($"Received: {Encoding.Utf8.GetString(message)}");
}
}
5 replies