Book Image

Microsoft .NET Framework 4.5 Quickstart Cookbook

By : Jose Luis Latorre
Book Image

Microsoft .NET Framework 4.5 Quickstart Cookbook

By: Jose Luis Latorre

Overview of this book

With about ten years since its first release, Microsoft's .NET Framework 4.5 is one of the most solid development technologies to create casual, business, or enterprise applications. It has evolved into a very stable framework and solid framework for developing applications, with a solid core, called the CLR (Common Language Runtime) Microsoft .NET Framework 4.5 includes massive changes and enables modern application and UI development."Microsoft .Net Framework 4.5 Quickstart Cookbook" aims to give you a run through the most exciting features of the latest version. You will experience all the flavors of .NET 4.5 hands on. The “How-to” recipes mix the right ingredients for a final taste of the most appetizing features and characteristics. The book is written in a way that enables you to dip in and out of the chapters.The book is full of practical code examples that are designed to clearly exemplify the different features and their applications in real-world development. All the chapters and recipes are progressive and based on the fresh features on .NET Framework 4.5.The book will begin by teaching you to build a modern UI application and improve it to make it Windows 8 Modern UI apps lifecycle model-compliant. You will create a portable library and throttle data source updating delays. Towards the end of the book, you will create you first Web API.
Table of Contents (19 chapters)
Microsoft .NET Framework 4.5 Quickstart Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Improving our application with toast notifications


Toasts notify the user of relevant and time-sensitive events such as the reception of a new e-mail or a reminder for an approaching appointment.

They will help our application to accomplish another of the Windows Store app principles, which is to feel connected and alive.

Getting ready

We will implement this recipe on the application that we have been developing, but we could apply what we are going to do to any Windows Store app.

How to do it...

Here we are going to add the capability of creating toasts to our app.

  1. Open the application and HelloPage.xaml.cs and add the following using namespaces:

    using Windows.Data.Xml.Dom;
    using Windows.UI.Notifications;
    Add there the following method:
    private void GenerateToastNotification()
    {
    ToastTemplateType toastTemplate = ToastTemplateType.ToastText01;
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
    XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
    toastTextElements[0].AppendChild(toastXml.CreateTextNode("A toast!"));
        //The duration
    XmlNodeList rootElement = toastXml.GetElementsByTagName("toast");
        ((XmlElement)rootElement[0]).SetAttribute("duration", "long");
        //Create and send the toast
    ToastNotification toast = new ToastNotification(toastXml);
    ToastNotificationManager.CreateToastNotifier().Show(toast);
    }
  2. Open HelloPage.xaml. Add a button near the header with the text Generate Toast.

  3. On the code behind the click event handler of the added button, add a call to the newly created GenerateToastNotification() method.

  4. Open the Package.appmanifest designer and on the Application UI tab, we will look for the Toast Capable option and select Yes on the combobox.

  5. If we execute the application and click a number of times on the Launch Toast button, our display should look similar to the following image:

How it works...

In a way very similar to our tile notification, we picked up the toast template type and loaded the template content into an XmlDocument variable.

After this we accessed the element that we wanted to change, in our case the text element, and changed its value.

We changed its duration by setting one of the attributes of the toast element.

Finally, we created ToastNotification from the resulting XML and showed the toast.

There's more...

We could navigate to a specific destination in our app when the user taps on the toast, as that might be an invitation to explore new and interesting data.

We can use different toast templates, schedule toast notifications, or even send toast push notifications through the WNS.

The toast can be extensively customized with images, sounds, or an expiration time.