MrME
MrME

Reputation: 337

Monogame XAML readout textbox inside gameloop

Heey,

I'm currently working on my second XNA/Monogame game for Windows 8/Metro but ran into a problem. We now came at the point which we need to store a highscore with a name attached to it so I need to handle the onscreen keyboard to get the info.

I searched through the forum and I found some topics related to this but no post with some example code or a description which helped me completely fixing my problem. I changed my project to a XAML template and I got a TextBox working in my GamePage but now I need to get the TextBox inside my game loop to read it out so I can save the name besides my score and I have currently no idea how to do this.

My current code of my GamePage.cs

    public GamePage(string launchArguments)
    {
        this.InitializeComponent();

        // Create the game.
        _game = XamlGame<Main>.Create(launchArguments, Window.Current.CoreWindow, this);

        txtTest.TextChanged += txtTest_TextChanged;
    }

    void txtTest_TextChanged(object sender, TextChangedEventArgs e)
    {
        Debug.WriteLine(txtTest.Text); //Write content to public string in Main.cs
    }

I found out how I can write the content of the TextBox to a string inside my gameloop but now I'm stuck how I can control the TextBox his properties from inside my gameloop so I can set the Visibility and Focus. Do I need to create my own EventHandler which will watch if I set a Boolean or something?

Thanks in advance.

Greetings,

ForT3X

Upvotes: 3

Views: 690

Answers (3)

Richard Garside
Richard Garside

Reputation: 89200

You can share a view model between the XAML page and your game.

The XAML page GamePage creates the instance of your game class. When it does you can also let the game class know about your view model.

_game = XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, this);
_game.XamlGameDataViewModel = new GameDataViewModel();
DataContext = _game.XamlGameDataViewModel;

There is more detail in my post Sharing your view model between Monogame and Xaml

Upvotes: 1

craftworkgames
craftworkgames

Reputation: 9957

Disclaimer: Let me just say that I've never worked with Windows 8 XAML projects or the GamePage class before but after doing a little googling I think I understand enough to help.

It seems that your issue boils down to a circular dependency. You want 2-way communication between your GamePage and your Game class.

Communicating from the GamePage to the Game class is easy, because the GamePage is already responsible for creating the Game class and storing it in the _game member variable. Therefore, to send messages from your GamePage to the Game you just need to add a method to your Game class, for example:

void txtTest_TextChanged(object sender, TextChangedEventArgs e)
{
    _game.SetHighscoreName(txtTest.Text);

    Debug.WriteLine(txtTest.Text); //Write content to public string in Main.cs
}

Communicating back the other way (from Game to GamePage) is a little trickier, but it can be solved using an interface and property injection.

First, create an interface that belongs to your Game class. What I mean by that is; it lives in the same project and or namespace as the Game class. It might look something like this:

public interface IGamePageController
{
   void ShowHighscoreTextBox();
}

Then, add a property to your Game class like this:

public IGamePageController GamePageController { get; set; }

Next, have the GamePage class implement the interface like so:

public partial class GamePage : PhoneApplicationPage, IGamePageController
{
    //...

    public void ShowHighscoreTextBox()
    {
        txtTest.Visibility = Visibility.Visible;
    }
}

And finally, in the GamePage constructor you need to set the GamePageController property.

// Create the game.
_game = XamlGame<Main>.Create(launchArguments, Window.Current.CoreWindow, this);
_game.GamePageController = this;

Once you have this pattern in place, it's easy to add new ways for your Game and GamePage classes to communicate by adding more methods to the interface or Game class.

Upvotes: 4

pinckerman
pinckerman

Reputation: 4213

If you need to store some values you should try with IsolatedStorage.
As MSDN says:
"Isolated storage is not available for Windows Store apps. Instead, use the application data classes in the Windows.Storage namespaces included in the Windows Runtime API to store local data and files."

You can find more information here.

Using Windows.Storage you should do something like this:

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (string.IsNullOrEmpty((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["highscore"]))
{  
    localSettings.Values["highscore"] = highscore;
}

Upvotes: 0

Related Questions