so I've been programming for a while but Visual Studio just has me stumped on this. I have an application and I need to switch between multiple menus. My method for doing this is (as I've done before in Unity) is group all the elements of each menu into a Canvas GUI element, and using code to set which ones are visible and which are not. I'm just not sure how to reference the canvas itself in code (or the others, since I need multiple ones but I'm starting with one for now)
I've been completely unable to find any documentation on how to do this. It should be simple enough. The only 'answer' I found was for .net 4.0, but I'm using 6.0 and C# in Visual Studio 2022 and the solution does not work.
The only 'solution' I found (which throws an error):
object mainCanvas = Canvas.Findname("Main_Menu");
Can someone tell me how to actually do this, or if I'm just going in the wrong direction or something? (Also why is there no documentation on how to do this? It's common enough to do in Unity but the method theres won't work in Visual Studio)
CodePudding user response:
For a XAML based UI, the way to do this is to assign a name to the Canvas, and then it is accessible in the code-behind.
<Canvas x:Name="myCanvas"/>
Then in the code behind
void SomeEvent(object sender, RoutedEventArgs e)
{
myCanvas...//Do whatever you were going to do with the canvas.
}