C
C#3mo ago
naber top

Avalonia Beginner

should I design my app in MainWindow.axaml or MainView.axaml?
36 Replies
naber top
naber top3mo ago
The document tells me to put my stuff in MainWindow but I recieve errors doing that.
SpReeD
SpReeD3mo ago
Take a look at the App.axaml.cs:
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
singleViewPlatform.MainView = new MainView
{
DataContext = new MainViewModel()
};
}
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
singleViewPlatform.MainView = new MainView
{
DataContext = new MainViewModel()
};
}
It should be clear what Window is used for. Maybe you start without avalonia targeting everything and concentrate on desktop dev first. It should be way easier for a beginner.
naber top
naber top3mo ago
What should I use WPF? Oh I see your point
SpReeD
SpReeD3mo ago
WPF is for Windows only
naber top
naber top3mo ago
So I can stick with avalonia? But only focus on desktop?
SpReeD
SpReeD3mo ago
Yes, depending on your goal - but for test purposes and would suggest using MVVM App targeting desktop dev, without browser/web assembly and mobile Since you mentioned WPF which is strongly bound to the MVVM pattern.
naber top
naber top3mo ago
I should proceed with WPF then switch to Avalonia?
SpReeD
SpReeD3mo ago
Are familiar with the MVVM pattern?
naber top
naber top3mo ago
Not at all.
SpReeD
SpReeD3mo ago
Are you familiar with MVC or MVP?
naber top
naber top3mo ago
No I am not familiar with any type of model patterns
SpReeD
SpReeD3mo ago
Do you wanna make a GUI program to run on windows?
naber top
naber top3mo ago
Yes
SpReeD
SpReeD3mo ago
Start with WinForms, it's event-driven and definitely easier to work and understand than WPF, Avalonia, Xamarin, Maui, etc.
naber top
naber top3mo ago
I got roasted using WinForms cause I asked a question which requires a MVVM approach Then they told me to learn WPF I am familiar with the xaml but not MVVM
SpReeD
SpReeD3mo ago
WinForms can be used with the MVVM pattern, somehow, but it's not designed to do so; WPF was designed to use it. So, for beginners, I strongly recommend starting with something that doesn't need to be build in a strict pattern. A WinForms application isn't outdated or anything, WinForms is a good, quick and still up-to-date way to design applications which concentrates on delivering results and not having fancy stylings and animations, etc.
naber top
naber top3mo ago
So why WinForms make it easier for me to learn MVVM if it isn't going to force me to use it or designed for it?
SpReeD
SpReeD3mo ago
It doesn't, it will make it easier for you to eventually have a working application
naber top
naber top3mo ago
I am somewhat experienced with WinForms but the app I am trying to make needs to use MVVM I guess
SpReeD
SpReeD3mo ago
Once you get familiar with OOP and everything related, like encapsulation, you start to learn a design-pattern, like MVVM. But the basic for any design-pattern is working in OOP rules.
naber top
naber top3mo ago
Oh thats good advice So how can I practice my OOP usage
SpReeD
SpReeD3mo ago
Why do you think it needs MVVM? - It's just a design-pattern and won't make any difference on the result of that application.
naber top
naber top3mo ago
My application has a main window and a bunch of popup windows that has their own pages, own functions which mangles with the main windows properties, since I don't know MVVM it's not my thought I asked here and they told me to go this path.
SpReeD
SpReeD3mo ago
Just my 2 cents here, make it work first, refactor later. A design-pattern is something you learn later on; first you need to get the hang of OOP. Of what you describe, WinForms is totally capable of doing so.
Kouhai
Kouhai3mo ago
I would strongly advise against using Winforms if you're not forced to use it, Avalonia is much better
naber top
naber top3mo ago
Yeah I understand your point but how do I get the hang of OOP, the apps I coded to practice my c# knowledge didn't require OOP is Avalonia harder than WPF?
Kouhai
Kouhai3mo ago
Not at all
naber top
naber top3mo ago
But just like Spreed said I don't know OOP well and Avalonia has a MVVM structure
SpReeD
SpReeD3mo ago
It is, it is for someone without any knowledge of object-oriented programming ...
Kouhai
Kouhai3mo ago
In that case I would avoid GUI applications and just build console apps, GUI adds another layer of complexity that makes it harder to learn core concepts. Well, you'd usually use MVVM with WPF as well 😅
naber top
naber top3mo ago
At which point should I decide to go GUI
Kouhai
Kouhai3mo ago
When you have a general understanding of core C# concepts, like inheritance, interfaces, Generics, LINQ You don't have to be an expert at all, but having an understanding of core concepts would make building GUI apps much easier
naber top
naber top3mo ago
Well how do I get that understanding cause normally I try to create a project using my knowledge, even though I know a bit of OOP I couldn't think of a project I would use it,(or it gets too complicated for me) what should my approach be in this kind of situations?
Kouhai
Kouhai3mo ago
Applying what you've learned by building projects is essential and you should keep doing that, but also reading official docs helps a lot of times. Now for how to approach that, if you have a project in mind, try to make it in a console application for now (because GUI adds a lot of complexity) For example if you're building a clinic app, usually you'd have a GUI, but in fact you can use a terminal UI as simple as
1. Add patient information
2. Modify patient information
3. View patient data
4. Delete patient data
5. Reports
1. Add patient information
2. Modify patient information
3. View patient data
4. Delete patient data
5. Reports
naber top
naber top3mo ago
thanks
Kouhai
Kouhai3mo ago
That doesn't mean you should keep building console apps, but until you personally feel comfortable about general concepts I would say console apps are better. And if you hit an obstacle you can always ask here in this server 👍