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
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.
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
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;
}
Related
Im really new at this, Im following an example of a book I bought and I'm lost since I'm not sure if things have change in this regards, I've look all over the internet (google and here) to find the answer and nothing, deeply apologize if it has been asked before.
The thing is I'm following the code snipets from the book and to do a Balloon Shop online store, so to create a user control that will present the information for the different departments located in the database.
I need to reference a method in the user control located in the public class CatalogAccess, but is showing that CatalogAccess does not exist in the current context.
This is the code behind of the user control, where the problem happens:
public partial class UserControls_DepartmentsList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
list.DataSource = CatalogAccess.GetDepartments();
list.DataBind();
}
}
This is the code for CatalogAccess:
public static class CatalogAccess
{
public static DataTable GetDepartments()
{
DbCommand comm = GenericDataAccess.CreateCommand();
comm.CommandText = "Get Departments";
return GenericDataAccess.ExecuteSelectCommand(comm);
}
}
Mostly your CatalogAccess class is defined in a separate namespace and if so then you might want to import that saying using some_namespace;. Also, if it's defined in a separate project/assembly then you will have to add the reference of that assembly to your aspx project
I developing an wpf application with MVVM Light and I have all my ViewModels and Models within a class library. Now I would like to change a user setting but dont have access to the settings of my UI-Project in the viewmodel of my class library.
Question: Do I have to implement the change of a user setting into my View(UI)-Project to get this to work or ist there a way I could do this in my class library?
the only object I could use there is the Application.Current.Properties but that dont seem to be the correct ones:
Application.Current.Properties["Design"] = s;
I know I have to use:
Settings.Default.Design = s;
and on Exit I need to save all my settings to make the change persistent:
Settings.Default.Save();
I've seen something here - that seems to be a similar issue but I dont understand the answers...
I found an easy solution by myself. Just need to use the OnExit-Event (App.Xaml.cs):
protected override void OnExit(ExitEventArgs e)
{
if (Settings.Default.Design != DesignName())
{
Settings.Default.Design = DesignName();
Settings.Default.Save();
}
}
I have qr page in xamarin forms, and what i want is when the qr is shows up the screen brightness will be brighter, and i cant find solutions for that. i found some code in internet but it return with some error code message
EDITED :
I deleted the source code because its look like make some people confused, the code that i tried is for xamarin android and thats why it didnt work for me (I thought the xamarin.android and xamarin.forms code is almost same thats why i copy the code and paste it in xamarin forms and got some error message). And now my Real question is How to Change the screen brightness via xamarin Forms can we do that ? if yes how any link that can i try thanks
Xamarin.Forms is not a platform abstraction, but a UI abstraction. Hence there is no access to system services like the screen brightness. Neither did I find a NuGet to achieve this, hence you'll have to implement platform specific classes to adjust the screen brightness and resolve via DependencyService.
Implement the interface in your PCL
public interface IBrightnessService
{
void SetBrightness(float factor);
}
and use that interface operations using DependencyService from your common project to your platform specific implementation
var brightnessService = DependencyService.Get<IBrightnessService>();
brightnessService.SetBrightness(.2);
For a very good compact example of how to use DependencyService see this page
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
Android
Your error message
An Object is required for the non static field, method , or property 'Windows.Attribute'
means that you are trying to access a type as if it was an object. You need a context in which there is a Window:Window object, this would be the case in your MainActivity for instance.
When you are in another context you'll need to obtain an instance of Window somehow. Pre 2.5 this was possible with
var window = ((Activity)Forms.Context).Window;
This still works, but is deprecated. Anyway, you could use the CurrentActivity plugin and get the Window with
var window = CrossCurrentActivity.Current.Activity.Window;
(source)
using Xamarin.Forms;
[assembly: Dependency(typeof (AndroidBrightnessService))]
public class AndroidBrightnessService : IBrightnessService
{
public void SetBrightness(float brightness)
{
var window = CrossCurrentActivity.Current.Activity.Window;
var attributesWindow = new WindowManagerLayoutParams();
attributesWindow.CopyFrom (window.Attributes);
attributesWindow.ScreenBrightness = brightness;
window.Attributes = attributesWindow;
}
}
iOS
Use UIScreen.MainScreen.Brightness to adjust the brightness.
using Xamarin.Forms;
using UIKit;
[assembly: Dependency(typeof (iOSBrightnessService))]
public class iOSBrightnessService : IBrightnessService
{
public void SetBrightness(float brightness)
{
UIScreen.MainScreen.Brightness = brightness;
}
}
Android:
public void SetBrightness(float brightness)
{
Window window = (MainActivity.Instance as Activity).Window;
var attributesWindow = new WindowManagerLayoutParams();
attributesWindow.CopyFrom(window.Attributes);
attributesWindow.ScreenBrightness = brightness;
window.Attributes = attributesWindow;
}
This is my code:
public class CrazyWindow: EditorWindow
{
[MenuItem("Window/CrazyWindow")]
public static void Window()
{
EditorWindow.GetWindow(typeof(CrazyWindow));
Debug.Log("It should have appeared!");
}
string test = "";
public void OnGUI()
{
test = EditorGUILayout.TextField ("Text Field", test );
}
}
I'm using Unity3D v. 4.3.4f1 (free version) on Windows 7.
I have no idea why this is happening, as I can see in tutorials in the internet, that's how it should be done. The script is also in the Editor folder.
I'm able to click on the option "CrazyWindow" in the window menu, and I also get the Debug message informing me that the window should be working, but nothing happens besides that. No window is created at all!
What might be the cause of my problem?
Problem solved.
As Bart mentioned, I was using a custom Editor Layout, which was the case for the window not showing.
I just switched to one of the factory editor layouts and: ta dah, the window was there...
Pretty buggy thought.
Try renaming the 'CrazyWindow' part in the MenuItem and of the class itself. Unity remembers whether a window is visible or not and somehow something goes wrong there. Probably it thinks your window is visible (in cache) while actually it is not.
As Bart said, it remembers useless things
Just make it remember what we want it to
private void OnLostFocus() {
GetWindow<CrazyWindow>().Close();
}
I have made an application in XNA 3.1 in which a model is being loaded in picture box in windows form, which is working just fine: below is the code for Game1 class.
Now I tried to load multiple models in that for that I refered to an application of WinFormControlLoading, here I want to know that to write instead of modelviewcontrol line or where do I call the LoadContent function which calls my model
void LoadModel(string fileName)
{
Cursor = Cursors.WaitCursor;
string buildError = contentBuilder.Build();
if (string.IsNullOrEmpty(buildError))
{
// If the build succeeded, use the ContentManager to
// load the temporary .xnb file that we just created.
modelViewerControl.Model = contentManager.Load<Model>("Model");
}
else
{
// If the build failed, display an error message.
MessageBox.Show(buildError, "Error");
}
Cursor = Cursors.Arrow;
}
In this line error occurs
modelViewerControl.Model = contentManager.Load<Model>("Model");
when i changed my Game1 class function of LoadContent to public like
Game1 game;
game.LoadContent = contentManager.Load<Model>("Model");
I get error of
Error 1 'WindowsGame1.Game1.LoadContent()': cannot change access modifiers when overriding 'protected' inherited member 'Microsoft.Xna.Framework.Game.LoadContent()'
How I solve this?
Any help would be appreciated.
I don't know in which class you are doing this, but this class has to inherit from GameComponent or DrawableGameComponent, in this way you can use the ContentManager like this:
Game.Content.Load<Model>("Model");