UnauthorizedAccessException在初始化ProximityDevice(Windows手机)初始化、手机、UnauthorizedAccessException、Windows

2023-09-06 11:27:25 作者:纵饮孤独

我想用 ProximityDevice 在我的应用程序。我若为外地​​或在构造函数中,像这样的:

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(); 
    }
}

或这样的:

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

如果我把它声明为静态code使没有问题:

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

如何解决这个问题?

How to fix that problem?

推荐答案

请确保您添加的ID_CAP_PROXIMITY能力,你的应用程序清单。

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