[英]How to get screen resolution for WinPhone 8 from a background thread
我正在创建一个库。 我们需要的一件事是手机的屏幕分辨率(以像素为单位)
我们成功地使用了这种方法
Screen.Width = (int) System.Windows.Application.Current.Host.Content.ActualWidth;
Screen.Height = (int) System.Windows.Application.Current.Host.Content.ActualHeight;
但是我们不处理后台线程调用此方法的情况,因此我们将其更改为使用Dispatcher:
System.Windows.Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
{
Screen.Width = (int) System.Windows.Application.Current.Host.Content.ActualWidth;
Screen.Height = (int) System.Windows.Application.Current.Host.Content.ActualHeight;
});
但是,我们抛出了“无效的跨线程访问”异常,似乎只是在使用BeginInvoke。
如何在不引用当前呈现的XAML页面的情况下正确处理此问题?
只是访问Application.Current.RootVisual
会引发无效的跨线程访问异常,因此您无法以这种方式访问调度程序。 而是使用System.Windows.Deployment.Current.Dispatcher
:
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Screen.Width = (int) System.Windows.Application.Current.Host.Content.ActualWidth;
Screen.Height = (int) System.Windows.Application.Current.Host.Content.ActualHeight;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.