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 - C#: Canvas.Loaded not firing

 
 
Old 03-13-2007   #1 (permalink)
znelson


 
 

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


 
 

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


 
 

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
 

Thread Tools


Similar Threads
Thread Forum
dynamic loaded ascx delegate event not firing in parent form .NET General
DataGridView firing way to many SelectionChangedEvents! Please Help .NET General
Linkbutton firing every other time issue .NET General
firing noise in wmp11 Vista General


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