Creating and implementing Razor templates within Xamarin Studio

In this section, we will look at how to go about creating a new Razor template solution using Xamarin Studio. We will begin by developing the basic structure for our application, as well as adding all the necessary database models and user interface files.

Before we can proceed, we need to create the BookLibrary project. It is very simple to create this using Xamarin Studio. Simply follow the given steps:

  1. Launch the Xamarin Studio application, and choose the New Solution... option, or alternatively choose File | New | Solution... or simply press Shift + Command + N .
  2. Next, choose the WebView App option which is located under the iOS | App section, ensure that you have selected C# as the programming language to use, and click Next:

    Creating and implementing Razor templates within Xamarin Studio

  3. Then, enter in BookLibrary as the name for your app in the App Name field as well as specifying a name for the Organization Identifier field.
  4. Next, ensure that both the iPad and iPhone checkboxes have been selected for the Devices field, as well as ensuring that you have chosen iOS 10.0 for the Target field to support the minimum iOS version that we want our app to support:

    Creating and implementing Razor templates within Xamarin Studio

  5. Click on the Next button to proceed to the next step in the wizard:

    Creating and implementing Razor templates within Xamarin Studio

  6. Next, ensure that the Create a project directory within the solution directory. checkbox has been selected and click on the Create button to save your project at the specified location.

Once your project has been created, you will be presented with the Xamarin Studio development environment, along with several project files that the template created for your Razor template Solution, as shown in the following screenshot:

Creating and implementing Razor templates within Xamarin Studio

As you can see from the preceding screenshot, the BookLibrary solution has been divided into three separate folders. The following table provides a brief description of what each area is used for:

Folder type

Description

Models

This section is responsible for representing the model that our views will use, and contains a structure of fields that will be displayed and/or written to by our Razor templates.

Resources

This section contains a place for you to add the images and CSS, or JavaScript files that your application will use.

Views

This section is responsible for containing all of the HTML5 Razor templates that your application will be referencing, and they need to contain and be prefixed with the .cshtml extension.

One thing you will notice is that our solution contains a file called style.css. This is because our application is essentially a hybrid mobile solution, and contains the files you might expect from a web solution, as can be seen in the following code snippet:

    /* This is a minimal style sheet intended to demonstrate
     how to include static content in your hybrid app.
     Other static content, such as javascript files and images,
     can be included in this same folder(Resources on iOS or Assets
     on Android), with the same Build Action(BundleResource on iOS
     or AndroidAsset on Android), to be accessible from a path starting
     at the root of your hybrid application. */ 
 
      #page { 
      margin-top:10px; 
      } 

As you can see, this file doesn't contain much information, but as we work our way through this chapter, we will be adding to this file, and building the Razor template user-interface files.

Adding the SQLite.Net package to the BookLibrary solution

Now that we have created our BookLibrary solution, the next step is to add the SQLite.Net NuGet package to our solution, since this doesn't get added automatically for us. The SQLite.Net package will essentially provide us with the ability to have our application write to an SQLite database, to store our book details.

Note

A NuGet package is essentially the package manager for the Microsoft development platform that contains the client tools that provide our solution with the ability to produce and consume .NET packages.

Let's look at how to add the SQLite.NETNuGet package within our BookLibrary solution, by performing the following steps:

  1. Right-click on the Packages folder that is contained within the BookLibrary solution, and choose the Add Packages... menu option, as shown in the following screenshot.

    Adding the SQLite.Net package to the BookLibrary solution

  2. This will display the Add Packages... dialog. Enter in SQLite.Net within the search dialog, and then click on the sqlite-net option within the list, as shown in the following screenshot:

    Adding the SQLite.Net package to the BookLibrary solution

  3. Finally, click on the Add Package button to add the sqlite-netNuGet package to the Packages folder, contained within the BookLibrary solution.

When you click on the Add Package button, you will notice that the package manager will create two new files for us within our solution. These are called SQLite.cs and SQLiteAsync.cs, and are basically wrapper convenience classes that allow .NET and Mono applications to store data within a SQLite 3 database.

Now that you have added the NuGet package for the sqlite-net library, we can begin to utilize this library within the BookLibrary solution that we will be covering in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.136