ContentManager.Load inside MainPage.xaml.cs

Mar 18, 2010 at 7:35 PM

Hi, i'm using MainPage.xaml.cs (instead of MyGame.cs) & trying to create an array of meshes but couldn't get ContentManager.Load to work.

Please help.. thanks!


[FILE: MainPage.xaml.cs]

public partial class MainPage : UserControl {

               private ContentManager xxContentMgr;
               public Mesh[] tmpmesh = new Mesh[9];

       public MainPage()   {

            InitializeComponent();              

// "_teapot" is defined in MainPage.xaml in <UserControl.Resources><Geometries:Mesh x:Key="_teapot AssetName="/GettingStarted;component/Assets/untitled.ase"/>

                tmpmesh[0] = Resources["_teapot"] as Mesh;
                myscene.Scene.AddNode(tmpmesh[0]);                                        // this one OK
                tmpmesh[0].Position.X += 1 * 10;
     
                //tmpmesh[1] = xxContentMgr.Load<Mesh>("untitled.ase");         // doesn't work, stuck at silverlight loading 100%
                tmpmesh[1] = tmpmesh[0] ;                                                        // shows but killed tmpmesh[0]
                myscene.Scene.AddNode(tmpmesh[1]);
                tmpmesh[1].Position.X += 2 * 10;

            }

  // rest of the class

}

 

Coordinator
Mar 18, 2010 at 8:00 PM

Were do you get the instance of the ContentManager ?

There is a certain event chain that occurs, so in order for that to work, you would probably need to hook the Loaded event on MainPage and do your stuff there. But I can't guarantee that it will, as the only ways for now is through Xaml and the Game class.

Mar 19, 2010 at 4:12 PM

the ContentManager instance.. sorry.. i'm not sure about your question, I just started learning java/c# & OOP for a month only! (was on no-so-OOP VB6..).

Can you give me a sample of hooking the MainPage.Loaded event OR loading mesh programatically in MainPage.xaml because I'm seeing that TargetDevice.Initialize<MyGame>() will be deprecated soon. I also tried Balder.Core.Utils.Cloner.DeepClone<Mesh>(tmpmesh[1], tmpmesh[0]) but not working.

FYI, I've to comment-out "// TargetDevice.Initialize<MyGame>()" in App.xaml.cs for the MainPage.xaml Silverlight controls (eg: Button) to work.

 

 

 

 

 

Mar 19, 2010 at 7:01 PM

Yes-lah, I've found the solution using XamlReader.Load ! Credit goes to http://www.gotnet.biz/Blog/post/Silverlight-Tip---Loading-XAML-Fragments-Dynamically.aspx


                public Mesh[] tmpmesh = new Mesh[999];
Object dynaxaml; string xamltxt = "<Geometries:Mesh "; xamltxt += " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" "; xamltxt += " xmlns:Geometries=\"clr-namespace:Balder.Core.Objects.Geometries;assembly=Balder.Core\" "; //xamltxt += " x:Key=\"_enemyShip_90\" "; xamltxt += " AssetName=\"/GettingStarted;component/Assets/backbox.ase\" />"; dynaxaml = System.Windows.Markup.XamlReader.Load(xamltxt) as Mesh; myUserControl.Resources.Add("_enemyShip_90", dynaxaml); tmpmesh[90] = Resources["_enemyShip_90"] as Mesh; myscene.Scene.AddNode(tmpmesh[0]); tmpmesh[90].Scale = new Vector(5f, 5f, 5f); tmpmesh[90].Position.X += 2 * 10; tmpmesh[90].Position.Z -= 20;

 

 

Coordinator
Mar 20, 2010 at 8:18 AM

Ok. Great you figured it out. 

Since you're already generating Xaml like that, it would be a lot easier if you did it all in Xaml and accessed the objects programmatically in the Code Behind.

For a litle primer on how to do it with Balder: 
Xaml support

Loading content

 

You'll find all the tutorials here

 

The TargetDevice thing will go away shortly, as it is no longer needed.