Silverlight Tips of the Day 系列翻译与领悟#9
Silverlight Tip of the Day #9 – How to Capture the Size of your Browser when it is Resized.
在这章中,我将告诉你当浏览器改变大小时如何获取它的大小。当你想把你的元素对齐到浏览器的边框上,或者在屏幕上居中显示你的人物,等等,这都是必要的工作。
为了完成这项工作,我们简单地为 App.Current.Host.Content 对象附加一些改变尺寸的事件。
Page.xaml.cs:
public Page()
{
InitializeComponent();
App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
}
从这里,我们可以获取到浏览器改变时的尺寸。
void Content_Resized(object sender, EventArgs e)
{
double height = App.Current.Host.Content.ActualHeight;
double width = App.Current.Host.Content.ActualWidth;
}