3

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

MrME
  • 337
  • 2
  • 14
  • I think I have a very rough idea of what your talking about but this is a difficult question to answer without some code. Post some relevant bits of code and explain your project structure. – craftworkgames Sep 02 '13 at 01:24
  • I update my Question with some info I gained from searching the internet en further specifying my problem that's left – MrME Sep 02 '13 at 11:01

3 Answers3

4

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.

craftworkgames
  • 9,437
  • 4
  • 41
  • 52
1

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

Richard Garside
  • 87,839
  • 11
  • 80
  • 93
0

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;
}
pinckerman
  • 4,115
  • 6
  • 33
  • 42
  • I need to store some values indeed but I'm gonna port it to other platforms so I now use SQLite due to this being more portable. So I appreciate the tip but my question is about how to read out a textbox so your answer is unrelated to my question. – MrME Sep 01 '13 at 20:00
  • You don't know how to read the textbox text or you don't know how to read it in a specified moment? – pinckerman Sep 01 '13 at 20:09
  • I know how to use TextBox.Text the problem is how I can acces it from my GamePage inside my gameloop and so reading out the text and setting some variables. – MrME Sep 01 '13 at 21:38
  • You should have a GamePage.xaml containing the XAML code and a GamePage.xaml.cs containig the C# code. In this one you can access your TextBox by its parameter Name you've set in the other file. – pinckerman Sep 01 '13 at 22:30
  • I knew how to access it in the code behind and know figured out to write it simply to a public string inside my gameloop now the problem left is that I need to acces the properties of that textbox (Visibility etc.) from inside my gameloop (I also updated my question with this) – MrME Sep 02 '13 at 11:04