reeeeeee
reeeeeee
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
So.. what do you think about this? 😅
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
I would be a lot less messy if I could have only type of objects (but I will need more of them because I need some additional formatting before saving value to object - for exampe: string can only include numbers) Yeah, I know that reflection is usually better to be avoided, but this is currently the best idea I got, haha
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
and loading is like this:
public async Task GetSettings() {
var fileContent = await File.ReadAllTextAsync("config.json");
var fds = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(fileContent);
foreach (var property in this.GetType().GetProperties().Where(x => typeof(CanvasPosition).IsAssignableFrom(x.PropertyType) && x.CanWrite))
{
if (fds.ContainsKey(property.Name) && typeof(CanvasPosition).IsAssignableFrom(property.PropertyType))
{
var jsonValue = fds[property.Name];
var value = JsonSerializer.Deserialize(jsonValue.GetRawText(), property.PropertyType);
property.SetValue(this, value);
}
}
}
public async Task GetSettings() {
var fileContent = await File.ReadAllTextAsync("config.json");
var fds = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(fileContent);
foreach (var property in this.GetType().GetProperties().Where(x => typeof(CanvasPosition).IsAssignableFrom(x.PropertyType) && x.CanWrite))
{
if (fds.ContainsKey(property.Name) && typeof(CanvasPosition).IsAssignableFrom(property.PropertyType))
{
var jsonValue = fds[property.Name];
var value = JsonSerializer.Deserialize(jsonValue.GetRawText(), property.PropertyType);
property.SetValue(this, value);
}
}
}
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
Okay, so I managed to do some "improvements", which include less hardcoding stuff. I have two models, CanvasPosition and StringCanvasPosition (this one inherits canvas position - some elements requires string, some int, so I just separated them.
public class CanvasPosition
{
public double? CanvasLeft { get; set; }

public double? CanvasTop { get; set; }
public string BindableName { get; set; }
}


public class StringCanvasPosition : CanvasPosition
{
public string TextValue { get; set; }
}
public class CanvasPosition
{
public double? CanvasLeft { get; set; }

public double? CanvasTop { get; set; }
public string BindableName { get; set; }
}


public class StringCanvasPosition : CanvasPosition
{
public string TextValue { get; set; }
}
Then I have bindable properties like this:
private StringCanvasPosition _receivedRequestsWrapper { get; set; }
public StringCanvasPosition ReceivedRequestsWrapper ....getter, setter...
private StringCanvasPosition _receivedRequestsWrapper { get; set; }
public StringCanvasPosition ReceivedRequestsWrapper ....getter, setter...
For the serialization and deserialization I used reflection. I am saving it to File and the code looks like this: Saving:
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (var property in this.GetType().GetProperties().Where(x => typeof(CanvasPosition).IsAssignableFrom(x.PropertyType)))
{
dict[property.Name] = property.GetValue(this);
}
var options = new JsonSerializerOptions
{
WriteIndented = true
};
var jsonString = JsonSerializer.Serialize(dict,options); ;
File.WriteAllText(file, jsonString);
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (var property in this.GetType().GetProperties().Where(x => typeof(CanvasPosition).IsAssignableFrom(x.PropertyType)))
{
dict[property.Name] = property.GetValue(this);
}
var options = new JsonSerializerOptions
{
WriteIndented = true
};
var jsonString = JsonSerializer.Serialize(dict,options); ;
File.WriteAllText(file, jsonString);
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
oh, when I was typing, this thought occured me,actually it might even work, will test it tommorow morning. If you are OK with that, can I ask you some questions if they will be needed?
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
Ahh, just when I thought I already have a solution.. looks like I don't. One more question and then I'll let you go..
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
Okay yeah, I had started with something similar and managed to make it work, which is nice. Thank you! (I should look into stuff like "sealed record", "abstract", "immutableDictionary") Haha ye, every new way of storing data ends totally ugly... well, the above way with hardcoding every property kinda works, maybe some better idea will pop in my mind anytime soon.. It technically works, except some properties have margin set in XAML and on fresh set of X and Y form file, their margin stays there and its location is actually wrong. An easy "fix" would be get rid of margins, but yea 😅
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
i really have no idea how could i improve that and not make a mess, hah
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
Also, if I would like to store them into a json file and do loading from there like this
SpecialBindableObject MyTextBox1;
SpecialBindableObject MyTextBox2;
SpecialBindableObject Label1;
...
Dict<stirng, SpecialBindableObject> savedObjects = deserializeJsonContent from file
and then for each property something like:
MyTextBox1 = savedObjects["MyTextBox1"];
MyTextBox2= savedObjects["MyTextBox2"];
Label1= savedObjects["Label1"];
SpecialBindableObject MyTextBox1;
SpecialBindableObject MyTextBox2;
SpecialBindableObject Label1;
...
Dict<stirng, SpecialBindableObject> savedObjects = deserializeJsonContent from file
and then for each property something like:
MyTextBox1 = savedObjects["MyTextBox1"];
MyTextBox2= savedObjects["MyTextBox2"];
Label1= savedObjects["Label1"];
How ungly is that? 😅
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
or did you mean something else?
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
And instead of binding 3 different variable, I can use only one, and do binding in xaml likeSpecialObject.PositionX, SpecialObject.PositionY, etc?
18 replies
CC#
Created by reeeeeee on 10/19/2024 in #help
Saving Draggable objects location wpf
So some kind of SpecialBindableObejct with at least 3 properties (value, PositionX, PositionY) and INotifyPropertyChanged
18 replies
CC#
Created by reeeeeee on 10/18/2024 in #help
When to use Dependency injection?
yeag i have to use Microsoft.Extensions.DependencyInjection; so i guess its fine, becausei ts not some kind of 3rd party library
5 replies
CC#
Created by reeeeeee on 10/17/2024 in #help
How do you implement paging on API requests?
So all service methods shoudl be something like this, or is it any easier/better way?
public async Task<List<BlogPosts>> GetBlogposts(int? page, int? pageSize)
{
if (page.HasValue && pageSize.HasValue)
{
await context.Blogposts.OrderBy(p => p.ReleaseDate).Paginate(page, 20).ToListAsync();
}
await context.Blogposts.OrderBy(p => p.ReleaseDate).ToListAsync();
}
public async Task<List<BlogPosts>> GetBlogposts(int? page, int? pageSize)
{
if (page.HasValue && pageSize.HasValue)
{
await context.Blogposts.OrderBy(p => p.ReleaseDate).Paginate(page, 20).ToListAsync();
}
await context.Blogposts.OrderBy(p => p.ReleaseDate).ToListAsync();
}
11 replies
CC#
Created by reeeeeee on 10/16/2024 in #help
WPF Design patterns
ah okay. So this multilayer architecture is more of a web application thing? (or you dont split app there as well?)
6 replies
CC#
Created by reeeeeee on 10/8/2024 in #help
Multilayer architecture - explanation?
anyway thanks for your help 🙂
23 replies
CC#
Created by reeeeeee on 10/8/2024 in #help
Multilayer architecture - explanation?
i previously watched some tutories/guides which connected Controller - Service - repository - context, and I just could figure it out why would you pass the stuff so many times
23 replies
CC#
Created by reeeeeee on 10/8/2024 in #help
Multilayer architecture - explanation?
yeye ofc 🙂
23 replies
CC#
Created by reeeeeee on 10/8/2024 in #help
Multilayer architecture - explanation?
Aha, ok. so User service would have something like this: private DbContext _myDbContext
GetUserByName(stirng name) => _myDbContext.Users.FirstOrDefault(x => x.Name == name);
GetUserByName(stirng name) => _myDbContext.Users.FirstOrDefault(x => x.Name == name);
23 replies
CC#
Created by reeeeeee on 10/8/2024 in #help
Multilayer architecture - explanation?
yeah i always have some doubts about naming properly, lol
23 replies