Windows 10 – Universal Windows Platform – Jsonfile App

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

2015-visual-studio

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

2015-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.

2015-new-project

Step 4

Once done select from the Menu, Project, then Add New Item…

2015-project-add-new-item

Step 5

From the Add New Item window select Visual C# from Installed and select Code then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

2015-add-new-item-library

Step 6

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Json;
using Windows.Storage;
using Windows.UI.Xaml.Controls;

public class Music
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _album;
    private string _artist;
    private string _genre;

    public Music() { Id = Guid.NewGuid().ToString(); }

    public string Id { get; set; }
    public string Album { get { return _album; } set { _album = value; NotifyPropertyChanged(); } }
    public string Artist { get { return _artist; } set { _artist = value; NotifyPropertyChanged(); } }
    public string Genre { get { return _genre; } set { _genre = value; NotifyPropertyChanged(); } }
}

public class Library
{
    private const string filename = "file.json";
    private StorageFile file;
    private ObservableCollection<Music> collection = new ObservableCollection<Music>();

    private async void read()
    {
        try
        {
            file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            using (Stream stream = await file.OpenStreamForReadAsync())
            {
                collection = (ObservableCollection<Music>)
                    new DataContractJsonSerializer(typeof(ObservableCollection<Music>))
                    .ReadObject(stream);
            }
        }
        catch
        {
        }
    }

    private async void write()
    {
        try
        {
            file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename,
                CreationCollisionOption.ReplaceExisting);
            using (Stream stream = await file.OpenStreamForWriteAsync())
            {
                new DataContractJsonSerializer(typeof(ObservableCollection<Music>))
                    .WriteObject(stream, collection);
            }
        }
        catch
        {
        }
    }

    public ObservableCollection<Music> Collection { get { return collection; } }

    public Library()
    {
        read();
    }

    public void Add(FlipView display)
    {

        collection.Insert(0, new Music());
        display.SelectedIndex = 0;
    }

    public void Save()
    {
        write();
    }

    public void Remove(FlipView display)
    {
        if (display.SelectedItem != null)
        {
            collection.Remove(collection.Where(w => w.Id ==
            ((Music)display.SelectedValue).Id).Single());
            write();
        }
    }

    public async void Delete(FlipView display)
    {
        try
        {
            collection = new ObservableCollection<Music>();
            display.ItemsSource = Collection;
            file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            await file.DeleteAsync();
        }
        catch
        {

        }
    }
}

It should then appear as such:

2015-jsonfile-app-library

Step 7

From the Solution Explorer select MainPage.xaml

2015-mainpage-library

Step 8

Select from the Menu, View then Designer

2015-view-designer

Step 9

The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Page.BottomAppBar>
	<AppBar IsOpen="True" IsSticky="True">
		<StackPanel Orientation="Horizontal">
			<AppBarButton Icon="Add" Label="Add" Click="Add_Click"/>
			<AppBarButton Icon="Remove" Label="Remove" Click="Remove_Click"/>
			<AppBarButton Icon="Save" Label="Save" Click="Save_Click"/>
			<AppBarButton Icon="Delete" Label="Delete" Click="Delete_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<FlipView Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center"  
		ItemsSource="{x:Bind Path=Library.Collection}">
	<FlipView.ItemTemplate>
		<DataTemplate>
			<Grid VerticalAlignment="Top" HorizontalAlignment="Center" Width="300">
				<Grid.RowDefinitions>
					<RowDefinition/>
					<RowDefinition/>
					<RowDefinition/>
					<RowDefinition/>
				</Grid.RowDefinitions>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="Auto"/>
					<ColumnDefinition Width="*"/>
				</Grid.ColumnDefinitions>
				<TextBlock Grid.Row="0" Grid.Column="0" Margin="10">Id:</TextBlock>
				<TextBox Grid.Row="0" Grid.Column="1" Margin="10" 
						   Text="{Binding Id}" IsReadOnly="True"/>
				<TextBlock Grid.Row="1" Grid.Column="0" Margin="10">Album:</TextBlock>
				<TextBox Grid.Row="1" Grid.Column="1" Margin="10" 
						 Text="{Binding Album, Mode=TwoWay}"/>
				<TextBlock Grid.Row="2" Grid.Column="0" Margin="10">Artist:</TextBlock>
				<TextBox Grid.Row="2" Grid.Column="1" Margin="10" 
						 Text="{Binding Artist, Mode=TwoWay}"/>
				<TextBlock Grid.Row="3" Grid.Column="0" Margin="10">Genre:</TextBlock>
				<TextBox Grid.Row="3" Grid.Column="1" Margin="10" 
						 Text="{Binding Genre, Mode=TwoWay}"/>
			</Grid>
		</DataTemplate>
	</FlipView.ItemTemplate>
</FlipView>

It should appear as such:

2015-jsonfile-app-mainpage-xaml

Step 10

Select from the Menu, View then Code

2015-view-code

Step 11

Once in the Code View below the public MainPage() { … } the following should be entered:

public Library Library = new Library();

private void Add_Click(object sender, RoutedEventArgs e)
{
	Library.Add(Display);
}

private void Save_Click(object sender, RoutedEventArgs e)
{
	Library.Save();
}

private void Remove_Click(object sender, RoutedEventArgs e)
{
	Library.Remove(Display);
}

private void Delete_Click(object sender, RoutedEventArgs e)
{
	Library.Delete(Display);
}

It should then appear as such:

2015-jsonfile-app-mainpage-code

Step 12

That completes the Universal Windows Application so Save the Project then select the Debug and Simulator option to run the Application

2015-simulator

Step 13

Once the Simulator has started the Application should then appear

2015-jsonfile-app-simulator-run

Step 14

After the Application has started running you can tap the Add button, then enter Album, Artist and Genre then can use Save to store the data using JSON in the Application

2015-jsonfile-app-simulator-ran

Step 15

To Exit the application select Stop Debugging in Visual Studio Community 2015

2015-stop

Step 16

Another option is to run as a Windows 10 Mobile application, select Debug and select Emulator 10.0.10586.0 WVGA 4 inch 512MB option to run the Application

2015-emulator

Step 17

Once the Emulator has started the Application should then appear

2015-jsonfile-app-emulator-run

Step 18

After the Application has started running you can tap the Add button, then enter Album, Artist and Genre then can use Save to store the data using JSON in the Application

2015-jsonfile-app-emulator-ran

Step 19

To Exit the application select Stop Debugging in Visual Studio Community 2015

2015-stop

Creative Commons License

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s