Windows Phone 8.1 SmartCard应用程序,后台任务崩溃

时间:2015-03-27 08:00:48

标签: c# windows-phone-8 nfc smartcard background-task

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

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

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

BackgroundTaskManager类位于windows phone app项目中,TaskOne类位于运行时组件项目中。

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

要求我启用了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

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

以下是sourecodes:

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

    }
}

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


    }
}

有谁看到我做错了什么? 谢谢阅读。 greez,Skippy

修改

我在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);

1 个答案:

答案 0 :(得分:0)

那里有错误代码

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

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