This is possibly "by design" (i.e. I don't know) but I've noticed that if I reparent a Silverlight control from one Canvas to another then its Loaded event fires for a second time when it is reparented.
So, with a user control such as;
public class UserControl1 : Control
{
public UserControl1()
{
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("SilverlightProject6.UserControl1.xaml");
this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
this.Loaded += new EventHandler(UserControl1_Loaded);
}
void UserControl1_Loaded(object sender, EventArgs e)
{
}
}
with a XAML definition of;
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640"
Height="480"
Background="White"
>
</Canvas>
if I then have a Page that uses this as in;
<Canvas x:Name="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Page_Loaded"
x:Class="SilverlightProject6.Page;assembly=ClientBin/SilverlightProject6.dll"
xmlns:ctl="clr-namespace:SilverlightProject6;assembly=ClientBin/SilverlightProject6.dll"
Width="640"
Height="480"
Background="White"
>
<Canvas x:Name="c1" Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" Background="Red" MouseLeftButtonUp="OnMouseDown">
<ctl:UserControl1 x:Name="ctl1"/>
</Canvas>
<Canvas x:Name="c2">
</Canvas>
</Canvas>
and some code that does the re-parenting;
public partial class Page : Canvas
{
public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
}
private void OnMouseDown(object sender, MouseEventArgs args)
{
this.c1.Children.Remove(this.ctl1);
this.c2.Children.Add(this.ctl1);
}
}
then I find that at the point where I move the control from the c1 Canvas to the c2 Canvas the Loaded event on the control fires for the second time.
As I say, that may well be "by design" as I can see why you might want to know about such a big event in your lifetime as a control but it's worth being aware of.
Posted
Thu, Jul 26 2007 2:16 AM
by
mtaulty