I have a scenario where an event on a user control that is several layers deep in the control heirarchy needs to change the visibility of another usercontrol. The second control does not have the same direct parent or at the same level but is a child of
the top level "Page.xaml".
Currently I'm just getting a null reference when attempting FindName from teh first User Control indicating that it is not finding the second control. Is there a specific element I need to call FindName on or something else I need to do in order to allow
this interaction to take place?
Someone correct me if I'm wrong, but I'm pretty sure you'll have to use a parent of the second control in order to find it ( in this case use the root element from your Page.xaml ). I believe FindName will only find controls within it's own hierarchy, rather
than on a global scope.
I would just keep a reference to it in a class somewhere ( I have a singleton "GameInfo" class I use to keep track of things that most other classes will probably need to use like the root canvas, etc. ). So in the Page_Loaded event for the main canvas I setup
the GameInfo class with the necessary values so everything else can reference them later in code.
That sounds like it would work perfectly for me, thanks a ton. Here's a (potentially stupid) follow-up question, how do I scope the object so that it is available to all other classes, like your "GameInfo" class?
The way I did it was making the class a singleton. Here's the entire class ( hopefully the forum doesn't butcher the formatting too much :) :
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Game
{
public class GameInfo
{
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// SINGLETON!
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
private static GameInfo gameInfoInstance = new GameInfo();
public static GameInfo Get()
{
return gameInfoInstance;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// MEMBER VARIABLS
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// Screen size
static public double SCREEN_WIDTH = 800.0;
static public double SCREEN_HEIGHT = 600.0;
// Main canvas object
public Canvas HostCanvas;
// State manager
public StateManager stateManager;
}
}
Then in my main Page.xaml.cs class I have:
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// Page_Loaded()
//
// Main entry point.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
public void Page_Loaded(object o, EventArgs e)
{
// Initialize the game
Init();
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// Init()
//
// Initialize the game
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
public void Init()
{
// Grab reference to main canvas
rootCanvas = (Canvas)FindName("LayoutRoot");
// Setup game info
GameInfo.Get().HostCanvas = rootCanvas;
GameInfo.Get().stateManager = stateManager;
}
Now you can reference the game info anywhere ( as long as it's in the same namespace of course ) like so:
Thanks a ton, that looks like a similar construct will help me out. What was throwing me was not having to explicitly scope the object (can you tell I'm an ASP.NET guy?).
johnnystock
Contributor
2295 Points
362 Posts
Question about FindName
Mar 14, 2008 12:08 PM | LINK
I have a scenario where an event on a user control that is several layers deep in the control heirarchy needs to change the visibility of another usercontrol. The second control does not have the same direct parent or at the same level but is a child of the top level "Page.xaml".
Currently I'm just getting a null reference when attempting FindName from teh first User Control indicating that it is not finding the second control. Is there a specific element I need to call FindName on or something else I need to do in order to allow this interaction to take place?
Thanks in advance for any help.
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action
RegularKid
Member
100 Points
39 Posts
Re: Question about FindName
Mar 14, 2008 03:27 PM | LINK
Someone correct me if I'm wrong, but I'm pretty sure you'll have to use a parent of the second control in order to find it ( in this case use the root element from your Page.xaml ). I believe FindName will only find controls within it's own hierarchy, rather than on a global scope.
- Bryan
johnnystock
Contributor
2295 Points
362 Posts
Re: Question about FindName
Mar 14, 2008 04:01 PM | LINK
That's what I was thinking was happening. Any idea how I can get a reference to the root element from an unknown level of layers deep in the tree?
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action
RegularKid
Member
100 Points
39 Posts
Re: Question about FindName
Mar 14, 2008 04:44 PM | LINK
johnnystock
Contributor
2295 Points
362 Posts
Re: Question about FindName
Mar 14, 2008 04:54 PM | LINK
That sounds like it would work perfectly for me, thanks a ton. Here's a (potentially stupid) follow-up question, how do I scope the object so that it is available to all other classes, like your "GameInfo" class?
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action
RegularKid
Member
100 Points
39 Posts
Re: Question about FindName
Mar 14, 2008 05:10 PM | LINK
The way I did it was making the class a singleton. Here's the entire class ( hopefully the forum doesn't butcher the formatting too much :) :
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Game
{
public class GameInfo
{
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// SINGLETON!
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
private static GameInfo gameInfoInstance = new GameInfo();
public static GameInfo Get()
{
return gameInfoInstance;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// MEMBER VARIABLS
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// Screen size
static public double SCREEN_WIDTH = 800.0;
static public double SCREEN_HEIGHT = 600.0;
// Main canvas object
public Canvas HostCanvas;
// State manager
public StateManager stateManager;
}
}
Then in my main Page.xaml.cs class I have:
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// Page_Loaded()
//
// Main entry point.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
public void Page_Loaded(object o, EventArgs e)
{
// Initialize the game
Init();
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// Init()
//
// Initialize the game
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
public void Init()
{
// Grab reference to main canvas
rootCanvas = (Canvas)FindName("LayoutRoot");
// Setup game info
GameInfo.Get().HostCanvas = rootCanvas;
GameInfo.Get().stateManager = stateManager;
}
Now you can reference the game info anywhere ( as long as it's in the same namespace of course ) like so:
GameInfo.Get().HostCanvas
or
GameInfo.Get().SCREEN_WIDTH
etc.
Hope that helps!
- Bryan
johnnystock
Contributor
2295 Points
362 Posts
Re: Question about FindName
Mar 14, 2008 05:22 PM | LINK
Thanks a ton, that looks like a similar construct will help me out. What was throwing me was not having to explicitly scope the object (can you tell I'm an ASP.NET guy?).
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action