问题
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