Share a Generic XAML ListView, ItemsSource, and DataTemplate with UserControls in Windows Universal Apps

This post applies to Windows Universal Apps specifically. These techniques probably won’t work with WPF or Windows 8/8.1 Apps because the XAML hierarchy and namespaces are different.

The title is certainly a mouthful, but stick with me to understand where this scenario can be useful.

Let’s say you want to re-use a ListView without copy and pasting everything about it wherever you need it. Simple enough, right? Just put it in a UserControl! Not so fast. What if you want to define the ListView’s ItemsSource from one level above the UserControl? It gets a little more complicated. This is especially true if you want to take advantage of a VisualStateManager with the ListView’s ItemTemplate.

CustomListView

For example, I had a UserControl which allowed users to scroll left/right on a ListView by clicking buttons left/right instead of using a scrollbar. I wanted to share this ListView in multiple places,  but didn’t want to restrict the UserControl to a single ItemsSource by hard coding it in the UserControl itself. I wanted to “pass in” the ItemsSource and DataTemplate to define the layout of the items from one level above the UserControl. Additionally, I wanted the layout to change based on the width of the screen to work with phones, tablets, and wide layouts.

So here’s what I did after a few hours of struggling through wildly varying documentation across WPF, Windows 8/8.1, and Windows Universal Apps.

Creating the DataTemplate UserControl

We first need to define the lowest level of the chain to determine what and how the data will be displayed in each data bound ListViewItem.… Continue reading

Customizing ApplicationDbContext in ASP.NET MVC 5 and ASP.NET Identity 2.0

As of Visual Studio 2013 Update 1, the ASP.NET MVC 5 templates with Authentication enabled will create a project that contains a class named ApplicationDbContext. This is the Entity Framework DbContext that is used by the ASP.NET Identity libraries to manage user records.

By default, here is the generated class:

You’ll notice that it inherits from IdentityDbContext with a generic type of ApplicationUser. The base DbContext handles whatever is needed by the ASP.NET Identity libraries, and the ApplicationUser is the model that describes the authenticated user. If you’re like me, and you don’t want to create a ton of separate DbContext classes for different repositories, you can just mash it all together into ApplicationDbContext like so.

Upgrading an Existing Project from ASP.NET Identity 1.0 to 2.0

I have recently been playing around with ASP.NET MVC 5 via Visual Studio 2013 and the new ASP.NET Identity libraries. While the project templates mostly help you get to where you need to go when starting a brand new application, upgrading from the first Identity package (1.0) to the latest stable (2.0) wasn’t quite as smooth as I expected. For reference, Visual Studio 2013 Update 1 project templates use the 1.0 version of the ASP.NET Identity libraries. Visual Studio 2013 Update 2 RC (at the time of this writing), uses the 2.0 version.

By default, the project templates will set you up with a database schema that is accessible via EntityFramework and Code First. Since the templates utilize Code First, you will need to manage database migrations. And that’s where the trouble came in when upgrading from 1.0 to 2.0. The database schema changed (and will require code migrations) as a result of upgrading since the library supports new features like email confirmation, phone numbers, new primary keys, new indexes, and more. Attempting to run the application after simply upgrading all the libraries through NuGet gave me this essay of an error:

The model backing the ‘ApplicationDbContext’ context has changed since the database was created. This could have happened because the model used by ASP.NET Identity Framework has changed or the model being used in your application has changed. To resolve this issue, you need to update your database. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=301867). Before you update your database using Code First Migrations, please disable the schema consistency check for ASP.NET Identity by setting throwIfV1Schema = false in the constructor of your ApplicationDbContext in your application.

Continue reading

Using SDL2-C# to Capture Text Input

A common feature of applications and video games is to allow the player to input text for various reasons. Maybe we want to allow the player to input their character’s name in an RPG, name a city in SimCity, or type a chat message to a friend in online. Using SDL2, we can take advantage of its built-in text processing system which abstracts much of the operating system event handles and character encoding mechanisms.

On consoles such as Xbox and Playstation, text input is rather simplistic and limited to visual keypads that you select via the controller. On a PC, we have the full range of widely varying keyboards from English and Spanish to Russian and Japanese. If we want our game or application to attract users on an international scale, it’s probably in your best interest to learn here and now how to use SDL2 to accomplish this goal.

At first glance, it probably seems simple to process text input. If the user presses the ‘A’ key on the keyboard, the OS will send an event that the keyboard was just pressed, the key was ‘A’, and no modifier keys were pressed (CAPS, SHIFT, CTRL, ALT, etc…). That’s it, right? Unfortunately, there are a ton of languages on this planet, and some of them have thousands of characters in them. People who type in those languages most certainly do not have thousand-letter keyboards or entire walls of their houses dedicated as a giant keyboard. This basically means that some characters will require multiple key presses just to process.… Continue reading

How to load a map from Tiled Map Editor and render it with SDL2

I recently created a library named SharpTiles that is heavily based on Nick Gravelyn’s TiledLib. Both libraries will allow developers to load maps from Tiled Map Editor. However, TiledLib is based around XNA while SharpTiles is based around SharpDL (an XNA-like game framework for SDL2). I will discuss more about SharpDL in another post, but you can refer to the simple code in this post to learn how to render a map with a small subset of the library.

Load a Map

  • Window is a SharpDL object that represents a SDL_Window
  • Renderer is a SharpDL object that represents a SDL_Renderer
  • TiledMap is the main object that you will use to load a map file

Draw a Map

You can draw a map by looping through all TileLayers (layers which containd textured tiles) and all Tiles within the layers.… Continue reading

Setting Up SDL2 with Visual Studio 2012 and 2013

I have confirmed that everything below works for Visual Studio 2013.
Links updated to the latest release (2.0.3).

The Simple DirectMedia Library (SDL) version 2 has been released after a long time in development. It seems that the author has more time on his hands and more corporate backing from his new position at Valve Software. I’ve heard that they use SDL for various small time projects that don’t require heavy rendering. Even so, SDL does a formidable job at making things easy.

I wrote this guide under the following setup, but I’m sure that it can be applied to other version of Windows (and maybe even Visual Studio).


Anyway, try your setup and let me know how it goes. Follow these steps to get all setup and ready to code:

  1. Download the development libraries
  2. Download the 32-bit binary or the 64-bit binary
  3. Extract both zips to a convenient location
  4. Open Visual Studio and create an empty C++ project
  5. Right click the created project in the Solution Explorer, go to Properties
  6. Click VC++ Directories under Configuration Properties
  7. In the “Include Directorires” line, add the include directory from the extracted development library
  8. In the “Library Directories” line, add the libx86 directory from the extracted development library
  9. Click Linker –> Input under Configuration Properties
  10. In the “Additional Dependencies” line, add “SDL2.lib” and “SDL2main.lib” strings
  11. Click Linker –> System under Configuration Properties
  12. In the “SubSystem” line, change the selection to WINDOWS(/SUBSYSTEM: WINDOWS)
  13. Finally, copy the SDL2.dll file from the extracted 32-bit binary folder to your project’s output directory alongside the executable

OK, the ugly project setup is finished.… Continue reading

Resolving the problem where “WADLogsTable” is not created in Windows Azure Diagnostics

I’ve been using Windows Azure to host a Worker Role in a Cloud Service for an in-development version of my new app, Steam Community Viewer. This service will allow users to be notified of when their friends come online or start playing a game through Windows 8 Toast Notifications. Additionally, it will push updates to Live Tiles for new Steam deals.

During my tests, I tried to turn diagnostics on, but failed to save them to a permanent storage because the table that gets created when you enable basic logging wasn’t actually being created! Windows Azure Basic Logs will be saved in Azure Storage Tables under “WADLogsTable” based on a logging level (error, critical, verbose, etc…). This table is supposed to be automatically created when the service is uploaded and deployed with tracing enabled. If you find that the official Windows Azure documentation on using diagnostics is not helping you with this task, try the code below in your service OnStart() overload.

Porting a Game from XNA 4.0 to MonoGame 3.0 for Windows 8 Store

It’s fairly certain that Microsoft won’t be supporting XNA for non-Windows Phone platforms, so I decided to take some time to port my game to MonoGame. Just recently, the MonoGame team has released MonoGame 3.0 which includes full support for the Windows 8 Store. Follow these meager instructions to get yourself started with doing that. If you’re lucky like I was, there’s a chance that your code is almost a 100% carry-over to the MonoGame libraries.

  1. Download MonoGame 3.0 Installer
  2. Create New MonoGame Project for Windows 8 Store
    • Using Visual Studio 2012, create a new MonoGame project under Visual C# –> MonoGame –> MonoGame Game
  3. Manually Copy Code and Change References
    • Make sure MonoGame is referenced using the Windows 8 lib at C:Program Files (x86)MonoGamev3.0AssembliesWindows8MonoGame.Framework.Windows8.dll
    • Copy classes and files to the new project so that it mirrors the hierarchy of your XNA 4.0 project
    • Any libraries that are referenced which were compiled against the full .NET Framework will need to be recompiled against the .NET Core Framework (a subset of the .NET Framework that Windows Store Apps will reference)
    • Compile and fix any errors generated because of references to things that are no longer supported by Windows Store
    • Copy serialized .xnb files manually to the output directories of your MonoGame project (this is required because the Content Pipeline is not supported in Visual Studio 2012)

Some of the errors that I got were references to Close() method calls on streams (removed in .NET Core) and setting DTD validation on XmlStreams (also removed in .NET Core).… Continue reading

Chains of Acadia: Weapon Juggling

Like I stated in my previous blog post, I wanted to implement a weapon bar that allows players to cycle through weapons for different situations. Well, I came up with what you see in the screenshot below:

You can see a couple things here (and ignore the red boxes, those are just there for development purposes). This screenshot shows off the player status bar (health, shields, weapon experience) as well as the weapon bar. The icons in the weapon bar indicate the weapon that will be used when that slot is selected. In the screenshot, I have the flamethrower selected (yea, it’s not totally obvious that it’s a fireball). I can cycle through the weapons by using the scroll wheel or pressing the number keys that correspond to the slot. For example, pressing “1” will select the first slot (which is empty here) and pressing “6” will select the last slot (which is a proximity mine). For now, the input is only keyboard based, but when I eventually try to release this game on Xbox Live Indie Games, I will be more concerned about the gamepad as well.

Now, the juggling part comes in because each weapon gains experience when used and when experience points are picked up from enemies, bosses, and puzzles. However, only the active weapon will gain the benefit of the experience gain. This adds a bit of choice regarding what you want to level up first because the experience isn’t shared! When you fill the experience bar for a weapon, it will be upgraded to a new level.… Continue reading

Read about My Game: The Chains of Acadia

I have been developing software professionally for about 5 years now, and I have been playing video games nearly my entire life. The combination of the two is something that I decided to tackle back in July of this year. Having never programmed for anything other than enterprise software, I decided to ease my way into the scene via XNA. I was already familiar with C# through my work, so it was an obvious choice to learn the basics and dip my toes into the ocean of game design.

Since I am a full-time employee, I usually only get nights and weekends to work on this project. I would love to dedicate all my time to the development of my first game, but I am constantly reminded that something has to pay the bills. Anyway, you did not come here to read about my life; you came to read about my game! I have quickly learned that game development calls for many different talents. Some of the requirements include skills that I simply do not possess. Namely, I have trouble producing quality art, sound, and music. Since I am in this alone (for now), I have to rely on the wonderful open source community to fill this important gap. I can handle the technical side no problem, but when it comes to art, yikes. Without further ado, here is my game.

The Chains of Acadia is a story of a hero. Oh, wow, how original. The thing is though, Acadia (the hero), does not know that he is a hero.… Continue reading