繁体   English   中英

带有后台任务的Windows Phone 8.1 SmartCard应用崩溃

[英]Windows Phone 8.1 SmartCard app with background task crashing

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

开始时,我制作了一个空白的Windows Phone App项目和一个Windows运行时组件 ,并将它们添加到解决方案中。 Phone App Project引用了Windows Runtime Component Project。

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

Windows Server应用程序项目中有BackgroundTaskManager类,运行时组件项目中有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

我尝试调试 ,并将中断点放在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;
        }

    }
}

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


    }
}

有人看到我在做什么错吗? 谢谢阅读。 格里茨,斯基皮

编辑:

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

你在那里有错误代码

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM