To debug and run a program using XNA in Silverlight5

Page updated :

Since the project is created, the parts that you understand should be skipped.

The first step is to create a Silverlight application. Select File, New, and Project from the Visual Studio menu.

プロジェクトの新規作成

Select Silverlight Application.

Silverlight アプリケーションを選択

For web site hosts, be sure to specify the site that you want to host. Test pages that are automatically generated when you debug silverlight alone cannot run XNA programs using the GPU. (This is not the case if you create a test page manually.)

ホストする Web サイトの作成

When the project is created, right-click Browse Settings in the Silverlight application and select Add Reference.

参照の追加

Select Assembly and Framework from the left menu to check the following items: (Do not press the OK button yet.)

  • Microsoft.Xna.Framework
  • Microsoft.Xna.Framework.Graphics
  • System.Windows.Xna

参照するアセンブリを選択

Then select "Extend" from the left menu, check the following items, and press ok button.

  • Microsoft.Xna.Framework.Graphics.Extensions
  • Microsoft.Xna.Framework.Math

You have chosen the minimum required to run the XNA program, so choose the assembly you want to reference as needed.

参照するアセンブリを選択

The assembly reference is added.

参照一覧

Now you'll modify the code. This time, we'll just give you a minimum of information about whether the XNA program worked correctly.

Open MainPage.xaml from the Silverlight application and add the following code:

<Grid x:Name="LayoutRoot" Background="White">
  <!-- 追加ここから -->
  <DrawingSurface Draw="DrawingSurface_Draw" />
  <TextBlock x:Name="textBlockMessage"/>
  <!-- 追加ここまで -->
</Grid>

DrawingSurface is the area you want to draw in the XNA program. When you run it, a Draw event occurs repeatedly, so write a drawing process there. Let's draw only the background color so that it is understood that it is working correctly this time.

TextBlockMessage is for text to display whether an XNA program is working correctly or not. This time it's in place to confirm it, so it's not really a release.

Then open the MainPage.xaml.cs file. First, organize the using area. Because the namespace for the Silverlight application is specified, change it for XNA. Define it according to your application when you actually make it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Graphics;

Next, make the text display the current drawing mode when it starts and why it was not able to draw. I'm writing it in the constructor because I know when the Silverlight application is launched.

public MainPage()
{
  InitializeComponent();

  textBlockMessage.Text
    = string.Format("RenderMode:{0}\r\nRenderModeReason:{1}",
                    GraphicsDeviceManager.Current.RenderMode.ToString(),
                    GraphicsDeviceManager.Current.RenderModeReason);
}

Write the drawing process. This time, i'm just drawing the background in the XNA Framework when a drawing event occurs.

private void DrawingSurface_Draw(object sender, DrawEventArgs e)
{
  GraphicsDeviceManager.Current.GraphicsDevice.Clear(new Color(0x64, 0x95, 0xED));
}

Let's debug.

デバッグ実行。

When you do, it should look like a figure. RenderModeReason@GPUAccelerationDisabled disables GPU drawing where hardware drawing is not available in RenderMode_Unavailable. This is a server-side issue, so you need to enable it first.

描画ができない

Open the "<Project Name>TestPage.aspx" file from a ASP.NET project hosted by Silverlight Run.

TestPage.aspx を開く

Add EnableGPUAcceleration to the object parameter and set the value to true. This would allow this Silverlight application to use the GPU on the server side. (It's actually solved by the client's processing, but it's the server that outputs the HTML, so it's a server-side problem.)

<body>
  <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
      <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
        <param name="source" value="ClientBin/DebugXna.xap"/>
        <param name="onError" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="5.0.61118.0" />
        <param name="autoUpgrade" value="true" />
        <!-- 追加ここから -->
        <param name="EnableGPUAcceleration" value="true" />
        <!-- 追加ここまで -->
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Microsoft Silverlight の取得" style="border-style:none"/>
        </a>
      </object>
      <iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
    </div>
  </form>
</body>

Now run it and you'll see. RenderModeReason is now SecurityBlocked. This is because GPU drawing is not allowed by default for security reasons. Since this is a client issue, you should have the user perform the following steps and allow gpu drawing to be released in addition to debugging.

RenderModeReason が SecurityBlocked に変化

To allow GPU drawing, right-click the Silverlight application and select Silverlight from the menu.

Silverlight を選択

As the dialog opens, select the Permissions tab, select the appropriate site, and then click the Allow button.

アクセス許可

When the permission is "Allowed", click the OK button.

アクセス許可が「許可」に代わる

If you are debugging, updating the browser does not take effect, so once you close and run the browser again, you will see that RenderMode becomes hardware drawing and xna background drawing is also enabled. Now you can create your Own XNA program.

ハードウェアによる描画が有効