1
votes

I upgraded a windows phone 8 app to 8.1 silverlight. But while working with geofencing I wanted to show a toast. So I wrote this code

public sealed class Task : IBackgroundTask
    {
        static string TaskName = "GeofenceTask";
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Get the information of the geofence(s) that have been hit
            var reports = GeofenceMonitor.Current.ReadReports();
            var report = reports.FirstOrDefault(r => (r.Geofence.Id == "sample") && (r.NewState == GeofenceState.Entered));

            if (report == null) return;

            // Create a toast notification to show a geofence has been hit
            var toastXmlContent = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

            var txtNodes = toastXmlContent.GetElementsByTagName("text");
            txtNodes[0].AppendChild(toastXmlContent.CreateTextNode("Geofence triggered toast!"));
            txtNodes[1].AppendChild(toastXmlContent.CreateTextNode(report.Geofence.Id));

            var toast = new ToastNotification(toastXmlContent);

            var toastNotifier = ToastNotificationManager.CreateToastNotifier();

            toastNotifier.Show(toast);
        }
}

But during certification it says

This API is not supported for this application type - Api=Windows.UI.Notifications.ToastNotification. Module=. File=GeofenceTask.winmd.

After which I did some research and found this answer which says

you have to select WNS if you want to use Windows.UI.Notifications otherwise you have the use ShellToast

So I was using MPN because Azure mobile service does not support WNS for WP8.1 silverlight apps.

Notification Hubs Windows Phone SDK does not support using WNS with Windows Phone 8.1 Silverlight apps.

Source

So I thought I should use ShellToast in that case. But now I reached the real dead end. ShellToast is not supported in Windows runtime component projects. I dont have the right reference needed. i.e., Microsoft.Phone.Shell. If I add

Using Microsoft.Phone.Shell; 

'Phone' shows with red underline. What should I do?

Update:

I tried to add the Microsoft.phone.dll from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\WindowsPhone\v8.0

but I got an error where the ToastShell was defined which says:

An exception of type 'System.InvalidProgramException' occurred in GeofenceTask.winmd but was not handled in user code

Additional information: Common Language Runtime detected an invalid program.

1

1 Answers

1
votes

So you need another assembly that contains the classes you need from the namespace Microsoft.Phone.Shell? I googled and found on msdn ShellToast Class where an assembly is stated:

Assembly: Microsoft.Phone (in Microsoft.Phone.dll)

and

Version Information: Windows Phone OS Supported in: 8.1, 8.0, 7.1

You have to add this dll to you project references. And it comes with WP OS so you can just reference it without having to bundle it with your app, so set Copy Local: False

[Edit] Maybe you added the wrong assembly? The path you have posted in the comments to my answer seems wrong to me. Look for it in the following folder:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\Microsoft.Phone.dll