如何在后台任务中保存原始通知,然后从主项目访问它

时间:2015-08-31 12:37:19

标签: push-notification windows-phone-8.1

我很久以来一直坚持这个。

我收到原始通知并生成祝酒词:

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferral = taskInstance.GetDeferral();

        BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
        RawNotification rn = taskInstance.TriggerDetails as RawNotification;
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
        var toastDescriptor = ToastNotificationManager.GetTemplateContent(toastTemplate);
        var txtNodes = toastDescriptor.GetElementsByTagName("text");
        txtNodes[0].AppendChild(toastDescriptor.CreateTextNode("RAW"));
        txtNodes[1].AppendChild(toastDescriptor.CreateTextNode(rn.Content));
        var toast = new ToastNotification(toastDescriptor);
        var toastNotifier = ToastNotificationManager.CreateToastNotifier();
        toastNotifier.Show(toast);

        await writeToFile(rn);
        deferral.Complete();


    }

然后我将原始通知写入文件:

private async Task writeToFile(RawNotification rn)
    {
        var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
      FILENAME, CreationCollisionOption.OpenIfExists);

        using (var netStream = await file.OpenStreamForWriteAsync())
        {
            using (var writer = new StreamWriter(netStream, Encoding.UTF8, 512, true))
            {
              writer.WriteLine(rn.Content.ToString());
            }
        }
     }

        static readonly string FILENAME = "background.txt";
  }

对于一个单一的通知它工作正常,但如果我尝试创建所有通知的列表并将其发送到listView的主项目,它不会!

public static IAsyncOperation<IList<string>> ReadLastRunTimeAsync()
    {
        return (ReadFromFileAsync().AsAsyncOperation());
    }
    static async Task<IList<string>> ReadFromFileAsync()
    {
        string runtime = "Not Run";
        IEnumerable<string> collection = null;
        //ObservableCollection<string> list = new ObservableCollection<string>();

        try
        {
            var file = await ApplicationData.Current.LocalFolder.GetFileAsync(FILENAME);

            using (var netStream = await file.OpenStreamForReadAsync())
            {
                using (var reader = new StreamReader(netStream))
                {
                    //runtime = reader.ReadToEnd();
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        collection.Concat(new[] { line }); // Add to list.
                        //reader.WriteLine(line); // Write to console.
                    }
                }
            }
        }
        catch (FileNotFoundException)
        {

        }
        return (collection.ToList());
    }

请帮忙。 如果有其他方式,请指导我。 我想要的只是收到的所有通知的列表视图。

0 个答案:

没有答案