Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Accessing items in XAML from code

 
 
Old 01-11-2006   #1 (permalink)
Griff


 
 

Accessing items in XAML from code

This is going to seem really daft, but what is wrong with the following ?
(It causes a runtime BAML exception with no useful feedback when the code
behind tries to set the button property).

----Window1.xaml----------------
<Window x:Class="TestOfControl.Window1"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
Title="TestOfControl"
>

<Grid >
<Button x:Name="TestButton"/>
</Grid>
</Window>
------------------Window1.xaml.cs---------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TestOfControl
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.TestButton.Content = "hello";
}
}
}

I have left out a load of mapping stuff in this example but having it in
does not seem to fix it.
Should I wait until the Window is loaded before trying to set properties of
Window contents ?

I have an example which is basically the same (though a lot too messy to
post) that seems to work (in which I set the opacity of a named canvas from
the code behind) so I'm puzzled as to what might be wrong here.

I'm sure it's something completely daft.

Thanks in advance.
--
Griff
(trying to make an industrial UI with XAML/WPF/c#)

My System SpecsSystem Spec
Old 01-11-2006   #2 (permalink)
Jason Dolinger


 
 

Re: Accessing items in XAML from code

Griff wrote:
> Ok, I have realised what is wrong now.
>
> If I hand a method off the "Loaded" event it permits me to do this.
> So ,my problem is that I am trying to access objects before they have had a
> chance to be instantiated.
> I wonder if 99% of my BAML exceptions are actually because of this issue ??


I had the same problem for awhile until Drew Marsh set me straight. You
definitely can not access any of those references to your XAML objects
from your C# code within the constructor in the code behind file. You
must wait until the window, app, whatever has been loaded. If you look
at the .g.cs file that gets generated you'll see a method called

void System.Windows.Serialization.IComponentConnector.Connect(int
connectionId, object target) {}

In this method, the WPF framework hooks up the actual objects to the
internal references declared in the same file (these are the references
you actually use in your own C# code behind).

The (int connectionId, object target) method doesn't get called until
after the constructor for your App or Window is called.

Jason
My System SpecsSystem Spec
Old 01-12-2006   #3 (permalink)
Rob Relyea [MSFT]


 
 

Re: Accessing items in XAML from code

We hope to be able to fix this. We don't like it, but want to make the tree
available after initialize component.
It only happens if you navigate to the page. Using StartupUri in MyApp.xaml
uses a navigation code path.

If you use the way the old templates did it...listening to the startup event
on application and instantiating a window, you can write code in the
constructor that modifies the tree.

Sorry for the problems...hope to fix in the future :-)

Thx, Rob
"Jason Dolinger" <jdolinger@lab49.com> wrote in message
news:u3BwM3sFGHA.1124@TK2MSFTNGP10.phx.gbl...
> Griff wrote:
>> Ok, I have realised what is wrong now.
>>
>> If I hand a method off the "Loaded" event it permits me to do this.
>> So ,my problem is that I am trying to access objects before they have had
>> a chance to be instantiated.
>> I wonder if 99% of my BAML exceptions are actually because of this issue
>> ??

>
> I had the same problem for awhile until Drew Marsh set me straight. You
> definitely can not access any of those references to your XAML objects
> from your C# code within the constructor in the code behind file. You
> must wait until the window, app, whatever has been loaded. If you look at
> the .g.cs file that gets generated you'll see a method called
>
> void System.Windows.Serialization.IComponentConnector.Connect(int
> connectionId, object target) {}
>
> In this method, the WPF framework hooks up the actual objects to the
> internal references declared in the same file (these are the references
> you actually use in your own C# code behind).
>
> The (int connectionId, object target) method doesn't get called until
> after the constructor for your App or Window is called.
>
> Jason



My System SpecsSystem Spec
 

Thread Tools



Vista Forums 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 Ltd

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