Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Connecting a C# code behind file to the declarations of controlsin XAML.

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-10-2006   #1 (permalink)
Jason Dolinger
Guest


 

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 SpecsSystem Spec
Old 01-10-2006   #2 (permalink)
Drew Marsh
Guest


 

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 SpecsSystem Spec
Old 01-10-2006   #3 (permalink)
Jason Dolinger
Guest


 

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 SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to include XAML files into another XAML file? star-italia .NET General 6 06-12-2008 04:56 AM
How to deploy an XAML-based App with its XAML-UI-File? Solveigh Avalon 11 11-08-2006 07:50 AM
File.xaml and File.xaml.cs are atomically checked out/in from sourcesafe Pascal Bourque Avalon 1 04-26-2006 06:47 PM
Questions on XAML code behind api Jonathan Nix Avalon 1 01-31-2006 06:59 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51