我使用C#和Windows Phone 8.1作为通用应用程序。
我这篇文章所使用的后台任务: http://www.romasz.net/how-to-add-a-backgroundtask/
我想,当我的应用程序在后台,打开一个网站,并抓住html源代码。 我用这个代码(在后台项目):
public async void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine("Background started\nRetrieving data");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://mysite"));
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
string pageSource = new StreamReader(response.GetResponseStream()).ReadToEnd();
pageSource = (System.Net.WebUtility.HtmlDecode(pageSource));
Debug.WriteLine("source: " + pageSource);
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList textElements = toastXml.GetElementsByTagName("text");
textElements[0].AppendChild(toastXml.CreateTextNode("My first Task - Yeah"));
textElements[1].AppendChild(toastXml.CreateTextNode("source: " + pageSource));
ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
}
这是我调试输出背景:
'BACKGROUNDTASKHOST.EXE' (CoreCLR: DefaultDomain): Loaded 'C:\windows\system32\mscorlib.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\Data\SharedData\PhoneTools\AppxLayouts\cc0fe12e-67d1-47c2-be7b-58b7bf08691fVS.Debug_AnyCPU.myUser\MyTask.winmd'. Symbols loaded.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.RUNTIME.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\WinMetadata\Windows.winmd'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.THREADING.TASKS.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\System.Net.Requests.ni.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\System.Net.ni.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.IO.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.RUNTIME.EXTENSIONS.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.DIAGNOSTICS.DEBUG.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Background started
Retrieving data
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\System.Runtime.WindowsRuntime.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\en-US\mscorlib.debug.resources.dll'. Module was built without symbols.
The program '[3620] BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1).
它的发射,但不能访问我的网站。 请帮我。
谢谢。