Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts 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

Accessing items in XAML from code

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


 

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)
Griff
Guest


 

RE: Accessing items in XAML from code

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 ??
--
Griff
(trying to make an industrial UI with XAML/WPF/c#)


"Griff" wrote:

> 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   #3 (permalink)
Jason Dolinger
Guest


 

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   #4 (permalink)
Rob Relyea [MSFT]
Guest


 

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

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing EventArgs of RoutedEvent in XAML Jens Weiermann Avalon 1 06-30-2008 06:33 AM
problem in accessing a wcf service through wpf-xaml ram Avalon 0 04-19-2007 03:06 AM
XAML code libraries? Jim Galasyn Avalon 0 05-13-2006 11:58 AM
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