✅ Issues with Avalonia

So im new to Avalonia and trying to make my first real program with it now as i want to adapt my old project to go from WinUI 3 to be written in Avalonia Im current testing around with MVVM as im also very new to that but i have some issues with it. In my example i want to render a button just to see if my ViewModel is even being bound, the Binding can perfectly recognize my variable but the screen that is rendering remains black, im also gonna post the app.axaml.cs code since this might also cause the issue if the page isnt being set properly
No description
No description
No description
66 Replies
leowest
leowest5w ago
what u expect to happen and what is happening instead? U would also need to post the shell xaml and more of the login xaml
Somgör from Human Resources
what i expect is the login screen showing (which is curently just a button thats disabled as set in the LoginViewModel)
Somgör from Human Resources
you mean app.axaml?
leowest
leowest5w ago
no the main window xaml
leowest
leowest5w ago
can u show me a screenshot of the solution explorer might be easier to tell u which files I need
leowest
leowest5w ago
so yea MainView xaml and MainWindow
leowest
leowest5w ago
👆
Somgör from Human Resources
ah i switched them up my bad
leowest
leowest5w ago
so yeah as it stands u have MainWindow showing Mainview and MainWIndow having LoginViewModel as its datacontext I dont see why it would show your login page this is the break down MainWindow is a shell as u can see in the image above
leowest
leowest5w ago
it has <views:MainView /> that is what makes the MainView UserControl show up inside MainWindow
leowest
leowest5w ago
and this is what binds the MainViewModel to MainView
No description
leowest
leowest5w ago
does that make sense
Somgör from Human Resources
what i essentially want to achieve is switch up the entire viewmodel with a different page if needed :SCgetoutofmyhead:
leowest
leowest5w ago
ok I guess it did not made sense
Somgör from Human Resources
yes but it says its only for the designer
leowest
leowest5w ago
what u need to do is essentially the samething MainView is doing u need to attach the datacontext to your Login usercontrol and add the control to your MainWindow
Somgör from Human Resources
you mean like
<views:MainView />
<views:LoginView />
<views:MainView />
<views:LoginView />
?
leowest
leowest5w ago
yep amd remove MainView
Somgör from Human Resources
but what if i need MainView to be the first page if for example the user is already logged in LoginView is the fallback if no account is present
leowest
leowest5w ago
then u would need to write code that will test if user is logged in or not and render the paged based on that using a ContentControl
Somgör from Human Resources
ive written that code before so i can just copy that but how would i be able to do this on the startup process last time on WinUI i put it into the app.cs thingie and then pass the fitting page to the current window
leowest
leowest5w ago
well I would do it on the MainWIndow(aka your shell) besides u would need a vm to control navigation anyway its just a matter of having a <ContentControl Content="{Binding CurrentPage}" /> and have that CurrentPage be set
Somgör from Human Resources
so you mean on the codebehind of MainWindow?
leowest
leowest5w ago
based on whether the user is or not logged in no I have in the MainWindow, VM I usually call it Shell.axaml ShellViewModel.cs
Somgör from Human Resources
so i need to add a <ContentControl> to the MainWindow that essentialls decides which view is currently the current page?
leowest
leowest5w ago
or u could just use the Window Content if u wont have anything else in it
Somgör from Human Resources
i tried adding this but still getting a blacksreen
No description
No description
Somgör from Human Resources
also CurrentPage is valid
No description
Somgör from Human Resources
:SCcrying:
leowest
leowest5w ago
to use viewmodelbase u would need to have a viewlocator ContentControl Content and Window Content usually take a control if u want it to take a viewmodelbase then you need to write a converter which is usually called viewlocator avalonia have an example of it in their docs https://docs.avaloniaui.net/docs/concepts/view-locator anyway I g2g for now
Somgör from Human Resources
i dont understand this at all :SCgetoutofmyhead:
leowest
leowest5w ago
without using a viewlocator u would have to define templates for example
<Window.DataTemplates>
<DataTemplate DataType="vm:MainViewModel">
<views:MainView />
</DataTemplate>
<DataTemplate DataType="vm:LoginViewModel">
<views:LoginView />
</DataTemplate>
</Window.DataTemplates>

<ContentControl Content="{Binding CurrentPage}" />
<Window.DataTemplates>
<DataTemplate DataType="vm:MainViewModel">
<views:MainView />
</DataTemplate>
<DataTemplate DataType="vm:LoginViewModel">
<views:LoginView />
</DataTemplate>
</Window.DataTemplates>

<ContentControl Content="{Binding CurrentPage}" />
then u can have say
[ObservableProperty]
private ViewModelBase _currentPage = new LoginViewModel();
[ObservableProperty]
private ViewModelBase _currentPage = new LoginViewModel();
and it would initially open the login page
Somgör from Human Resources
im trying to follow the todo list tutorial to understand Avalonia more but the tutorial doesnt wok :stare: it cant find my binding even tho it clearly gets recognized
Somgör from Human Resources
it also cant find InitializeComponent on the codebehind of my view so idk whats going on with that
leowest
leowest5w ago
do u have a github of your code
Somgör from Human Resources
no this is fixed but no matter what i do it tries to get ListItems from MainViewModel which isnt even the class its in so why does it get recognized by my IDE in the first place
leowest
leowest5w ago
well if u can upload your code to github I can take a look at it
Somgör from Human Resources
GitHub
GitHub - FabioGaming/TodoTutorial
Contribute to FabioGaming/TodoTutorial development by creating an account on GitHub.
leowest
leowest5w ago
which view todolistview?
leowest
leowest5w ago
yeah you never set the todolistview to its vm that's why
Somgör from Human Resources
how would i do that
Somgör from Human Resources
isnt that what these 2 do?
No description
leowest
leowest5w ago
no just only tell what type
Somgör from Human Resources
:SCcrying:
leowest
leowest5w ago
what tutorial are u following?
Somgör from Human Resources
To Do List App | Avalonia Docs
In this tutorial you will create a simple to do list application using Avalonia UI and the Model View View-Model (MVVM) pattern with binding to the to do list (collection) data.
Somgör from Human Resources
this page more specifically
leowest
leowest5w ago
it even says at the end
No description
leowest
leowest5w ago
:when:
Somgör from Human Resources
:SCgetoutofmyhead: alright then im stupid thanks tho
leowest
leowest5w ago
so the next step is to wire the datacontext which is what plugs it all together
leowest
leowest5w ago
👍
Somgör from Human Resources
much appreciated :soPortuguese: