Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

Making apps for Windows 8 Part II : Little Notes sample

Rate me:
Please Sign up or sign in to vote.
4.22/5 (8 votes)
28 Oct 2012CPOL8 min read 34.6K   608   13  
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 http://go.microsoft.com/fwlink/?LinkId=234238

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()
        {
            this.InitializeComponent();

            // 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";
                requestData.SetText(Notepad.Text);
            }
        }

        /// <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.
                NotesGrid.Items.Add(block);

                // 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.
            NotesGrid.Items.Remove(block);
        }

        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))
                    NotesGrid.Items.Remove(element);
            }
        }
    }
}

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.

License

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


Written By
Student
Canada Canada
Microsoft Student Partner at University of Waterloo

Comments and Discussions