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");
Related
I have created a boolean algebraic simplifier. It simplifies expressions and I am content with it. However, I am trying to add a feature that allows users to check if two expressions are equivalent. For this I have created a new form that allows the user to input two expression by clicking buttons. To do this, I thought it best to simplify both expressions and then compare the two for equivalency. As I have got lots of subroutines and code that works for simplification in another form, I thought making the form a child form of the form with the code in would allow me to call the subroutines instead of copying them onto the form. I have made these protected in the parent form. I have inherited like so:
public partial class Expression_Equivalency_Form : Expression_Simplifier
However, when I click onto the form designer, this error appears and I cannot view the graphical interface of the form:
"Could not find file File Path"
The file is in the debug folder which is within the bin folder within the folder containing the program and is recongised in the parent class. The file is read from and appeneded by the parent form without issue. I have tried to research this but have been unable to find a solution. Does anyone know one?
I have read to the file and appended to it. I have also used the following code to remove any blank lines from my text file:
File.WriteAllLines("PreviousExpressionInputs.txt",
File.ReadAllLines("PreviousExpressionInputs.txt").Where(l => !string.IsNullOrWhiteSpace(l)));
Code that writes to the file:
using (BinaryWriter Writer = new BinaryWriter(File.Open("PreviousExpressionInputs.txt",
FileMode.Append)))
{
Writer.Write(expressionandanswertowritetotextfile);
}
Code that reads from the file:
foreach (string line in File.ReadLines("PreviousExpressionInputs.txt"))
{
try
{
LinesInFile.Add(line);
}
catch (Exception)
{
}
}
Consider following facts:
When you open a form in design mode, the constructor of its base class will run.
When you look for a relative file name, the path will be resolved relative to the current working directory of the application.
When the form is in design mode, the current application is Visual Studio and its working directory is where the devenv.exe is located.
It describes why you cannot find your text files. Because you have some code in the constructor of your base form(or fir example load event handler of the base form) which looks for the file and since the filename is relative, its looking for the file in the Visual Studio working directory and could not find file.
How to prevent the problem? Check DesignMode property to prevent running the code:
public partial class MyBaseForm : Form
{
public MyBaseForm()
{
InitializeComponent();
}
private void MyBaseForm_Load(object sender, EventArgs e)
{
MessageBox.Show("This will show both in run-time and design time.");
if (!DesignMode)
MessageBox.Show("This will show just in run-time");
}
}
Create the derived form and open it in designer to see what happens:
public partial class Form1 : MyBaseForm
{
public Form1()
{
InitializeComponent();
}
}
To learn more about how designer works take a look at this post.
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;
}
I have recently got into c# with XNA and am just making the transition to MonoGame since I've read that XNA is no longer supported. With that said I have come across a problem in MonoGame that I didn't have with XNA when attempting to make a Load() method for a Sprite class in my program. The way I used to do it in XNA is as follows:
public void Load(ContentManager content)
{
content.Load<Texture2D>(AssetName);
}
Now the problem I have with MonoGame is that I cannot seem to reference ContentManager in my Sprite class. The class has all the 'using Microsoft.Xna.Framework' that my Game1 class has, and nothing in my code is static so I don't understand why I cannot reference ContentManager, since it is not recognised when I try and put it in the Load(). Is there a different way to do this in MonoGame, or am I not referencing it properly?
Hm, I don't see the issue right away, but I'll try to help:
You have probably tried right-click/resolve already, but it's worth saying it anyways.
Reminder that Loading content in Monogame is different than using XNA. You've to use the build-in pipeline tool and transfer the content over there, don't forget to build it everytime when uploading a new texture.
There has been no changes to the Content.RootDirectory?
Just making some heads-up to be sure you've done that. and hopefully this will help you out as well.
Assuming you've referenced the MonoGame.Framework.dll or NuGet package there's nothing special about the code. It should look something like this:
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace YourGameProject
{
public class Sprite
{
public string AssetName { get; set; }
public void Load(ContentManager content)
{
content.Load<Texture2D>(AssetName);
}
}
}
I just wrote that code in my own project and it compiles. If it's not working for you there must be something else wrong.
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;
}
I have the following code trying to load a web page on my app:
Console.WriteLine("login view class");
NSUrl url = new NSUrl("http://deekor.com");
NSUrlRequest request = new NSUrlRequest(url);
loginWebView.LoadRequest(request);
The last line:
loginWebView.LoadRequest(request);
is throwing an System.Reflection.TargetInvocationException exception.
loginWebView is a UIWebView
Whats going wrong?
Edit
I think I was forgetting this line: loginWebView = new UIWebView(); After adding it the exception is gone... however the page is not loading..
Looking at the locals in debug I see loginWebView is set to null before I call loginWebView = new UIWebView();. Do I want to be creating a new one like this, or is something wrong with my outlet?
LoginViewController.Designer.cs
StoryBoard
#Deekor,
I loaded up a new project, and I tried to make exactly what you were making. I ran into the same issue if I followed these steps:
Create a new iPhone, single view, storyboard project.
Open up the storyboard file and drag a UIWebView onto the UIViewController that is created automatically in the new project.
Go back to Xamarin Studio and in the LoginViewController.cs file that was created for me, try to put in the loginWebView.Request code.
This doesnt work, as you noticed because the web view is null. It should not be. I don't know why, but there was also missing methods in that LoginViewController.cs file that is auto created. I could not override ViewDidLoad() which is where you want to be. Even though the class is inheriting from UIViewController, the override was just not available. So here is how I fixed the problem:
Delete the auto generated UIViewController in the Storyboard.
Delete the auto generated class that came with it (LoginViewController.cs)
Now in the storyboard, drag in a new UIViewController.
In the Custom Class box in interface builder, enter your class name: LoginViewController.
Go back to Xamarin Studio, and let it sync up and create the files for you again.
No go back to Interface Builder/Xcode and drag a UIWebView onto that new LoginViewController you created in step 3.
Drag your outlet for the loginWebView just as you did before.
Go back to Xamarin Studio and let it sync up.
Open your LoginViewController.cs file and now you can override ViewDidLoad().
Now you can simply have this:
public partial class LoginViewController : UIViewController
{
public LoginViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
loginWebView.LoadRequest(new NSUrlRequest(new NSUrl("http://deekor.com")));
}
}
I don't know why sometimes the auto-generated files from a new project act strange, but basically all I did was delete what it created, and manually add the same thing again and then it worked. The web view being null when you launched is what made me think something was wrong. Please comment if something is confusing, I will do my best to explain better if I can. I did get this working following the steps above. In the image attached you can see it running in the simulator, showing your page.
Just to see what is happening try the following:
Console.WriteLine("login view class");
var webView = new UIWebView(this.View.Frame);
this.View.Add(webView);
NSUrl url = new NSUrl("http://deekor.com");
NSUrlRequest request = new NSUrlRequest(url);
webView.LoadRequest(request);
If that works it means it is an issue with how the WebView has been added to your Storyboard and then connected as an outlet...