Click here to Skip to main content
11,429,777 members (75,970 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Making apps for Windows 8 Part II : Little Notes sample

, 28 Oct 2012 CPOL
Tutorial on developing apps for windows 8
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at

namespace LittleNotes
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
        public MainPage()

            // Tell Windows 8 that our app can share.
            DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested;

        void MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
            // We only have text to share when a note is selected.
            if (Notepad.Visibility == Visibility.Visible)
                DataPackage requestData = args.Request.Data;
                requestData.Properties.Title = "My little note";

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)

        private void NewNoteBtn_Click(object sender, RoutedEventArgs e)
            // Hide the menu.
            NotesGrid.Visibility = Visibility.Collapsed;
            // Reset the content of the text editor since we are starting a blank note.
            Notepad.Text = "";
            // Show text editor.
            Notepad.Visibility = Visibility.Visible;

        private void SaveClose_Click(object sender, RoutedEventArgs e)
            // If we are in text editing mode.
            if (Notepad.Visibility == Visibility.Visible)
                // Creates a new textblock that for this note.
                TextBlock block = new TextBlock();
                block.Width = 250;
                block.Height = 125;
                block.Text = Notepad.Text;

                // Assign the click function.
                block.Tapped += block_Tapped;

                // Add that note to the grid.

                // Go back to main menu.
                Notepad.Visibility = Visibility.Collapsed;
                NotesGrid.Visibility = Visibility.Visible;

        private void block_Tapped(object sender, TappedRoutedEventArgs e)
            // Get a reference to the block that has been tapped.
            TextBlock block = sender as TextBlock;

            // Open the text editor with the content of that block.
            Notepad.Text = block.Text;
            NotesGrid.Visibility = Visibility.Collapsed;
            Notepad.Visibility = Visibility.Visible;

            // Since we are currently editing this block, remove it from the menu.
            // It will be added again once we save the note.

        private void Delete_Click(object sender, RoutedEventArgs e)
            foreach (UIElement element in NotesGrid.SelectedItems)
                // Don't delete the "New Note" button.
                if (element.GetType() != typeof(Button))

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Canada Canada
Microsoft Student Partner at University of Waterloo

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 28 Oct 2012
Article Copyright 2012 by Advecticity
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid