✅ 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
66 Replies
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
what i expect is the login screen showing (which is curently just a button thats disabled as set in the LoginViewModel)
you mean app.axaml?
no the main window xaml
ah
can u show me a screenshot of the solution explorer
might be easier to tell u which files I need
so yea MainView xaml
and MainWindow
👆
ah i switched them up
my bad
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
yes
it has <views:MainView />
that is what makes the MainView UserControl show up inside MainWindow
and this is what binds the MainViewModel to MainView
does that make sense
what i essentially want to achieve is switch up the entire viewmodel with a different page if needed :SCgetoutofmyhead:
ok I guess it did not made sense
yes but it says its only for the designer
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
you mean like
?
yep amd remove MainView
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
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
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
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
so you mean on the codebehind of MainWindow?
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
so i need to add a
<ContentControl>
to the MainWindow that essentialls decides which view is currently the current page?or u could just use the Window Content
if u wont have anything else in it
i tried adding this but still getting a blacksreen
also CurrentPage is valid
:SCcrying:
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
i dont understand this at all :SCgetoutofmyhead:
without using a viewlocator u would have to define templates
for example
then u can have say
and it would initially open the login page
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
it also cant find InitializeComponent on the codebehind of my view so idk whats going on with that
do u have a github of your code
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
well if u can upload your code to github I can take a look at it
GitHub
GitHub - FabioGaming/TodoTutorial
Contribute to FabioGaming/TodoTutorial development by creating an account on GitHub.
which view
todolistview?
yes
yeah you never set the todolistview to its vm
that's why
how would i do that
isnt that what these 2 do?
no just only tell what type
:SCcrying:
what tutorial are u following?
here
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.
this page more specifically
Data Binding | Avalonia Docs
TUTORIALS - To Do List App
:SCchaos:
it even says at the end
:when:
:SCgetoutofmyhead:
alright then im stupid
thanks tho
so the next step is to wire the datacontext
which is what plugs it all together
ye
👍
much appreciated
:soPortuguese: