Sunday, November 24, 2013

Windows Phone 8 submission Tip

Dont forget to set the icon for the live tiles in the application manifest.  I failed submission for using the default icon in live tiles because I was setting it in code but did not do it in the manifest.

Saturday, June 1, 2013

Seeding a Lex.DB

Lex.DB is an object database that can be used in Windows Store and Windows Phone apps.   This is a fast free alternative to SQLite.

To learn more about Lex.DB see Lex's blog



In my case I wanted to include a database that already had data in it with a windows phone 8 app I created.


List<AutoMobileInformation.AutoMobileCodes> lstComplete = 
                        new List<AutoMobileInformation.AutoMobileCodes>();
using (var db = new DbInstance("auto codes"))
{
     db.Map<AutoMobileInformation.AutoMobileCodes>().Automap(i => i.Id)
                       .WithIndex("Code", i => i.Code).WithIndex("Manufacture", i => i.Manufacture);
     db.Initialize();
     lstComplete = db.LoadAll<AutoMobileInformation.AutoMobileCodes>().ToList();
     if (lstComplete.Count == 0)
    {
         var lex = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Lex.Db"
                         CreationCollisionOption.OpenIfExists);
         var auto = await lex.CreateFolderAsync("auto codes"
                         CreationCollisionOption.OpenIfExists);
         StorageFile seedFile1 = await StorageFile.GetFileFromPathAsync(
                     Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, 
                                  @"AutoMobileCodes.index"));
         StorageFile seedFile2 = await StorageFile.GetFileFromPathAsync(
                     Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, 
                                  @"AutoMobileCodes.data"));
         await seedFile1.CopyAsync(auto, @"AutoMobileCodes.index"
                     NameCollisionOption.ReplaceExisting);
         await seedFile2.CopyAsync(auto, @"AutoMobileCodes.data"
                     NameCollisionOption.ReplaceExisting);
    }
}
In the code first we create an instance of the database.  The we add an index to the database.  Then we check and see if there is any data in the database.  
If there is not I replace the Index and Data file in isolated storage with files that have data.
 

Sunday, May 12, 2013

Create a Windows Store Analog Clock

This sample uses xaml and c# to create an analog clock in a windows store application.  Xaml allows you to vary the angle of lines when drawn on the screen. I used an ellipse as the background of the clock. We will draw 3 lines for the hour, seconds, and minute hand on an analog clock.  A rotate transform turns a line into the second, minute and hour hand.  The transform will draw the line from the center of a circle at the angle needed to represent the time. For seconds and minutes multiply the value by 6 to get the angle for the clocks hand transform.  For hours multiply the hour by 30 to get the angle needed for the hour hand transform.   I use a dispatch timer to update angle of the hands based on the current time.  The dispatch timer will fire an event every second which we use to update the angle of the clocks hand.   The clock could be improved by using a clock image instead of an ellipse.  
Additional work would need to be done to be able to submit the app to the store.   If ads were added you would need to give the app permission to access the internet.  Any app that has access to the internet would require a privacy policy.
 The Xaml
<Page
    x:Class="WSAnalogClock.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WSAnalogClock"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
 
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid Width="300" Height="300">
            <Ellipse Width="300" Height="300" Fill="Blue"></Ellipse>
            <!-- Second  -->
            <Rectangle Margin="150,0,149,150" Name="rectangleSecond" 
        Stroke="White" Height="120" VerticalAlignment="Bottom">
                <Rectangle.RenderTransform>
                    <RotateTransform x:Name="secondHand" CenterX="0" 
                CenterY="120" Angle="0" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <!---->
        
        <!-- Minute  -->
            <Rectangle Margin="150,49,149,151" Name="rectangleMinute" 
        Stroke="LightGreen">
                <Rectangle.RenderTransform>
                    <RotateTransform x:Name="minuteHand" CenterX="0" 
                CenterY="100" Angle="0" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <!---->
        
        <!-- Hour  -->
            <Rectangle Margin="150,80,149,150" Name="rectangleHour" 
        Stroke="LightYellow">
                <Rectangle.RenderTransform>
                    <RotateTransform x:Name="hourHand" CenterX="0" 
                CenterY="70" Angle="0" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <!---->
        
    </Grid>
    </Grid>
</Page>
 
The C# code to update the clock
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
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 WSAnalogClock
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        DispatcherTimer timer = new DispatcherTimer();
 
        public MainPage()
        {
            this.InitializeComponent();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start(); 
        }
        
        void timer_Tick(object sender, object e)
        {
            secondHand.Angle = DateTime.Now.Second * 6;
            minuteHand.Angle = DateTime.Now.Minute * 6;
            hourHand.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5);
        }
 
        /// <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)
        {
        }
    }
}

Visual Studio 2012 and .NET 4.5 Expert Development Cookbook

Visual Studio 2012 and .NET 4.5 Expert Development Cookbook 
One of the books I was technical reviewer for was recently released. I highly recommend you pick up a copy of Visual Studio 2012 and .NET 4.5 Expert Development Cookbook

 Chapter 1: Introduction to Visual Studio IDE Features
Introduction
Identifying the various components of Visual Studio IDE
Working with Solution Explorer and Class View
Working with the main workspace area of IDE
Navigating between code inside the IDE
Extending Visual Studio templates
Using Code Snippets in Visual Studio
Using Smart Tags and Refactor in Visual Studio

Chapter 2: Basics of .NET Programs and Memory Management
Introduction
Inspecting the internal structure of a .NET assembly
Working with different types of assemblies
Inspecting the major components of a .NET program
How to work with custom configurations for an application
How to disassemble an assembly
Securing your code from reverse engineering by using obfuscation
Understanding .NET garbage collection and memory management
How to find memory leaks in a .NET program
Solutions to 10 common mistakes made by developers while writing code
 Chapter 3: Asynchronous Programming in .NET
Introduction
Introduction to Threading and Asynchronous Threading patterns
Working with Event-based asynchronous pattern and BackgroundWorker
Working with thread locking and synchronization
Lock statement using task-based parallelism in concurrent programming
Working with async and await patterns
Working with Task Parallel Library data flows

Chapter 4: Enhancements to ASP.NET
Introduction
Understanding major performance boosters in ASP.NET web applications
How to work with statically-typed model binding in ASP.NET applications
Introduction to HTML5 and CSS3 in ASP.NET applications
Working with jQuery in Visual Studio with ASP.NET
Working with task-based asynchronous HttpHandlers and HttpModules
New enhancements to various Visual Studio editors
Chapter 5: Enhancements to WPF
Introduction
Getting started with WPF and its major enhancements in .NET 4.5
Building applications using MVVM pattern supported by WPF
Using the Ribbon User Interface in WPF
Using WeakEvent pattern in WPF
  Chapter 6: Building Touch-sensitive Device Applications in Windows 8
Introduction
Building your first Windows 8 style tiles application using JavaScript, HTML5, and CSS
Writing a library for WinJS
Building your first Windows 8 style tiles application using C# and XAML
Working with storage files in Windows 8 style tiles applications
Understanding the application life cycle of WinRT applications

Chapter 7: Communication and Sharing using Windows 8
Introduction
How to enable app to app sharing inside a Windows 8 environment
Working with notification and services
How to perform background transfers of data in Windows 8 style tiles applications

Saturday, March 9, 2013

Using the wmi to get a list of installed software

Introduction

Gets a list of Software installed on the local computer 

Building the Sample

Uses Visual Studio 2012 or Visual Studio 2012 Express for Windows Desktop,  The code listed below can be compiled with earlier versions of visual studio, visual basic express edition,  or c# express edition
 
Description
 
Uses the windows managment interface and visual basic to get a list of the installed installed software on the computer.  The name of the installed software will be displayed in an windows forms list box. You can use the classes inSystem.Management to query the windows managment interface to get information about the local computer.  In this case we query the Win32_Product class to get the installed software on the computer.   The name property in the returned data is what we will display in a windows form listbox.
 
The Win32_Product class returns the name, install location, install date, etc..  The Win32_Product class also function for unistalling the software
 
 
C#
   public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
             
 
    private void LoadSoftwareList() 
    { 
        listBox1.Items.Clear(); 
        ManagementObjectCollection moReturn;   
        ManagementObjectSearcher moSearch; 
 
        moSearch = new ManagementObjectSearcher("Select * from Win32_Product"); 
 
        moReturn = moSearch.Get(); 
       foreach(ManagementObject mo in moReturn) 
       { 
           listBox1.Items.Add(mo["Name"].ToString()); 
       } 
 
    } 
 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            LoadSoftwareList(); 
        }
 
Visual Basic
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
        LoadSoftwareList() 
    End Sub 
 
    Private Sub LoadSoftwareList() 
        ListBox1.Items.Clear() 
        Dim moReturn As Management.ManagementObjectCollection 
        Dim moSearch As Management.ManagementObjectSearcher 
        Dim mo As Management.ManagementObject 
 
        moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Product") 
 
        moReturn = moSearch.Get 
        For Each mo In moReturn 
            ListBox1.Items.Add(mo("Name").ToString) 
        Next 
 
    End Sub