ShellToast通知没有出现windows phone c#

时间:2015-03-18 14:43:51

标签: c# multithreading windows-phone-8 windows-phone toast

我有一个periodicTask运行,连接到网络,上传一些数据,完成后我想展示一个shelltoast。

这似乎不适合我。如果我在不调用WebRequest的情况下调用shellToast,它就可以工作,所以我猜它是一个线程问题。不确定如何解决它

TASK

protected override void OnInvoke(ScheduledTask task)
{
    if (DeviceNetworkInformation.IsWiFiEnabled)
    {
        if (!DeviceNetworkInformation.IsCellularDataEnabled && DeviceNetworkInformation.IsNetworkAvailable)
        {
            // check if there are any scorecards to upload
            var service = new ApiService();

            service.UploadCompleted += UploadCompleted;
            service.UploadFailed += UploadFailed;

            .. do some processing and populate results variable

            _itemCount = results.Count;
            _hasItems = (_itemCount > 0);

            foreach (var item in results)
            {
                service.Upload(item);
            }
        }
    }

    // only call this if there was nothing in the queue, or whether no internet connection
    if (!_hasItems)
    {
        NotifyComplete();
    }

}

public void UploadFailed(object sender, UploadResponse e)
{
    // ... do some processing

    // complete
    ItemCompleted("Upload failed", "Item 'REF' failed at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
}

public void UploadCompleted(object sender, UploadResponse e)
{
    // complete
    ItemCompleted("Upload success", "Item 'REF' uploaded at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
}

public void ItemCompleted(string title, string content)
{
    // show message to user
    var toast = new ShellToast();
    toast.Title = title;
    toast.Content = content;
    toast.Show();

    // decrease count
    lock (countLock)
    {
        _itemCount--;
    }

    // if none left, we are done
    if (_itemCount == 0) NotifyComplete();
}

服务上传

public void Upload(int id)
{
    var response = new UploadResponse();

    try
    {
        // create our request with our data
        JObject data = new JObject();
        // add values to data object

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("my.url.com");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/json;charset=utf-8";

        //we first obtain an input stream to which to write the body of the HTTP POST
        webRequest.BeginGetRequestStream((IAsyncResult result) =>
        {
            HttpWebRequest preq = result.AsyncState as HttpWebRequest;

            if (preq != null)
            {
                Stream postStream = preq.EndGetRequestStream(result);

                // convert to bytes
                byte[] dataAsBytes = System.Text.Encoding.UTF8.GetBytes(data.ToString());

                postStream.Write(dataAsBytes, 0, dataAsBytes.Length);
                postStream.Close();

                // finalize the request
                preq.BeginGetResponse((IAsyncResult final_result) =>
                {
                    HttpWebRequest req = final_result.AsyncState as HttpWebRequest;
                    if (req != null)
                    {
                        try
                        {
                            //we call the success callback as long as we get a response stream
                            WebResponse webResponse = req.EndGetResponse(final_result);

                            using (Stream responseStream = webResponse.GetResponseStream())
                            {
                                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
                                JObject jsonResponse = JObject.Parse(reader.ReadToEnd());
                                string message = jsonResponse.Value<string>("Message");

                                // now if we have a response, wicked! lets check it
                                if (message.Equals("Item Added"))
                                {
                                    // raise event
                                    UploadCompleted(this, response);
                                }
                                else
                                {
                                    response.Message = message;

                                    // raise event
                                    UploadFailed(this, response);
                                }
                            }
                        }
                        catch (WebException e)
                        {
                            //otherwise call the error/failure callback
                            response.Message = e.Message;

                            // raise event
                            UploadFailed(this, response);
                        }
                    }
                }, preq);
            }
        }, webRequest);
    }
    catch (Exception e)
    {
        response.Message = e.Message;

        UploadFailed(this, response);
    }
}

0 个答案:

没有答案
相关问题