Adding content in MonoGame - c#

I'm new to Monogame. I'm trying to add content (An image), but I can't do this. In each tutorial that I have seen, the solution explorer has a folder where I can put assets, but not mine.
I tried to add an image to the root folder, but it doesn't work.
hero = Content.Load<Texture2D>(#"Image1.png");

Right click on your solution and click Add, then New Project.
From there, select Installed > Visual C# > Monogame, and click on the MonoGame Content Project
After you do this, you will see your content project in your solution. In order to allow your game to use it, look under your Game project, and right click References. Navigate to Solution > Projects and tick the checkbox next to your Content project.
You can now add your images to your Content project, and use them like a regular XNA game.
Check out these links:
Managing Content |
Monogame
Loading Content in XNA
(MSDN)

Related

Forms Xaml Page missing in Visual Studio 2017 Xamarin

All the tutorials and web pages talk about using Project|Add New Item and adding a new "Forms Xaml Page". But whenever I install Xamarin and Visual Studio 2015, I just get the "Forms ContentPage" and "Forms ContentView" which just generate a C# file, no Xaml.
I have tried this and it didnt work.
And I've also tried reinstalling Visual Studio.
Does anyone have a clue what to do?
The real problem is that I want to keep the Xaml with the code-behind file together, like
I just did a clean install of Visual Studio 2017 and the "Forms Content Page Xaml" showed up in Add New Item dialog. Which items you chose when installing the Xamarin for Visual Studio pack? Compare with the ones I've selected. Maybe something was left unchecked?
Also, if you want to keep the Xaml file together with the code-behind file, there's a way to do it by editing the YourProjectName.projitems inside your project folder. To find it, right-click the your main project and open its folder in the Windows Explorer, and then use a file editor to edit the .projitems file.
Search for the Compile tag for the .cs files you want and add the DependentUpon tag inside it, just like in the example below:
<Compile Include="$(MSBuildThisFileDirectory)YourFile.xaml.cs">
<DependentUpon>YourFile.xaml</DependentUpon>
</Compile>
After doing this, unload and reload your project.
Unfortunatelly I've had to do this procedure to all the new Xaml files I've created on my projects.
I think you can use the content page, it has the .xaml extension and comes with the code behind. They just change the name of the template.

How to change application icon in Xamarin.Forms?

I replaced all the images everywhere (by this I mean in drawable folders and all Windows Assets folders and iOS Resources folder), but it still shows me the default Xamarin icon for the app. I tried this code, too, but it doesn't seem to work either. Can someone tell me the solution?
[assembly: Application(Icon = "#drawable/icon")]
Updating Icon and Name (Android)
If you changed the icon file name please ensure you update the Icon reference in MainActivity.cs:
[Activity(Label = "MyName", Icon = "#mipmap/myicon", Theme = "#style/MainTheme"]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
}
You should also update the names of icon.xml and icon_round.xml in the mipmap-anydpi folder to match to new icon name. If launcher-foreground.png was renamed then update the value of <foreground...> in corresponding icon.xml.
Summary (Android)
Replace the png's in mipmap folders with your new icon
If name was changed, update Icon value in MainActivity.cs
If name was changed, update name of (or create a new copy of) icon.xml and icon_round.xml
If name of launcher-foreground.png was changed then update value in icon.xml. Eg:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="#color/my_launcher_background"/>
<foreground android:drawable="#mipmap/my_launcher_foreground"/>
</adaptive-icon>
If name wasn't changed and you've cleaned and re-built project but still your new icons are not deploying: Delete obj folder from Android project directory
Details and Issues if not updating (Android)
Xamarin Forms/Android puts 2 icons in each mipmap folder (mipmap-mdpi, mipmap-hdpi, mipmap-xhdpi, etc) - icon.png and launcher-foreground.png.
Replace both these images in each of the mipmap folders. I kept the same names but it should be possible to use a different name.
If you rename launcher-foreground.png then you must update the values in icon.xml and icon_round.xml (or their equivalent new names). The value in the <foreground...> tag references the icon/png. So, if launcher-foreground.png was updated to my_launcher-foreground.png this should be reflected in the icon.xml (see step 4 in summary above).
If the icons still don't update even after cleaning/re-building it could be that the original/default icons still existed in the Android project obj folder. The solution is to delete the obj folder from the Android project directory.
iOS
For iOS, please see Managing Icons with Asset Catalogs. A summary taken from the linked page is provided below:
For icons, a special AppIcon image set can be added to the Assets.xcassets file in the app's project.
To use an Asset Catalog, do the following:
Double-click the Info.plist file in the Solution Explorer.
Click on the Visual Assets tab and click on the Use Asset Catalog button under App Icons.
From the Solution Explorer, expand the Asset Catalog folder.
Double-click the Media file to open it in the editor.
Under the Properties Explorer the developer can select the different types and sizes of icons required.
Click on given icon type and select an image file for the required type/size.
Click the Open button to include the image in the project and set it in the xcasset.
MacOs
The guide for MacOs is Application icon for Xamarin.Mac apps. You need to consider the following steps:
Required image sizes and filenames
Packaging the icon resources
Using the icon
detailed in the linked docs above.
For Android try to set the icon app like this:
[Activity(Icon = "#drawable/icon")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
.....
}
Make sure that you have changed all the icon images on all drawable folders ( drawable, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi ).
For iOS I like set the app icons and splash screen with Asset Catalogs, here you can find a guide to how to use it:
https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_images/app-icons/
For iOS in visual studio...
Step1: Generate all the app icons from https://appicon.co/
Step2: In solution explorer, under iOS project, expand 'Asset Catalogs' and double click on 'Assests'. You can change the existing 'AppIcon' icons or Add a new Asset and attach the icons.
This is how you add a new asset...
Step3: In solution explorer, under iOS project, double click on Info.plist.
Go to 'Visual Assets' tab, click on 'App Icons' and change the 'Source' to the
asset that you just created.
Done.
Hope this helps someone!
Answer is deprecated
If I understood you correctly, for iOS project: Properties -> iOS Application
For Android: Properties -> Android Manifest
I had the same problem, but the following helped resolve it:
Go to MainActivity.cs (in the Solution Explorer)
Look for where it says 'Icon ='
Change the location of the Icon, but don't include the extension, i.e. jpg, png, etc. (For ex. Icon = "#drawable/weatherIcon")
Save the file
Restart debugging (the little restart icon beside the stop and pause icon around the top right-hand side of your screen)
If you get stuck on any part of this check out this short video from 02:47 onwards https://youtu.be/5g8lWPQZFxs?t=167. It helped me a lot.
Try to replace all the icon on iOS project > Properties > iOS Application > Iphone Icons/IPad Icons.
I change all of them, the settings icon do change. Images that on Resouces can be created by just insert images on there.
Tested this today and noticed that you only need to change the icon images in all drawable folders,
drawable,
drawable-hdpi,
drawable-xhdpi
drawable-xxhdpi.
Depending on the device it's going to use different images. For me i used "Visual Studio Android Emulator" using:
- 5" KitKat(4,4) XXHDPI Phone (Android 4,4 - API 19)
Did not need to uninstall and reinstall the app in the emulator, it updated on it's own after starting the project with "build" checked in the menu Build/Configuration manager.
On top of that no code changes were made!
If you replace the Icon.png and still nothing changed on the device, than change "Copy to Output Directory" propertiy to "Copy if newer" or "Copy always".
That was my solution.
All of these solutions are great, but
THE SIMPLEST WAY:
You can now just use ResizetizerNT's nuget package and upload one svg file and set IsAppIcon to true. That should generate everything needed.
If the existing app icon is icon.png, just name the svg to icon.svg and it should automatically work.
Alternatively, you can also add another SVG for the background in order to support adaptive icons on Android.
Context: ResizetizerNT was created by the Xamarin.Forms/MAUI Engineering Manager, Jonathan Dick

Can't get content to load using Monogame with VS2012

My code is
new GameFont(Content.Load<SpriteFont>("LoadingFont"), "LoadingFont")
According to what I've read, you have to use VS2010 to compile your assets into .xnb format, which I have done, and place them into the Content subfolder in your bin directory, which I have also done. However, I get an error saying "Could not load LoadingFont asset!".
I'm not really sure what else to do. I read a very old post saying that assets made using XNA 4 won't work, but I don't know if that's still true, or how to change my version of XNA to 3.1.
Any ideas? Perhaps there's a better way without using VS2010 at all?
Have you added the content to the Content folder of your project and set the Build Action to Content (in the properties window).
I'm not exactly sure what the process is to use assets in the .xnb format. I know it can be done that way but I normally just add the raw image and sounds files to my Content folder directly. Font's might be a little trickier to do that way though.
EDIT: 2015
A lot of things have changed since this question was written. These days MonoGame has it's own Pipeline tool for processing content into XNB files. The first thing to try would be to process your SpriteFont using the new Pipeline. This way you can avoid the need to depend on XNA altogether.
Alternately, another way to get around font issues in MonoGame is to pre-render them to a texture using the BMFont tool and use MonoGame.Extended to render them in your game.
Once you've installed MonoGame.Extended you can load fonts created with BMFont just as you would a SpriteFont but using the BitmapFont class instead.
_bitmapFont = Content.Load<BitmapFont>("my-font");
Then render some text like so:
_spriteBatch.Begin();
_spriteBatch.DrawString(_bitmapFont, "Hello World", new Vector2(100, 200), Color.Red);
_spriteBatch.End();
I have a full tutorial on my blog
I've been using MonoGame in VS2012 with no trouble; Content.Load<T>("myContent") still works for me. One thing that has been catching me out lately (tip: don't code when sleep-deprived) is that you have to check your assets are set to "Copy to output directory" in Solution Explorer, or else when the game builds, the assets won't go with it, which is why you get that error - it can't load what isn't there! Right-click on your assets and look under Properties. If they're set to "Do not copy" then you'll want to change that.
Like #craftworkgames I can't give you a full answer. But the traditional Content Pipeline that came with XNA 3.1 has gone in 4. So any assets need loading a slightly different way. For example, to load a .png asset to display a sprite I do the following:
rock = Texture2D.FromStream(GraphicsDevice, TitleContainer.OpenStream(#"Rock-Large-Stones-PT.png"));
and add the .png file to the solution as normal. I'm pretty sure it's going to be something similar for fonts but I haven't quite figured it out yet either ;)
I was having the same issue not sure if you got it resolved or not but here is what I did.
Click on the Monogame Content Pipeline under the "Content" folder.
Add in your spritefont or texture.
Make sure in the Monogame Content Pipeline that you change the action from "Build" to "Copy"
Then when you want to load the content simply;
texture = content.Load("texture.png");
Hope this helps :)
I had similar problem but with 'Windows 8 Store' as a target platform... while everything was fine with Windows Open GL version.
On Windows 8 I received "could not load as a non-content file" when I tried content.Load("Fonts/TestFont").
I replaced MonoGame DLLs in my solution with the MonoGame.Windows8 project (I downloaded the sources from CodePlex) and I did some debugging... and found out that MonoGame looks for the Content folder in the bin...\Debug\AppX folder... while during the build the Content folder is copied into different place. So when I manually copied the Content folder into AppX folder, the error has gone and my game prototype works fine now.
I guess I either missed some setting in the Project preferences related to AppX (Presumably this folder is needed for the Windows 8 device emulator?) or... MonoGame should look for the Content folder in different place... Anyway I will be renewing the Content folder manually for now because it is copied into wrong place during the compilation.
I have Content folder in my project and build action is set correctly (Content)... and 'Copy always' is chosen but during the build (compliation) that Content folder is copied to Debug folder while it should be copied to Debug\AppX folder otherwise MonoGame can't find it.
(I maybe wrong with exact paths because I am currently at work and the issue is at my home PC)
Maybe it is just some configuration issue in my Visual Studio 2012.
Hope this information helps.

XNA windows phone 7 how to load from Content?

"
If you want to play along, you can create a Visual Studio project named NaiveTextMovement
and add the 14-point Segoe UI Mono font to the Content directory. The fields in the Game1
class are defined like so: "
how to add 14-point Segoe UI Mono font to the Content directory?
iv tried creating a folder in the solution and placing the font in it then i try to load it using Content.Load ... but it doesnt work. how is that?
You need to create a content project, then create a font description file. Reference the content project from your main XNA project.
In fact, a content project should already have been generated for you.
The font description file is an XML file. Editing it will be self-explanatory once you open it, as it will be auto-generated and well-commented.
When you create a new Windows Phone 7 game project, the template creates two different projects in your solution. One is the WindowsPhoneGame project and the other is the WindowsPhoneGameContent project.
The Game project is where you'll be adding all your code and the Content project is where you'll be adding all your game assets like sprites, textures and spritefonts.
You can see in the image below the two projects shown in the Solution Explorer (that's the pane in the upper right if you have the default configuration for Visual Studio).
The "Content" project is the one highlighted in the above image.
To add a SpriteFont, simple right click on the Content project and select "Add" and then "New Item" from the Add menu. This will open up the below dialog.
From the "Add New Item" dialog, simply select SpriteFont and give it the name you want. That will add a new SpriteFont object to your Content project and to your game.

adding images in my project

I'm trying to add/change images in my project, using Microsoft Visual Studio 2008 C#. Along with it, Devexpress components are also included.
What I did, I copied an image (.png file) and paste it it in my "PrintRibbonControllerResources.resx" and then after that I have to open again the MainForm.cs right click on the form and click on the Run Designer. It will open the Ribbon Control Designer. From there, I can add the image.
Do you think its ok? It's my first time to do this, and I don't have any experience & I'm learning it by doing.
Thanks
===
it seems I don't see the add function under resource tab
I generally add an image to the project itself. (Add/Existing item) I do this so I can use SourceControl to check in/out the image file. The resx then links to these files.
Basically, my philosophy is: if it works, don't fix it.
If this solution works for you, it's fine. At least, until you discover a situation where it fails.
If you go in the properties of your project, and select the resources tab, you can add it directly from there and it will be accessible in the default resource file of your project, which might be more convenient.
I like to add it to a solution folder (or folder in the project) and like that to the resx via "Add Exising File".
I get all of the advantages of having the resource file, but also the advantages of having a physical file (editing, quick view, etc.)

Categories

Resources