UnauthorizedAccessException while initializing ProximityDevice (Windows Phone)

一笑奈何 提交于 2019-12-11 11:46:38

问题


I want to use ProximityDevice in my application. If I declare it as field or in the constructor, like this:

public partial class MainPage : PhoneApplicationPage {
    // Constructor
    public ProximityDevice device;
    public MainPage() {
        InitializeComponent();
        device = ProximityDevice.GetDefault(); 
    }
}

or this:

public partial class MainPage : PhoneApplicationPage {
    // Constructor

    public MainPage() {
        InitializeComponent();
       public ProximityDevice device = ProximityDevice.GetDefault(); 
    }
}

I get

A first chance exception of type 'System.UnauthorizedAccessException' occurred in SecurityClient.DLL
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.Phone.ni.dll

If I declare it as static code causes no problems:

 public partial class MainPage : PhoneApplicationPage {
        // Constructor
        public static  ProximityDevice device = ProximityDevice.GetDefault();
        public MainPage() {
            InitializeComponent();       
        }
    }

How to fix that problem?


回答1:


Make sure you added the ID_CAP_PROXIMITY capability to your application manifest.



来源:https://stackoverflow.com/questions/21239536/unauthorizedaccessexception-while-initializing-proximitydevice-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!