今天在新建一个Silverlight项目时,发现原来的自适应窗口大小的功能不好使了!这是为什么腻?
出错代码如下:

Code
public Page()

{
InitializeComponent();

SizeChanged += new SizeChangedEventHandler(Page_SizeChanged);

this.Width = 1000;
this.Height = 600;
}

void Page_SizeChanged(object sender, SizeChangedEventArgs e)

{//断点设在这里可以发现,只有在初始化的时候,才会走到这里,以后无论如何改变窗口大小都不会走!
}
改后为:

Code
public Page()

{
InitializeComponent();

SizeChanged += new SizeChangedEventHandler(Page_SizeChanged);

LayoutRoot.Width = 1000;//问题所在:是LayoutRoot,不是this.
LayoutRoot.Height = 600;
}

void Page_SizeChanged(object sender, SizeChangedEventArgs e)

{//断点设在这里,真的每次改变窗口大小都会走到这里,然后就可以实现想要的自适应的效果了!
}
注意:xaml中的usercontrol当然也不能设大小!否则一样不好使啊!
总结:要想走SizeChanged事件,必须要改变usercontrol的大小,如果不设任何值,usercontrol的大小会随着窗口大小而改变的!相反地,无论怎么样它都是固定的值...