✅ implementing "Screens" in avalonia?

so i'm building a small app in Avalonia, I want to have a login screen that appears before the "main" screen i.e require a login before is there a built-in/preferred way to implement something like this? poking around the code I see desktop.MainWindow being set I'm guessing that it involves manipulating that to some extent? or can you do something like in HTML where you can set display:none on elements and then write code to manipulate that
8 Replies
Jimmacle
Jimmacle3d ago
you could use something like a ContentControl and change the content it's bound to, then assuming you have your datatemplates/viewlocator set up right it will create the right view for the viewmodel
enrico11011
enrico11011OP3d ago
i see, single main window and then change the contents out? ok that's worth a shot so i'm actually trying it right now, I've got a MainWindow with a TransitioningContentControl on it, the "Content" attribute is binding to a CurrentPage property on the MainWindowViewModel, and CurrentPage is set to an instance of LoginViewModel. it works, it displays and everything but I don't know how to pass data between the views - i'd need to trigger the MainWindow somehow to switch the view when someone successfully logs in but I can't wrap my head around how to do that could I just pass a reference to MainWindow in the constructor to LoginView? or does that violate mvvm?
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
The way I do it is by having a direct reference to the viewmodel when navigating which violates MVVM but idrc. Though if I were to do it properly, you would have a navigation class that handles the current page and maybe any previous pages. You would then have a function like NavigateTo(string page, object? data = null); where you can pass objects into the viewmodel which should inherit a base viewmodel that the navigator can handle.
enrico11011
enrico11011OP3d ago
yeah i'm passing around a reference to the MainWindowViewModel atm a navigation class does sound like a better idea I"m guessing it'll expose a NavClass.CurrentPage field that gets referenced by the main view?
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
I would suggest to use an event which the main view model can subscribe to. public event Action<ViewModelBase>? OnViewModelChanged;
enrico11011
enrico11011OP3d ago
Even better thanks Its the nav class that sends the event right? Just want to make sure i havent done any custom event work before
enrico11011
enrico11011OP2d ago
awesome, i got it working thanks 🙂

Did you find this page helpful?