ArgumentNullException on Content.Load<SpriteFont>

Jul 5, 2011 at 2:40 PM
Edited Jul 5, 2011 at 5:00 PM

I have a straight-forward project. Content.RootDirectory is set to "Content". I have a Content type project; Content/Fonts/Kootenay.spritefont added as "Content." I'm getting an ArgumentNullException on:

 

this._defaultFont = Content.Load<SpriteFont>("Fonts/Kootenay");

(Likewise if I specify "Kootenay" as the font name.) What am I missing? Is this even the right approach?

 

I've tried both adding .spritefont files to my main project and to a XNA Content project; It seems like it won't load, no matter what. I even tried game.LoadFont(...), but that didn't make any discernible difference.

I also tried adding a .xnb with the type set to Content, but it gives me the same exception.

Coordinator
Jul 6, 2011 at 12:02 AM

Fonts are a little weird because text is so different in Silverlight. For fonts that are not system fonts, you basically have two options, each with their benefits and drawbacks.

1. You can include the xnb file for the font instead of the spritefont file. This needs to be the xnb file generated from a debug build of your XNA solution. This is the easiest way, but you don't get the benefits of vector graphics in Silverlight, and it has the extra build step of building your XNA project in debug mode first.

2. You can include the SpriteFont file, but then you need to also include the ttf file in your project and you need to add an extra line of code that needs to be called on startup. There is an example of this in the SnakeGameSilverSprite sample in the SilverSprite solution. Let's say this is your SpriteFont file:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Becker Black NF</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>28</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Regular</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>

Then in Page.xaml.cs, add the following line to the code after the InitializeComponent call:

game.AddFont("Becker Black NF", "./Becker Black NF.ttf#Becker Black NF" );

 where "game" is the name of the game element in the xaml. The first string in this call is the same as the FontName in the spritefont file, and the second is the font file information.

For your case this will probably be something like:

game.AddFont("Kootenay", "./Kooten.ttf#Kootenay" );

assuming that you add Kooten.ttf to the root of your Silverlight project and give it a build type of Resource.

Let me know if you still have issues after following these steps.

Jul 6, 2011 at 11:55 AM
Edited Jul 6, 2011 at 4:26 PM

I did manage to get it working with the Snake example -- I don't know how I missed that; that would've solved most of my problems! Thanks for helping me work through that. I got both embedded and local fonts to work.

I've also noticed that this crash still occurs if it's embedded (ttf/otf and .spritefont) in a referenced DLL. If it's possible, I would really like this to work; i.e. that I have my Game class and game-specific content in one DLL, and some base classes and fonts in a referenced DLL. Is this something you might be able to do?

This would allow the creation of "font packs" and the like; I'm creating a reusable library, and it would be much better to just ship one DLL rather than also having to explain and detail how to add all the fonts (which is error-prone).

Coordinator
Jul 6, 2011 at 12:36 PM
Hmm I can look into this, there may be a way to do this currently already, I'll have to but a little sample together and see if I can find a location string that works.

On Wed, Jul 6, 2011 at 7:55 AM, ashes999 <notifications@codeplex.com> wrote:

From: ashes999

I did manage to get it working with the Snake example -- I don't know how I missed that; that would've solved most of my problems!

I've noticed, though, that this crash still occurs if it's embedded in a referenced DLL. If it's possible, I would really like this to work; i.e. that I have my Game class and game-specific content in one DLL, and some base classes and fonts in a referenced DLL. Is this something you might be able to do?

This would allow the creation of "font packs" and the like; I'm creating a reusable library, and it would be much better to just ship one DLL rather than also having to explain and detail how to add all the fonts (which is error-prone).

Read the full discussion online.

To add a post to this discussion, reply to this email (silversprite@discussions.codeplex.com)

To start a new discussion for this project, email silversprite@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jul 6, 2011 at 4:26 PM

Great, thanks. I've updated my last post to be a bit more clear. Your instructions were perfect :o) just took me a while to figure out what I'm doing.

Jul 21, 2011 at 1:02 PM

Any updates on this?

Sep 5, 2012 at 4:12 PM

Hi,

What is the proper code to load your spritefont in XNA in this example?

Content.load<SpriteFont>(".. WHERE?? ...")