✅ Getting Button Command Parameter Avalonia
I have 4 buttons on my home screen
when I click the button, how do I get the
PlayGame()
function to get access to that CommandParameter
?28 Replies
The command parameter is an object passed to the reactive command, for your case, your command expects a
Unit
as a parameter and returns a Unit
(sort of like void)
An example of receiving a different parameter would be:
Setting the reactive command would be:
so it would need to be
?
one sec
yes
If you want to pass other object states from the UI side into the ReactiveCommand you can name your axaml elements
eg.
this is the HomeScreenViewModel. The reason I need access to the Command Parameter is so that I can return that and the selected difficulty. One second and I'll explain that
when I return that CommandParameter and the SelectedDifficulty back to the MainWindowViewModel, it can determine which game was selected to play, and it's difficulty level so tha the switch statement can launch the correct game screen. The functions that are undefinied will also have similar Obervable.Merge()'s which will route the user to the end game screen when they complete the game. At this moment, I'm just trying to get MWVM access to the CommandParameter of the button clicked, and the selected difficulty for the switch case to execute correctly and pass the difficulty and the username to the correct screen (which is a smaller second issue. How do I get access to the username that was selected from the SelectUserViewModel when it's done in a different function)?
The question is a bit different from the original and has a lot to unpack, for the original question, if you'd like to select a single parameter to pass into a reactive command you can name the control in the axaml side and pass that via
CommandParameter="{Binding #ControlName.WhateverPropertyYouNeed}"
if you'd like to send multiple parameters, avalonia has some documentation regarding MultiBindings and converters.
If you want to return an object from a dialog, then check out avalonia's InteractionContext, the documentation for this is sadly very limited to the barebones basics, though this tutorial of theirs actually shows a way better example on how to do thisI don't really know how to explain it correctly so I'm going to try my best
this is my game model
when a button is clicked,
this part is activated. The first thing is setting the GameType equal to the button that is clicked command parameter and then that information creates a new game in the database with all null values except the Difficulty and GameType
So you just want to pass the "Addition" as the parameter to the PlayCommand?
the PlayGame command yes
so that way it can write it to the database
Try this one,
that's not working either and it's becoming frustrating
Whats the newest error?
cannot implicitly conver type ReactiveUI.ReactiveCommand<string, System.Reactive.Unit> to ReactiveUI.ReactiveCommand<string, MathGame.Models.Game>
Where is this error thrown? Mind screenshotting it?
Did you change your
to be something else instead?
it's not a unit. it's a game model
public ReactiveCommand<string, Game> Play { get; }
I think I have myself backwards. I don't need to create a new blank game when the game is selected. I just need to return the command parameter of the button clicked and the selected difficulty back to the main window view model
so the difficulty the user selects from the combo box and the command parameter from the button clicked for the game type is what needs to be returnedYou should have told me you've changed things, if you want that, then do
so here's where I'm at and I'm not getting any errors
I do apologize. I'm sorry. I just had a moment to think about what I was trying to do and then made the change.
let me know if u need any more assistance
so I've started the GameEnginView and GameEngineViewModel files. I now need these to launch correctly. My first problem is where I call PlaySelectedGame in the SelectUserViewModel. It's saying
Cannot implicitly conver type 'void' to MathGame.ViewModels.ViewModelBase
. All I know is that if I change it to
it works fine, but I need it to call the function that handles the view model instead of how the bottom code snippet shows
what I mean is that when the MainWIndowViewModel loads, it calls the CreateNewUser()
function and works fine, but when I use the CreateNewUser()
function to call PlaySelectedGame()
to show the home screen and get the values back that I need for screen navigation, it gives that errorIt might be that it expects a void and instead gets a game?
from:
Play = ReactiveCommand.Create<string, Game>(gameType => PlayGame(gameType),selectionOk);
<:zerotwo_shrug:841954087238107186>
i've never had game logic intertwined in ui logic
so its a bit out of my area of expertisein my SelectUserViewModel it returns a
<Unit, User>
instead of like the HomeScreenViewModel which returns a <string, Game>
and I don't have any game logic yet. This is all just getting information and returning it back to the main window view model and changing screens based off that information
uh, i got no idea sorry
it's all good 🙂
thank you for your help so far ❤️
I was able to get it resolved. I wasn't supposed to set it to
Content = ...
but instead just call the function itself as the view has the ViewModel content already instantiated within it.