문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top