![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | Connecting a C# code behind file to the declarations of controlsin XAML. Hi all, I'm trying to develop a TicTacToe example as show in the Sells O'Reilly book, Chapter 5. He's just updated his sample code from his book to make the examples compatible with the November CTP. I've just tried typing out one of his simple examples and it keeps crashing for me, however downloading his example code and running it works fine. I've traced the difference to the following method: void System.Windows.Serialization.IComponentConnector.Connect(int connectionId, object target) which is found in the generated .g.cs file for my Window. When I create a new project in VS 2005, it gives me an Application (MyApp) and a window (Window1), each with a .xaml file and a .xaml.cs code behind file. Let's say I add a few buttons to the window using XAML: <Button Grid.Row="0" Grid.Column="0" x:Name="cell00" /> <Button Grid.Row="0" Grid.Column="1" x:Name="cell01" /> <Button Grid.Row="0" Grid.Column="2" x:Name="cell02" /> I also have some code in the constructor of the Window1.xaml.cs file that attempts to access those buttons in C# code: public Window1() { InitializeComponent(); this.cells = new Button[] { this.cell00, this.cell01, this.cell02 }; foreach (Button cell in cells) { // do something with the button } } When the application runs, a method is created in the Window1.g.cs file: void System.Windows.Serialization.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: this.cell00 = ((System.Windows.Controls.Button)(target)); return; case 2: this.cell01 = ((System.Windows.Controls.Button)(target)); return; case 3: this.cell02 = ((System.Windows.Controls.Button)(target)); return; } this._contentLoaded = true; } The purpose of this method seems to be to "wire" the buttons instantiated in XAML to references in .g.cs so that you can use those references in your C# code behind. All good, right? So here's the problem. In the example from the Sell's book, the Connect() gets invoked when InitializeComponent() is called. I can't find the actual method call, I guess it's buried deep the WPF framework somewhere. However, in my own example, Connect() doesn't seem to be invoked until after the constructor of my class returns. What this means is that I am unable to reference any of the controls declared in XAML from the constructor of the C# code behind file. Can anyone think of what would account for the difference? I've scrutinized the Window1.xaml.cs and Window1.g.cs files and can't find any differences. There must be some parameter somewhere that Sells set, but I don't know about... Regards, Jason |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Connecting a C# code behind file to the declarations of controls in XAML. Jason Dolinger wrote: > I also have some code in the constructor of the Window1.xaml.cs file > that attempts to access those buttons in C# code: That is your problem right there. You cannot touch the buttons until the Window's Loaded event fires (usually you hook up in that event). It's fired after the control tree is deserialized, anytime before that is too soon. HTH, Drew |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Connecting a C# code behind file to the declarations of controlsin XAML. Drew Marsh wrote: > Jason Dolinger wrote: > >> I also have some code in the constructor of the Window1.xaml.cs file >> that attempts to access those buttons in C# code: > > > That is your problem right there. You cannot touch the buttons until the > Window's Loaded event fires (usually you hook up in that event). It's > fired after the control tree is deserialized, anytime before that is too > soon. > > HTH, > Drew > > Thanks Drew, that was what I had thought the case was (you'd actually answered that one for me in a previous post). But I clearly have this code example that I downloaded from http://www.sellsbrothers.com/writing/avbook/ where that doesn't seem to be the case. In the window's constructor there is a call to InitializeComponent() (defined in the Window.g.cs file). Somehow that call also calls the Connect() method. If you put a breakpoint in Connect(), it stops there as I try to step over the InitializeComponent() call.) When I code this from scratch, that does not happen and the behavior is as you describe it should be. I'm just trying to understand what could be the possible differences here. Thanks! Jason |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Is it possible to include XAML files into another XAML file? | .NET General | |||