初始化ProximityDevice时出现UnauthorizedAccessException(Windows Phone)

时间:2014-01-20 16:44:52

标签: c# .net windows-phone unauthorizedaccessexcepti

我想在我的应用程序中使用ProximityDevice。如果我将其声明为字段或在构造函数中,如下所示:

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

或者:

public partial class MainPage : PhoneApplicationPage {
    // Constructor

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

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

如果我将其声明为静态代码会导致没有问题:

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

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

确保已将ID_CAP_PROXIMITY功能添加到应用程序清单。