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

C#: Canvas.Loaded not firing

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 03-13-2007   #1 (permalink)
znelson
Guest


 

C#: Canvas.Loaded not firing

I have a Canvas that acts as a background image, let's call this
bgCanvas. I also have another Canvas that contains a TextBlock, let's
call this fgCanvas.

bgCanvas always exists in the scene. An event is raised and as a
result, I create a fgCanvas and add it to bgCanvas's children
collection. Before I add it though, I wire-up fgCanvas.Loaded because
I need to check it's ActualHeight property and you can't do that
before it's calculated. So the idea is, create this canvas, wire up
it's Loaded event, then add it and wait for the event to fire, then
retrieve ActualHeight.

Problem is Loaded never fires. I can visually see fgCanvas on the
screen so Load must have occurred right? But the handler is never
invoked.

I tried LayoutUpdated but that fires way too often and it's a plain
EventHandler with sender always null. I wish there was a
SizeCalculated event or something.. any ideas?


My System SpecsSystem Spec
Old 03-13-2007   #2 (permalink)
znelson
Guest


 

Re: C#: Canvas.Loaded not firing

I worked around this by simplifying and such. I have the bgCanvas and
I subclassed TextBlock which will lay on top. In the subclass I
override OnRenderSizeChanged() and raise a custom event. This gets me
the ActualHeight as soon as it's calculated.

I didn't plan on having to create a custom eventhandler delegate and
event args but unfortunately OnRenderSizeChanged() uses
SizeChangedInfo yet RenderSizeEventHandler uses RenderSizeEventArgs
and the two are not interchangeable. Not only that but when I tried to
instantiate a RenderSizeEventArg I got an error saying there is no
constructor defined.. WHAT?

So in the end I had to roll my own TextBlock subclass, eventargs, and
eventhandler. But it works and that's all that matters right?

I'd still like to know why that Canvas was being rendered on the
screen but Loaded was never firing....

My System SpecsSystem Spec
Old 03-23-2007   #3 (permalink)
WPCoder
Guest


 

Re: C#: Canvas.Loaded not firing

I'm not 100% sure what you're doing (so I may not be answering your question), but in a quick test with a Canvas called bgCanvas already on the form:

public partial class Window1
{
private Canvas fgCanvas;

public Window1()
{
this.InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
fgCanvas = new Canvas();
fgCanvas.Loaded += new RoutedEventHandler(fgCanvas_Loaded);
fgCanvas.Width = 100;
fgCanvas.LayoutUpdated += new EventHandler(fgCanvas_LayoutUpdated);
bgCanvas.Children.Add(fgCanvas);
}

void fgCanvas_LayoutUpdated(object sender, EventArgs e)
{
txtSize.Text = fgCanvas.RenderSize.ToString();
}

void fgCanvas_Loaded(object sender, RoutedEventArgs e)
{
Console.WriteLine("Loaded");
}
}

Both the Loaded and LayoutUpdated events are raised as expected. The sender arg is null though in the LayoutUpdated event -- so I needed to store a reference to the fgCanvas object as a private member variable. The txtSize TextBox I added gets the value of "100,0" when I click a button on the window.

What I'm mostly confused about is that it seems like you're trying to get a size for the Canvas after layout? The Canvas normally doesn't size -- it's content can extend beyond it's boundaries without clipping, so it's size is normally irrelevant.

I don't see any RenderSizeEventHanlder in the 3.0 documentation anywhere .... ?

http://www.wiredprairie.us/journal



"znelson" <znelson@gmail.com> wrote in message news:1173838305.954092.84480@n76g2000hsh.googlegroups.com...
>I worked around this by simplifying and such. I have the bgCanvas and
> I subclassed TextBlock which will lay on top. In the subclass I
> override OnRenderSizeChanged() and raise a custom event. This gets me
> the ActualHeight as soon as it's calculated.
>
> I didn't plan on having to create a custom eventhandler delegate and
> event args but unfortunately OnRenderSizeChanged() uses
> SizeChangedInfo yet RenderSizeEventHandler uses RenderSizeEventArgs
> and the two are not interchangeable. Not only that but when I tried to
> instantiate a RenderSizeEventArg I got an error saying there is no
> constructor defined.. WHAT?
>
> So in the end I had to roll my own TextBlock subclass, eventargs, and
> eventhandler. But it works and that's all that matters right?
>
> I'd still like to know why that Canvas was being rendered on the
> screen but Loaded was never firing....
>

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView firing way to many SelectionChangedEvents! Please Help Simon Harvey .NET General 5 08-11-2008 10:43 AM
Linkbutton firing every other time issue mazdotnet .NET General 0 03-30-2008 06:50 PM
Help sound firing out external and headset. Annoying my vanpool. Magoo Vista General 0 09-29-2007 02:39 PM
Triggers Firing for Child Objects gregbacchus@nospam.nospam Avalon 0 05-14-2007 03:59 PM
firing noise in wmp11 Neerav Kothari Vista General 2 04-03-2007 03:32 PM


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