0

我有一个问题要解决一个多星期,但我无法解决。我正在尝试用 nfc 和智能卡的东西制作一个非常简单的 Windows Phone 8.1 应用程序。后来我尝试使用 SmartCardEmulator 类提供的智能卡仿真,但现在我只想有一个后台任务,在接近 nfc 标签/智能卡时执行操作。对于这个问题,我制作了一个完整的新项目,其中仅包含基础知识。当我按下按钮时,程序不断崩溃。

开始时,我创建了一个空白的 Windows Phone 应用程序项目和一个Windows 运行时组件,并将其添加到解决方案中。Phone App Project 引用了 Windows 运行时组件项目。

MainPage.xaml我只放了一个按钮和一个文本框。

BackgroundTaskManager 类位于 windows phone 应用程序项目中,TaskOne 类位于运行时组件项目中。

在 Package.appxmanifest 文件中,我在声明中添加了后台任务,其中BackgroundTasks.TaskOne 作为入口点系统事件作为支持任务类型

要求中我启用了 NFC

按下按钮时出现的错误如下:

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.UnauthorizedAccessException' occurred in SimpleNfcApp.exe
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll

我尝试调试并将断点放在 BackgroundTaskManager.cs 中 RegisterBackgroundTask 方法的开头,然后在 MainPage.xaml.cs 中的 doSomething 结束时崩溃

以下是源代码:

MainPage.xaml.cs

namespace SimpleNfcApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage() {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e) {
        }

        // method put on a button
        private async void doSomething(object sender, TappedRoutedEventArgs e)
        {
            String taskName = BackgroundTaskManager.TaskOneName;
            bool isActive = isTaskActive(taskName);

            if(isActive == false) {
                startTask();
            } else {
                myTextBox.Text = "Task already active";
            }
        }

        // starts a task
        private async void startTask()
        {
            var trigger = new SmartCardTrigger(SmartCardTriggerType.EmulatorNearFieldEntry);
            var task = BackgroundTaskManager.RegisterBackgroundTask(BackgroundTaskManager.TaskOneEntryPoint,
                                                                    BackgroundTaskManager.TaskOneName,
                                                                    trigger,
                                                                    null);
            await task;
            myTextBox.Text += task.Result.ToString();
        }

        // checks if the task is already active
        private static bool isTaskActive(String name)
        {
            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == name)
                    return true;
            }
            return false;
        }

    }
}

背景任务管理器.cs

namespace SimpleNfcApp
{
    class BackgroundTaskManager
    {
        public const string TaskOneEntryPoint = "BackgroundTasks.TaskOne";
        public const string TaskOneName = "TaskOne";
        public static string TaskOneProgress = "";
        public static bool TaskOneRegistered = false;

        // creates and registers the task
        public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String taskName, IBackgroundTrigger taskTrigger, IBackgroundCondition taskCondition)
        {
            if (TaskRequiresBackgroundAccess(taskName))
                await BackgroundExecutionManager.RequestAccessAsync();

            var builder = new BackgroundTaskBuilder();

            builder.Name = taskName;
            builder.TaskEntryPoint = taskEntryPoint;
            builder.SetTrigger(taskTrigger);

            BackgroundTaskRegistration task = builder.Register();

            var settings = ApplicationData.Current.LocalSettings;
            settings.Values.Remove(taskName);

            return task;
        }

        // checks if it is a windows phone app or a simple windows app
        public static bool TaskRequiresBackgroundAccess(String name)
        {
            #if WINDOWS_PHONE_APP
                return true;
            #else

            #endif
        }

    }
}

TaskOne.cs

namespace BackgroundTasks
{
    public sealed class TaskOne
    {
        IBackgroundTaskInstance _taskInstance;
        BackgroundTaskDeferral _deferral;

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _taskInstance = taskInstance;
            _deferral = taskInstance.GetDeferral();

            int x;
            x = 2 + 2;

            _deferral.Complete();
        }


    }
}

有谁看到我做错了什么?谢谢阅读。格里兹,斯基皮

编辑:

我在 MainPage.xaml.cs 中的 doSomething 方法周围添加了一个try catch 块,并得到了一条新的更能说明问题的错误消息。

A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   at Windows.ApplicationModel.Background.BackgroundTaskBuilder.Register()
   at SimpleNfcApp.BackgroundTaskManager.<RegisterBackgroundTask>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SimpleNfcApp.MainPage.<startTask>d__0.MoveNext() Second exception caught.

此外,当我在注册任务之前更改 doSomething 中的触发器时,它可以工作。我试过这个:

//var trigger = new SmartCardTrigger(SmartCardTriggerType.EmulatorNearFieldEntry);
var trigger = new SystemTrigger(SystemTriggerType.ServicingComplete, false);
4

1 回答 1

0

你那里有错误代码

var settings = ApplicationData.Current.LocalSettings;
settings.Values.Remove(taskName);

ApplicationData.Current.LocalSettings - 对此文件的访问被拒绝。

于 2015-06-20T19:16:24.620 回答