SQLite Database in Windows phone app – Part 2 (SQLite Operations)

In my last blog post I have explained you how to install and setup SQLite Database in your Windows Phone app Project. In this post I will explain you how to create or delete a database and how to perform different operations like insertion, deletion & searching etc.

We will be working on the same project that we have created in last post. So if you have missed that post you can view it from the link given below. And follow the steps given below.

SQLite Database in Windows phone app – Part 1 (Installation and Setup)

 

Step 1: Create a new folder called Database in your project and add a class to it having the name Schema.cs.

 


 

Step 2: Now create a table called Students in form of class that we need to include in our Records.sqlite database.
The class having the name Students with all the getter and setter methods have been given below.

 


 

 

Note: One property needs to act as primary key and its value should be unique for each record. In this example Id of student acts as primary key.

Step 3: So it’s time to perform all SQLite operations. So I thought to make a single helperclass called ‘DatabaseHelper.cs’ in Database folder and handle all the SQLite operations with this helperclass.

 

 

Step 4: This is the main step where we will be writing all the SQLite operations in the helperclass.

 

 

1. Creating a Database While creating a database we need to first check whether it exists or not. If not then create a new database. The code for the following is given below.

 

 

Note: You can create more than one table in a database. In this post we have just created one table to keep things simple.

2. Deleting a Database
Note that database is created as a storage file in Windows Phone app so to delete it you only need to delete the following storage file. The code for the following is given below.

 

 
 3. Adding a new record The best practice while adding a new record in the database is to first check whether database with following name exists previously or not and then insert a new record. The code for the following is given below.  

 


 
4. Updating an existing record
To update a record in database we first need to query that object from the database and then only we can make changes to it. The code for the following is given below.

 

 

Note: In this example we are updating the marks of the student with given id using the search query.

5. Deleting an existing record Similar to updating an existing record you first need to query the record from the database and then perform the deletion operation. The code for the following is given below.

 


 
6. Selecting all students from the database
The code for fetching all the records from student table is given below.

 

 
Note: The full code for the DatabaseHelper.cs is given in this link.

Step 5: Now after writing the code for database operations the next step is to create the UI for the above and bind the data to it. On MainPage.xaml create 4 different buttons as shown in the figure below.

 

 

The code for the Design view is as following  
 

 
Different buttons are

1. Create Button– This button is used to create a new database. The code for the click event handler is as given below

 
 
 

2. Delete Button– This button is used to delete the current database if exists. The code for click event handler is given below.

 
 
 
3. Add a new record Button– This button navigates to new page. Where we can add a new record into the students table. The code for click event handler is given below.

 

 

4. Display all records Button– This button navigates to a new page. Where we can view all the records and delete them from the table. The code for click event handler is given below.  

 

 

Step 6: Create two new pages having the name AddRecord.xaml and Display.Xaml in your project.

 


 

AddRecord Page

In this page we insert a new record into the table

 


 

The code for xaml page is given below

 

 

When use clicks on the insert button the record is inserted into the database. The code for the click event handler is given below.

 

 

Display Page

In this page we display all the records that are present in the database as shown in the figure below.

 

 

The code for the xaml is

 

 

And the code for the Display.xaml.cs is as following

 

 

Note: In both of the pages we have made use of the functions defined in DatabaseHelper.cs.

 

Step 7: Add the following code for back button press in App.xaml.cs to control the navigation from one page to another.

 


 


 
If you have any doubts related to this post you can download the full source code of the project from the link given below.

SQLiteDatabase.zip

In my next blog post I shall explain you how to explore SQLite Database using IsoStoreExplorer tool.

http://windowsapptutorials.com/windows-phone-8-1/sqlite-database-in-windows-phone-app-part-3-sqlite-exploration/

Discover Relevant Tips to Provide a Great Mobile User Experience

As more and more people are using mobile devices, mobile app development has become increasingly refined. The proliferation of Smartphones and tablet devices has brought a whole new generation of developers who are ready to go all out to market their apps aggressively. However, as people become more habitual of mobile devices and more comfortable using apps, webmasters need to continuously look for ways to make their development successful.

Designing for mobile- whether it’s a native app or web app- it’s the user experience that matters a lot. A good user experience is extremely crucial for the success of any app. As more and more apps hitting the Google Play or Apple store, app users are becoming highly sensitive to a poor experience on mobile devices and are less likely to forgive apps which frustrate them. Webmasters must pay attention this aspect and thus develop accordingly. They need to understand the good and bad recipes of a design and their impact on the user experience.

If you are designing for mobile, here are some key tips and tricks which you might consider to please your audience and thus make your mobile development venture a success.

1. Giving Users Specific Hints

Being associated with a mobile app development services provider, if you are designing for mobile devices which are smaller than desktop don’t overwhelm your users with a lot of information. Use progressive disclosures technique to help users maintain their focus and attention in your design. Give users specific hints about the actions they can take to discover content they are actually looking for. Don’t give them truckload of information at once. Instead, give them opportunity to explore the content as per their needs. A good mobile device design must have the ability to strip down all the essential elements, because you have limited time to grab the user’s attention.

2. Pay Attention to the Information Structure

 

 

 

 

 

 

 

 

 

Here we are talking about the functionality as well as the content organized within a structure that’s relevant for the user to consume whatever is being provided on the web page. The structure basically consists of search, navigation, and labeling. To achieve an appropriate structure on the mobile design, make sure you give optimum labeling to the major features and content of your landing page and arrange it as per the user needs. Pay attention to the navigation requirements of the targeted device. Also, providing navigational direction will help users to explore the website or app better. Additionally providing links and navigation with comprehensive labels will help you provide a delightful user experience.

3. Respect Your Touch Screen Users

As we know that on mobile users are only equipped with fingers and not with mouse to click so, it’s extremely important to make things easy to tap. This is something which helps your audience to have a pleasurable mobile viewing experience. Additionally, fingers are much flatter, so adjust your elements accordingly. Make buttons, links or any other touch target bigger. Also assign touch targets with a considerable amount of margin or space. This would allow users to tap effortlessly with a finger. At least give 10px of margin can be assigned around the target.

4. Get the Design Right

 

 

 

 

 

 

 

 

The layout and design of your website on mobile is what you should pay attention to while planning to build it. A website with a poor mobile design will only frustrate your users. So, it is recommended to spend some time and money to get a high-quality website. A top-notch design means a design which is easy to scan, and conveys information instantly. Also, pay attention to give a uniform appearance to your visuals with the help of color and typography. Your mobile website design should be formulated in such a manner that it guides its users throughout, interact with them, and helps them to complete the task.

5. Plan a Logical Mobile Navigation

Mobile users boast less patience as compared to the desktop users. Users can access their mobiles anytime, anywhere. Therefore, it is crucial to design a framework which complements their situation and context. Your user might be walking down the lane, waiting for the bus or taxi, or standing in a crowded place or so on. While your job is to ensure a good user engagement on your site, a complicated framework in the form of complicated navigation, drop-down menus will only make them feel irritated.

Your navigation should be simple and be able to provide information with just few clicks. You can also leverage the benefits of site analytics to locate the search queries your users prominently look for, and display the information accordingly.

Conclusion

User experience is an essential aspect for the success of your website or app designed for mobile. Having a mobile-friendly website should be the top-priority and this is something which you can achieved by determining what your user want and how they want it.

How to get IP address in Windows Phone 8 and Windows 8.1 app

Windows Phone 8 provides an easy way to identify the IP address of the phone from the Windows Phone App. Note that there are possibilities of having multiple network interfaces on your Windows Phone. For example, the Windows Phone Emulator has 3 network interfaces as shown in the screenshot below. The code snippet tries to retrieve the first one which is added to the List.

Windows 8

DNS is not supported by Metro applications. So to find the IP address in a Metro app import the Windows.Networking.Connectivity namespace. NetworkInformation.GetInternetConnectionProfile retrieves the connection profile associated with the internet connection currently used by the local machine. NetworkInformation.GetHostNames retrieves a list of host names. Hostname has various properties such as Canonical Name, Display Name and Raw Name, but they all seem to return the same string. Here ipadd will give the IP address of the machine (localhost). The code below lets you fetch the IP address of the local machine.    

Here’s a screen shot of the app showing the IP address in a textbox.

Get the full source code of the solution with Windows Phone 8 and Window 8.1 projects in it.

Download full project source code IP-Address.zip

Working with local database in Windows Phone- Part I

In my last post I gave an introduction to local databases in Windows Phone. You can have a look at it again to refresh things.

http://www.windowsapptutorials.com/windows-phone/working-with-local-databases-in-windows-phone-8/

I will start from where I left in the last post so do have a look.

Creating a database

Now that you have added reference to System.Data.Linq, you can create a local database in your app. The tables of the database can be created anywhere as long as it is accessible globally in the app. Still I would recommend that make a separate class DB.cs for your tables.

Step 1. Add a new class DB.cs

Right click on your project in solution explorer and choose to add a new item. Choose a class in the dialog box that opens up and name it DB.cs


Step 2. Add the namespace System.Data.Linq.Mapping

Remove the class DB that was created in the file DB.cs when you added the class. Also add the following reference in your file.

using System.Data.Linq.Mapping;


Step 3. Creating a table

Now our first task is to create a database which is used locally isolated to the application. Since we are going to use the LINQ to SQL data context as a bridge between the local database and the data context we need to create 2 classes as shown below. The screen shot also shows a snapshot of a table whose code is given below. Add this table in DB.cs.




Note the following

  • The table name is User_details which is just below the declaration for the table. A table can be visualized as a class with attributes which can be imagined as data members.
  • The columns are added in a similar fashion as data members are added in a class. The data type of the column is defined along with the column name.
  • Notice that column ID is defined as the primary key for the table.
  • The table has three columns, ID, user_name and user_email

Next you need to define the DataContext for User_details which is basically used as the database schema to create an instance, copy the code from the below code block as shown in the screen below.

Note the DBConnectionString is used to identify the database in the isolated storage. An app could contain multiple databases and you should be able to identify each of them uniquely so each one is given a unique name. Here we will be using a single database so you don’t need to worry about the naming.

In our app the table User_details will be referred by the name Users. For each table of the database you need to create its data context. Here’s a screen shot from another app showing how the data context was defined for its database which contains multiple tables.



Step 4. Add the code to create database if it doesn’t exist when the app is opened

Below InitializeComponent in Mainpage.xaml add the following code which checks if a database exists and creates one if doesn’t.



Adding records to the database

Now that you have added a table in your database and set up the DataContext for connecting to that database, you can now add records to your table. Again it would be convenient for you if you create a separate class DatabaseAdd.cs for adding any record to your database. Here’s how you could add records to a table

Step 1. Add a new class DatabaseAdd.cs in your project

We will create a new class DatabaseAdd.cs for adding any record to our database

Step 2. Create a function AddUser in DatabaseAdd.cs

Our function AddUser takes name and email id as parameters and adds a record to User_details table. It uses the UserDataContext to connect to the database and creates a new instance of User_details class (table) and lastly inserts the record.

Note the following

  • UserDataContext is used to establish a connection with the database
  • A new instance of User_details is created
  • The data members are initialized with the values to be inserted
  • InsertOnSubmit inserts the record to the table
  • SubmitChanges commits the changes to the database

Here’s the code for AddUser function

Now that we have created a function to add records to the table, let us make a page in our app where one could enter the details and insert it to the database.

Step 3. Add a new page AddUsers.xaml and create a form to insert users in the database

Go to solution explorer and add a new page to your project and name is AddUsers.xaml. Create a form with two textboxes and a button as shown below.


Step 4. Add the button click event handler which when clicked will insert records to the table

Add a Click event handler for the button. The event handler takes the text from the two textboxes and checks if they are empty. If the entered name and email are not empty then it calls the AddUser function by creating an object of DatabaseAdd class.



Fetching records from database

Now that we have inserted records to our table, we would like to fetch records from it. Again I would like you to create a separate class FetchDatabase.cs to fetch records from any table of our database. Here’s how could fetch records from a table

Step 1. Add a new class FetchDatabase.cs to fetch records from any table of the database

Add a new class to your project and name is FetchDatabase.cs to your project

Step 2. Add a public function GetAllUsers which returns a list of records from a table

The function GetAllUsers returns a IList which contains all the records fetched from the database. We have set up the connection using the data context and then queried records from the User_details table. The query returns a list of records which is saved to a IList list. IList is nothing but a collection of objects of same type.



Step 3. Create a class Users to hold the details

We define a class with data-members id, name and email. It will be used to hold the details from the fetched records.

Step 4. Create a function getUsers to fetch and return a list of Users

  • GetAllUsers function returned a list of records. We call GetAllUsers and keep the fetched records in a Ilist usrs.
  • We also create a new list allmsgs to hold the details of all users
  • We then iterate through all the usrs and put the details of each user to a new instance of Users class. We then add this instance to our list allmsgs
  • Lastly we return the list

Now that the function to fetch records is ready, let us create a new page to display records from the database.

Step 5. Add a new page AllUsers.xaml and create a Listbox to display records from the table

Go to solution explorer and a new page AllUsers.xaml. Add a ListBox to this page and create its data template. The data template contains two TextBlock with name and email binded to it. These TextBlock are contained in a StackPanel and its tag property is binded to the id. We won’t be needing this tag now but when we need to manipulate the selected record, the id binded to the tag will let us identify the record.

Step 6. Add the code to fetch records and bind it to the listbox

In the constructor of the page after call to InitializeComponent create an instance of FetchDatabase and call the getUsers function as shown below



Now let us run the project and see if everything works perfectly.

UPDATE: Here’s the link to other posts in this series

Get the full source code here,

Download full project Databases.zip

Creating a Windows Phone app for your website using Microsoft App Studio

Microsoft aims at making it easier for developers to get started with app development on Windows platform. Our website too has a similar goal and we want more and more developers to start developing apps for this platform. To make life easier Microsoft started a new programme, App Studio where less experienced developers can have a go at making apps for Windows Phone and desktop. It’s a wizard type of thing where you get various customizations to make your app personal. Even some experienced developers use it to develop a prototype. After you are done with making an app in app studio, a project zip can be generated which can be further edited in Visual Studio. Otherwise you could directly publish the app from App Studio. Here’s how you could create an app using Microsoft App Studio

Step 1. Go to your App Studio account

Visit:

http://appstudio.windowsphone.com/

Log in to your account or sign up if you aldready don’t have one.

Step 2. Grant permissions to App Studio

When you log in to app studio for the first time it asks for certain permissions. Grant it those permissions and move forward

Step 3. Start a new project from the dashboard

When you log in, you will be taken to a screen that would list all your previous projects. Start a new project from this screen.

Step 4. Choose an app template for your project

You could choose from multiple pre-defined templates that help you get started quickly. Here we will be choosing Web App Template for making an app for our website http://www.windowsapptutorials.com.

Note: Web App Template supports only windows phone and not windows 8 for desktop. So you can publish this app only for windows phone.

Click on Create to move to the next step.

Step 5. Fill in the details under Content tab

  • Enter the App Title. Here the app name is `Windows App Tutorials`
  • Enter the base URL for your website
  • Customize App bar buttons if you need to
  • Change the Share actions to personalize it to your needs
  • Save the changes and move to Themes


Step 6. Selecting Theme for your app

You could modify the default theme for your app and choose between light and dark. You can even make a custom style for the app. Play with the customizations, save it and move to the next step.

Step 7. Add tile images, content, lock screen image and splash screen

Under the tab Tiles you can choose amongst Flip, Cycle and Iconic tiles and set its image and content. Also you can add a splash screen image and lock screen image for the application. Save these changes and move to the last tab, Publish Info.

Step 8. Fill in the publish info for the app

This step deals with the details which one usually finds under WMAppManifest.xml while working in Visual Studio.

  • Enter the app title
  • Choose default app language
  • Write something in the app description
  • Enter the desired app display name

Step 9. Associate app with the store

If you plan to publish the app in windows phone store then you need to fill in the required details under Associate App with store under Publish Info tab. Fill all the details correctly and confirm it. Similarly if you want to enable ads in your app then you need to check `Enable ad client` and fill in the details in `Microsoft pubCenter data`. For this you need to have a pubcenter account and create a new application and its ad units using that account. Those details need to be filled in here to enable ads. For now I am skipping ads to give you guys the best app experience (after all this would be our official app for now).

Save changes and click on Finish.

Step 10. Generate packages

Click on Generate to build your app and generate Download Package, Publish Package and Source code package.

You will be notified that windows 8.1 is not supported. Check Publish packages and Installable packages in the dialog box that pops up. Click on generate to get the packages.

It will takes a minute or two to build all the packages for you. Then the packages will be displayed to you with instructions to use it.

Step 11. Install Windows Phone 8 certificate to your phone

If you wish to deploy the app on your phone then click on `Install Windows Phone 8 Certificate` and scan the QR code to get the certificate installed on your phone.

Tap to open the key file and install it on your device.



Step 12. Download installable package

Download the installable package in a similar fashion by scanning the QR code and navigation to the embedded link.

Tap on the XAP file link to install it to your device.

Step 13. Download Publish Package

Lastly download the Publish package which contains the app XAP file so that you can submit the app to the store.

Step 14. Submit the app to the store

Now that you have successfully created an app, you could submit it to the store for others to see it. If you don’t have an account then sign up at

http://www.developer.windowsphone.com

Log in to your account and click on Submit App

Step 15. Click on App Info to fill in the app name, price and other info

Enter some for your app, choose a price and fill in other relevant info

Step 16. Upload your app package and other info

Save the changes in app info and move to Upload and describe your package.

Choose the XAP you earlier downloaded as Publish Package and upload it.

Start filling in other details once the app package has been uploaded.

Add a app title icon which will be visible in the marketplace and optionally add promotional images. You also need to one mandatory screen shot of the app.

Save the changes and click on Review Submission. Finally review your submission and check for any errors. If everything is correct then proceed to submit your app.

You have successfully created an app for your website using Microsoft App Studio and published it to the store. Sound off below and show off your apps created using App Studio. I will add another post explaining how you could edit an app created using App Studio using Visual Studio to add more features.

Here’s the marketplace link of the app we just created. Do download it and stay updated with our latest posts.

http://www.windowsphone.com/s?appid=42f23a4d-4477-4aba-9492-d124b0a737a9

 
If you want to make a simple feed app using app studio then this article may be useful. It explains how to draft a simple feed app for Engadget.

Working with local databases in Windows Phone 8

In this article I am going to explain how you could use the Isolated Storage by creating a local referential database which can be accessed locally by that Windows Phone application. Database operations could be performed using LINQ to SQL or using SQLite. Usually developers prefer LINQ to SQL for DB operations but SQLite becomes a better option when you plan to make apps both for Windows Phone and Windows 8. Currently Windows 8 supports databases using SQLite only, so if you don’t wish to rewrite the code for both platforms you should prefer SQLite.

LINQ to SQL object model uses the System.Data.Linq.DataContext namespace to basically make a proxy call to the local database in the Isolated Storage container. LINQ to SQL Runtime plays as a bridge between the data context object and the real data to do the manipulations based on the user selection. Here are a few points which you should keep in mind while making databases for your app

  • Database file is stored in Isolated Storage Container
  • The database is available only for the application targeted as it is isolated from other apps
  • LINQ is used for queries
  • Local database feature can be accessed directly by adding System.Data.Linq assembly only since primary support is available with the framework.
  • Connection string much be used in the format of “Data Source =’isostore:/DirectoryName/Databasename.sdf”;

We are going to perform the following tasks to get a good grasp on using databases

  • Creating a local database
  • Adding data to the local database
  • Fetching data from local database
  • Deleting data from the local database
  • Deleting a local database

Before you create a local database, you need to create a new project and add the required references to it.

Step 1. Create a new project `Database’ targeted for Windows Phone 8

Open Visual Studio and create a black Silverlight Windows Phone app.

 


 

Step 2. Check and add a reference to System.Data.Linq to your project

Right Click on References in your project’s Solution Explorer and click on Add Reference and

 

Browse and choose System.Data.Linq and add it to your project. I am not sure if this reference would be already added. If you are using the latest VS(currently VS 13 update 1) then it may be already referenced in your project as it was in mine. Otherwise you need to add this reference.

 

 

You could right click on Windows Phone under References to open the object browser and check if a reference to System.Data.Linq already exists.

 

 

Now you are all set to create a local database in your app. In my next post on databases I will explain how you can create, add and fetch from a database. I will update this post with the links to those posts.

UPDATE: New articles in this series

 

Creating an app lock for Windows Phone 8 with numeric password

Windows Phone still doesn’t allow its developers to integrate a universal app lock with third party apps. Still you could let the users put a lock on your app and prevent unrestricted access. This is a feature which some messaging apps should offer because friends and colleagues often breach the privacy. Their intended fun is often very annoying to the actual user of the app. So you as an app developer could let your users put a lock on his app. In this tutorial I will show how it could be done. We have three primary goals.

  • Let the user choose to have an app lock or not to have it
  • If he chooses to have an app lock then let him set the pin. We should also handle the situation where a pin is already set and the user wants to change it
  • When the app is launched it should check for a pin and if the app is locked then the user should be prompted for a password otherwise proceed normally

Here is how you can integrate it with your app. If you are struck, download the source code before-hand and use it as a reference.

Step 1. Creating a SetLock page to set and change pin

The user should be able to set a 4 digit pin and change it if it is already set. The screenshot shows the design view of the page.

  • I have placed a CheckBox which when checked to true, sets the visibility of textbox to visible.
  • There are 3 textboxes old_pin, pin, confirm_pin. When a user is setting a pin for the first time then only pin and confirm_pin are visible to him and when he tries to change his pin then old_pin is also visible.


Here is the XAML code to get a similar design for your page.


Step 2. Action for applock_Checked event handler on changing check status of check box


The code is self-explanatory.

  • It checks the status of check box
  • If it is checked to true then pin and confirm_pin is set to visible. If a pin was already set then old_pin is also set to visible
  • If it is checked to false then the textboxes are collapsed and the isolated storage settings is updated to reflect the change

Step 3. Set the key up event handler for the textbox

This is required to ensure that the user doesn’t set a pin of more than 4 digits. This event handler handles this situation and is used for all the 3 textboxes.
Step 4. Save pin event handler to set the pin This event handler saves the pin in isolated storage settings.

    • Checks if the app was already locked with a pin. If yes then it matches the old pin with the pin entered as old_pin. If they match a function is called to set the new pin

 

    • If the app wasn’t locked already then simply setpin() function is called

 

    • Setpin() function saves the pin in the isolated storage settings

 

Step 5. Handle the layout root loaded event to set the status of check box and textbox visibility When the page is loaded this event handler checks whether the app is locked or not and shows the appropriate options to the user
Step 6. Setting variables to store password and islocked status when the app launches
When the app is launched, set islocked and password from isolated storage settings.
Step 7. Designing the page to enter the pin when the app is launched The page contains a textbox where the user will enter his pin. I have edited the style template of the textbox to give it a better look and fill. We haven’t used a password box as it doesn’t support Numeric input scope. It would not look good if the user is not presented a numeric keyboard upfront. So we use a textbox with numeric input scope and use a key_up function to alter the displayed characters of the textbox.    

    • The layout root event handler checks whether the app is pin locked. If it is then it sets the password box to focus otherwise navigates to the MainPage of the app.
  • The key_up event handler of the textbox handles the following
      • It saves the input pin to a variable

     

  • It gets the length of the input string and displays as many password characters in the textbox instead of the entered pin
  • If the length equals 4 then it automatically checks the entered string against the pin and provides or denies access

 

 


Step 8. Finally edit WMAppManifest.xml to set the default page as Lock.xaml when the app launches.

Set the navigation page as Lock.xaml so that it is the first page that opens when the app is launched.


 

Here is how the app looks when it runs. The lock screen and the set lock screen are shown below.

Download full project Lock.zip

How to work with ListBox selected item in Windows phone application

In this post I am going to talk about different techniques of accessing a Listbox Selected item.

When we populate a Listbox with data items we have two options

1. First is that we can use Listbox items directly

2. Second we can make the use of databinding.

 

When we use option 1 the Selected item is usually the Listbox item so we can access it easily and perform the desired manipulations on it.

But when we make use of Second option which is the most widely used option the SelectedItem is the type of the business object (data class) which we use when performing data binding

There are different methods of accessing the ListBox Selected item which will be explained below with the help of an example.

 

In the following example I shall be creating a Listbox of student names along with a button which shall display their marks along with their name in a Texblock given below the List.

The different steps that need to be followed are

 

1. Create a new project Listboxselecteditem.

2. Then open the MainPage.xaml and remove the XAML code for the StackPannel named TitlePanel and replace it with the following code so as to change the page name and application name .

 


 

 

3. Now add the following Xaml code in the Grid named ContentPanel so as to create a list box in the page having the name mylistbox and bind the target Textblock nameblock  with data source name property. It also adds two Textblocks having the name Studentnameblock and marksblock to display the name and marks of selected student.

 



 

4. Now open the page MainPage.xaml.cs.

5. Create a public class called Resultclass having the properties name and marks in the page globally.


 

6. Then add the following code consisting of two global arrays for sample data to be used inside listbox named mylistbox

 

7. After that make a sample list from the data given above with the help of the following code given below.

 


 


 

8. Then call the function makeasamplelist in the constructor of the MainPage.xaml.cs.

 


 

If you face any problems while understanding the above code you can refer to the following blog post to understand the basics of data binding.

http://www.windowsapptutorials.com/windows-phone/data-binding-in-windows-phone-app-one-way-binding/

 

9. Now to add the following code for the Listbox SelectionChanged Event handler in the MainPage.xaml.cs.

This code will execute whenever you make a selection from the Listbox.

 


10. The code for the marks button Click EventHandler is as following. Whenever you click on the Marks button of a any Listbox item this code will execute.

 



.

11. Now run the application. You will see the following List created in your application.

 


 

12. Now Select any Listbox item. Suppose we select the second item that is Fred. When you will Select it the values of Textblocks Studentnameblock and marksblock will change respectively.

 


 

13. When you click on Marks button given in front of the student name the corresponding student name and marks will be shown in the Textblocks given below the Listbox.

When you Click on the Marks button given in front of James the following output will be shown.

 


 

14. If hope this post will be helpful to you. Download the full project zip file from the link given below.

 

Download full project file Listboxselecteditem.zip

 

How to bind ListBox to data in Windows phone application – Introduction

As an application developer, you can create ListBox controls without specifying the contents of each ListBoxItem separately. You can use data binding to bind data to the individual items.

In this post, I will explain with the help of an example how to create a ListBox that populates the ListBoxItem elements by data binding to a data source.

Suppose you want to display a list of students with their marks in a windows phone app. If we specify the contents of each item separately it would take a lot of time. So we make a use of data binding. The steps that need to be followed are

1. Create a new project ListboxBinding.

2. Then open the MainPage.xaml and remove the XAML code for the StackPannel named TitlePanel and replace it with the following code so as to change the page name and application name .

3. Now add the following Xaml code in the Grid named ContentPanel so as to create a list box in the page having the name mylistbox and bind the target Textblocks nameblock and marksblock with data source name and marks respectively.

 


 

4. Now open the page MainPage.xaml.cs.

5. Create a public class called Resultclass having the two properties name and marks.

6. Then add the following code consisting of two global arrays for sample data to be used inside listbox named mylistbox.

 

7. Then we can add the following data into the listbox in two ways.

First method:

First method is to create a Separate list for the Listbox and adding the items into that list. After that set the itemsource property of the Listbox to that of list created.

We can do this with the help of following code.

Second method: In second method instead of making a separate listbox we can directly add the items into the listbox. We can do this with the help of following code.
If you face any problems while understanding the above code you can refer to the following blog post to understand the basics of data binding.

http://www.windowsapptutorials.com/windows-phone/data-binding-in-windows-phone-app-one-way-binding/

8. Now call any one of the following method in the Constructor of the MainPage .Both will give the same list as the Output when you will run the application.

Suppose I call the firstmethod. Then the Constructor of the MainPage will look like


9. Now run the application on Emulator. You will see the following list created on the MainPage .


10. If you want to know more about the Listbox selected item you can refer to the following blog post

How to work with ListBox selected item in Windows phone application

11. I hope this post will be helpful to you. Download the project zip file from the link given below.

Download Full Project file ListboxBinding.zip

Checkout a related post on using ListView in android.

Using ListView in Android

How to implement trial functionality in windows phone app

This topic describes how to implement a trial experience in your Windows Phone app.
The purpose of a trial experience is to give the user an opportunity to try your app before they commit to buying it. In a trial experience, you typically offer a limited version of your app, for example, by disabling features, by disabling levels in a game, or by setting a time limit on using the app. The limitation you choose for your app is entirely up to you.

 

I will be demonstrating this with the help of an example. Suppose there are two pages in an application out of which only one can be opened in trial version that is first page .

In full version of the application you can open both the pages that is first page and second page.

The steps to followed to implement trail functionality are

 

1. First create a blank project and name it Trialapp.

2. Then open App.xaml.cs and add the following using directive to the top of the page

  3. In the App class, declare the following local static variable. The LicenseInformation class enables an app to determine if it is running under a trial license.
 
 
4. In the same App class in App.xaml.cs, add the following read-only property and local variable. This property is used to cache the license information while the app is running. The app uses the property whenever the current license information needs to be checked.

     

5.In the following sections, the license information will be checked in the Application_Launching and Application_Activated event handlers. Since this logic is used in two locations, we wrap it for convenience into its own method. In the App class located in App.xaml.cs, add the following method.    


 

6. Then open the MainPage.xaml and remove the XAML code for the Grid named LayoutRoot and replace it with the following code.


 
 


 

7. Then add two more pages to the application and name it Firstpage.xaml and Secondpage.xaml.

 


 

8. Open the Page Firstpage.xaml and change the application name to trial app and page name to first page. Similarly do it for Secondpage.xaml and change the page name to Second page.

 


 

9. In the MainPage.xaml.cs  code-behind file, add the following methods.


 

10. Now run the application in debug state you will see that the app runs in trial mode as the value of _isTrial in App.xaml.cs is true. The following screen will appear before you and the second button will be disabled.

 


 

11. Now when you will change the value of _isTrial to false the app will run as full version.

 


 

12. While submitting the app to store you need to tick the checkbox of trial functionality in your app. You can do this easily. Go to your dashboard. Select your app. Go to the app info page and check the checkbox.

 


 

 

Note: If you forget to check this checkbox you app will be available in full version to all the users in the marketplace.

 

13. You can download the Project zip file from the link given below.

 

Download full project trialapp.zip