silverlight自适应屏幕有很多方法,我自己在网上搜索了一些,并对此进行改进和完善。
第一种方法:一般来说可以捕捉resize事件,然后做变换。
第一种方法:一般来说可以捕捉resize事件,然后做变换。
public MainPage()
{
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
}
void Content_Resized(object sender, EventArgs e)
{
ScaleTransform tt = new ScaleTransform();
double width =0;
double height = 0;
if (!App.Current.Host.Content.IsFullScreen)
{
if (width != 0 && height != 0)
{
//浏览器得宽高
width = App.Current.Host.Content.ActualHeight / width;
height = App.Current.Host.Content.ActualWidth / width;
this.RenderTransform = tt;
}
else
{
width = App.Current.Host.Content.ActualWidth;
height = App.Current.Host.Content.ActualHeight;
}
}
tt.ScaleY = height;
tt.ScaleX = width;
InitializeComponent();
}
{
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
}
void Content_Resized(object sender, EventArgs e)
{
ScaleTransform tt = new ScaleTransform();
double width =0;
double height = 0;
if (!App.Current.Host.Content.IsFullScreen)
{
if (width != 0 && height != 0)
{
//浏览器得宽高
width = App.Current.Host.Content.ActualHeight / width;
height = App.Current.Host.Content.ActualWidth / width;
this.RenderTransform = tt;
}
else
{
width = App.Current.Host.Content.ActualWidth;
height = App.Current.Host.Content.ActualHeight;
}
}
tt.ScaleY = height;
tt.ScaleX = width;
InitializeComponent();
}
总结:用这种方法可以自适应各种屏幕分辨率和屏幕的大小。缺点是屏幕中的布局也会跟着屏幕的大小而进行位置调整,所以在布局时一定要注意。
第二种方法:去掉<usercontrol ></usecontrol>的width和hight。
总结:使用Grid布局,页面的布局便会随屏幕大小变化而变化,控件的Margin可以全设置为0。
缺点是图片不会随着分辨率不同而改变,当屏幕比图片的尺寸要小时,会出现滚动条。
布局时可以使用StackPanel,给StackPanel设置宽和高,这样页面的布局可以固定些。
第三种方法:采用StackPanel自适应分辨率。
他的好处是图片也可以随着分辨率不同大小也不一样。