C
C#16mo ago
Mekasu0124

✅ how to pass username?

in my HomeScreenView.axaml file I have
<TextBox Text="{Binding PlayerUserName}" />
<Button Command="{Binding $parent[Window].DataContext.NewGame}" />
<TextBox Text="{Binding PlayerUserName}" />
<Button Command="{Binding $parent[Window].DataContext.NewGame}" />
When a player clicks the New Game button, how can I pass the username back to the parent file MainWindowViewModel.cs
// MainWindowViewModel.cs
namespace Phasmo_Trivia.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
ViewModelBase _content;

public MainWindowViewModel(Database db)
{
Content = List = new HomeScreenViewModel(db);
}

public ViewModelBase Content
{
get => _content;
private set => this.RaiseAndSetIfChanged(ref _content, value);
}

public HomeScreenViewModel List { get; }

public void NewGame()
{
var vm = NewGameViewModel();
}
public void PreviousGames()
{

}
}
}
// MainWindowViewModel.cs
namespace Phasmo_Trivia.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
ViewModelBase _content;

public MainWindowViewModel(Database db)
{
Content = List = new HomeScreenViewModel(db);
}

public ViewModelBase Content
{
get => _content;
private set => this.RaiseAndSetIfChanged(ref _content, value);
}

public HomeScreenViewModel List { get; }

public void NewGame()
{
var vm = NewGameViewModel();
}
public void PreviousGames()
{

}
}
}
I need to pass this username to the NewGameViewModel(); so that as the player plays the game, it can store the username along with the game results.
4 Replies
Mekasu0124
Mekasu0124OP16mo ago
would the simple solution be to pass it as a command paremeter through the button?
Anchy
Anchy16mo ago
it doesnt sound like that should be the responsibility of the root viewmodel
Mekasu0124
Mekasu0124OP16mo ago
ok. I moved the new game and previous games functions to the HomeScreenViewModel file, and when I run my program, I get this error message merged in with my textbox. I'm not sure how to fix this, honestly.
Mekasu0124
Mekasu0124OP16mo ago
I'm trying to build it like I did my diary app. I was told that the MainWindow.xxxx files are supposed to hand the screen changes, so I'm trying to model after that I think I've done this wrong, tbh. I'm going to start over. so since the json file will only be used to add new games (when they end) and show previous games when that button is clicked on the home screen, do I need to pass the database through the app.axaml.cs file through to MainWindowViewModel? https://github.com/mekasu0124/PhasmoTrivia
Want results from more Discord servers?
Add your server