A day with .Net

My day to day experince in .net

Archive for the ‘Windows 8’ Category

SQLite with Windows 8 apps

Posted by vivekcek on September 6, 2012

Installing the SQLite

The first thing you want to do is install the package.You can do this from within Visual Studio itself in all editions.
From the Tools menu, choose Extensions and Updates and then choose the Online section (on the left of the dialog) and
search for ‘sqlite’ in the search term. This will show you the SQLite for Windows Runtime package.

Using the new package in your C#

Now that you have the SQLite for Windows Runtime package installed in your Visual Studio environment, you want to use it. In a managed (.NET) app you would do the following steps.
First, create your app (e.g., a Blank XAML app is fine). Once within your app, use the Add Reference mechanism to get to
the next step. Now you will not be browsing for any DLL directly like you would in a traditional .NET.
What we are adding here is a reference to the Extension SDK…not the library itself, a small but important distinction.
Once in the Add Reference dialog choose the Windows\Extensions view (see on left) and you’ll see SQLite for Windows Runtime.

To correctly use this in a managed app you’ll need to select that *and* the C++ runtime as seen below:

<

Now with this involved you can grab a managed wrapper to call the SQLite APIs. I personally recommend the sqlite-net library (available via NuGet) to make this easier for you.

Using the sqlite-net library you can perform tasks using a model similar to LINQ2SQL where you can have types represent database entities:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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;
using SQLite;

namespace App1
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "person.sqlite");
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                db.CreateTable<Person>();

                db.RunInTransaction(() =>
                     {
                         db.Insert(new Person() { Name = "vivek", Surname = "ps" });
                     });
            }

        }

    }

    public class Person
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [MaxLength(30)]
        public string Name { get; set; }

        [MaxLength(30)]
        public string Surname { get; set; }

    }

}

Now you just need to specify your architecture for your app (x86, x64, ARM) and when you build, the appropriate sqlite3.dll will be packaged in your app automatically. This also happens during the packaging step for the store so the right item is included for each architecture-specific package.

Posted in Windows 8 | Tagged: , , | Leave a Comment »

Navigation and Passing Information Between Pages Windows 8 Metro

Posted by vivekcek on September 4, 2012

Read my previous post Hello World Windows 8 Metro App

Download Source http://sdrv.ms/R3zfId

In this post i am going to show how we can implement navigation in a windows 8 metro app.
In metro the Frame class is primarily responsible for navigation and implements methods such as Navigate, GoBack, and GoForward.We use the Navigate method to display content in the Frame.

1. Create a new metro blank application.

2. Delete the MainPage.xaml.

3. Now add 2 Basic Page template to our app named FirstPage.xaml and SecondPage.xamal. The solution will look like below.

4. In the FirstPage.xaml.Find the TextBlock element named pageTitle and change the Text property to ‘First Page’. The XAML should look like below.

 <TextBlock x:Name="pageTitle" Grid.Column="1" Text="First Page" Style="{StaticResource PageHeaderTextStyle}"/>

5. Do the same title change in the SecondPage.xaml also.

6. Now we want to show the FirstPage.xaml when the application first launches. For that go to App.xaml.cs file under the OnLaunched() method add the code below.

 var rootFrame = new Frame();
            if (!rootFrame.Navigate(typeof(FirstPage)))
            {
                throw new Exception("Failed to create initial page");
            }

7. So when our application launches a rootFrame will be created and FirstPage.xaml will be loaded into the rootFrame.

8. Now we will create a TextBox and a Button in our FirstPage using the below xaml markup.

 <StackPanel Grid.Row="1" Margin="100,10,0,-1" >

            <TextBlock Text="Enter Your Name"/>
      
      <StackPanel Orientation="Horizontal" Margin="0,20,0,0">
  
              <TextBox x:Name="txtInput" Width="200" HorizontalAlignment="Left"></TextBox>
   
             <Button x:Name="btnSubmit" Content="Submit" Margin="5,0,0,0" ></Button>
         
      </StackPanel>
          
  <TextBlock x:Name="txtOutput"></TextBlock>
  
</StackPanel>

9. Generate Click event of button using property window of button.

10. Write the below code in button click event.

 private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(SecondPage), txtInput.Text);
        }

11. Add the below xaml markup in SecondPage.xaml. Which create a TextBlock to show passed parmeter.

<StackPanel Grid.Row="1" Margin="100,10,0,-1" >
 
             <StackPanel Orientation="Horizontal" Margin="0,20,0,0"/>
  
            <TextBlock x:Name="txtOutput"></TextBlock>
  
      </StackPanel>
</StackPanel>

12 In SecondPage.xaml.cs override the OnNavigatedTo() method and add below code.

 protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string name = e.Parameter as string;

            if (!string.IsNullOrWhiteSpace(name))
            {
                txtOutput.Text = "Hello, " + name;
            }
            else
            {
               txtOutput.Text = "Name is required.  Go back and enter a name.";
            }

        }

Posted in Windows 8 | Tagged: , , | Leave a Comment »

Hello World Windows 8 Metro App

Posted by vivekcek on September 3, 2012

Download Source Code: http://sdrv.ms/NQmKeD

Today I am going to show you how to create a simple Hello World metro app. Our Hello Word app contain a text box and a button. If we enter some text in the textbox and press submit button that text will be shown in our app.

Software’s You Need.
————————————-

. Windows 8 beta or release preview
. Visual Studio 2012 Express for Windows 8.

Knowledge Need
——————————-

. XAML,C#, Visual Studio.

1. Open the Visual Studio 2012.

2. Create a new blank project. Give some name ‘MyApp’.

3. Go to solution explorer and delete MainPage.xaml.

4. Now select and right click on the MyApp project. From the context menu select Add -> New Item

5. Add a basic page. Give name as MainPage.xaml.

6. Now in the xaml mark up of MainPage just above VisualStateManger and in the Root Grid Copy the xaml mark up code given below.

<StackPanel Grid.Row="1" Margin="100,10,0,-1" >
            <TextBlock Text="Enter Your Name"/>
            <StackPanel Orientation="Horizontal" Margin="0,20,0,0">
                <TextBox x:Name="txtInput" Width="200" HorizontalAlignment="Left"></TextBox>
                <Button x:Name="btnSubmit" Content="Submit" Margin="5,0,0,0" ></Button>
            </StackPanel>
            <TextBlock x:Name="txtOutput"></TextBlock>
        </StackPanel>

7. This xaml mark up create a textbox and button with some textblock for display data.

8. Now we need to generate a click event for out submit button. We need to use the property window to generate click event.

9. Add the below code in the event handler generated in MainPage.xaml.cs.

 private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            txtOutput.Text = "Hai " + txtInput.Text;
        }

10. Final Output

Posted in Windows 8 | Tagged: , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 422 other followers