Programming with .NET - Generalhttp://forums.silverlight.net//17.aspx/1?Programming+with+NET+GeneralGeneral discussions around authoring Silverlight .NET applications.Mon, 01 Jan 0001 00:00:00 -05001737115http://forums.silverlight.net//p/11615/37115.aspx/1?Question+about+FindName+Question about FindName <p>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 &quot;Page.xaml&quot;.</p> <p>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.&nbsp; 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?</p> <p>Thanks in advance for any help.</p> 2008-03-14T12:08:27-04:0037155http://forums.silverlight.net//p/11615/37155.aspx/1?Re+Question+about+FindNameRe: Question about FindName <p>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.</p> <p>- Bryan</p> 2008-03-14T15:27:25-04:0037161http://forums.silverlight.net//p/11615/37161.aspx/1?Re+Question+about+FindNameRe: Question about FindName <p>That's what I was thinking was happening.&nbsp; Any idea how I can get a reference to the root element from an unknown level of layers deep in the tree?</p> 2008-03-14T16:01:13-04:0037189http://forums.silverlight.net//p/11615/37189.aspx/1?Re+Question+about+FindNameRe: Question about FindName I would just keep a reference to it in a class somewhere ( I have a singleton &quot;GameInfo&quot; 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.<br> 2008-03-14T16:44:44-04:0037198http://forums.silverlight.net//p/11615/37198.aspx/1?Re+Question+about+FindNameRe: Question about FindName <p>That sounds like it would work perfectly for me, thanks a ton.&nbsp; 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 &quot;GameInfo&quot; class?</p> 2008-03-14T16:54:00-04:0037207http://forums.silverlight.net//p/11615/37207.aspx/1?Re+Question+about+FindNameRe: Question about FindName <p>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 :) :</p> <p><b>using System;<br> using System.Windows;<br> using System.Windows.Controls;<br> using System.Windows.Documents;<br> using System.Windows.Ink;<br> using System.Windows.Input;<br> using System.Windows.Media;<br> using System.Windows.Media.Animation;<br> using System.Windows.Shapes;<br> <br> namespace Game<br> {<br> &nbsp;&nbsp;&nbsp; public class GameInfo<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SINGLETON!<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private static GameInfo gameInfoInstance = new GameInfo();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static GameInfo Get()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return gameInfoInstance;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // MEMBER VARIABLS<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Screen size<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static public double SCREEN_WIDTH = 800.0;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static public double SCREEN_HEIGHT = 600.0;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Main canvas object<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Canvas HostCanvas;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // State manager<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public StateManager stateManager;<br> &nbsp;&nbsp;&nbsp; }<br> }</b><br> </p> <p>Then in my main Page.xaml.cs class I have:</p> <p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <b>//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Page_Loaded()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Main entry point.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void Page_Loaded(object o, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Initialize the game<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Init();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Init()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Initialize the game<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void Init()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Grab reference to main canvas<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rootCanvas = (Canvas)FindName(&quot;LayoutRoot&quot;);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Setup game info<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GameInfo.Get().HostCanvas = rootCanvas;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GameInfo.Get().stateManager = stateManager;</b></p> <p><b>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</b></p> <p>&nbsp;</p> <p>Now you can reference the game info anywhere ( as long as it's in the same namespace of course ) like so:</p> <p><b>GameInfo.Get().HostCanvas</b></p> <p>or</p> <p><b>GameInfo.Get().SCREEN_WIDTH</b></p> <p>etc.</p> <p>Hope that helps!</p> <p>- Bryan&nbsp;</p> 2008-03-14T17:10:35-04:0037216http://forums.silverlight.net//p/11615/37216.aspx/1?Re+Question+about+FindNameRe: Question about FindName <p>Thanks a ton, that looks like a similar construct will help me out.&nbsp; What was throwing me was not having to explicitly scope the object (can you tell I'm an ASP.NET guy?).</p> 2008-03-14T17:22:11-04:00