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