UnauthorizedAccessException在初始化ProximityDevice(Win

2019-10-19 06:13发布

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

如何解决这个问题?

Answer 1:

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



文章来源: UnauthorizedAccessException while initializing ProximityDevice (Windows Phone)