Android Addicted Apps Creator

August 31, 2009

SQLtoCS Class Generator – an SQL Table to C Sharp class generator.

Filed under: WPF — alinberce @ 18:47
Tags: , ,

Well, I’m back and I’m bad… as someone used to say. I’m really not that bad, but, for sure I know more. Because I’ve started to work on a new project for a customer of mine I felt the need to develop a tool to help me do stuff faster. One big pressure came from the fact that I develop this app in my free time which isn’t really that much. So while eating, sleeping, rebuilding house, staying with my wife, waiting for our unborn little girl, I managed to crop some lines of code. I also finalized reading a WPF book from start till last page. And man… I must say it again: WPF Rocks!!! Tananana it rocks.

What does this app do you ask: in nontechnical words, it does what I hate do do (repeated  work) 🙂 in technical words it creates two classes for an Sql Server table. I’ve always been a fan of nTier architecture and the way WPF and C# works it this concept pleases me. And in big lines, this program does the business layer (Business objects) of a nTier architecture. Or at least, this is what I like to think it does.

Here is a screenshot:

Capture

The classes are generated in a few seconds. Just enter the credentials for the SQL Server or SQL CE connection, select Database and the table and press the Generate button. There are a few customizable parameters: class name, collection name and the namespace. The cs classes are saved in the exe’s directory.

The program detects what keys (PK and FK) are contained by the table and generates methods based on them.There are a lot of tools that do this on the net.  I’ve tested quite a few. There was always something bothering me and this is the main reason I started doing one for myself.

Please take your time and test it. If you have suggestions, please sent it to me.

UPDATE: The application can be downloaded from here: SqlToCs

A short, bad quality Youtube presentation:

Ah,  and about that PayPal button.. hehe… Donations are never required but they are more than welcomed.

June 5, 2009

WPF Localization – Do you speak English ? Sprechen Sie Deutsch? 你讲中文?

Filed under: WPF — alinberce @ 19:59
Tags: , , ,

    By using localization, you allow controls in a application to change their content (particularly text labels and images) according to user preferences or according to current culture settings of the Windows operating system. In WPF unit of localization is in the XAML file (the compiled BAML resource). This means that for every language supported by the application you will have a BAML resource.

Let’s begin by creating a new WPF Application named WpfLocalization. In it we will add two buttons and a text block each with it’s English text. The Windows1.xaml file will look like this:

<Window x:Class="WpfLocalization.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Button  Margin="30" Name="button1">Save</Button>
        <Button  Margin="30" Name="button2">Cancel</Button>
        <TextBlock Margin="30">Item saved succesfully</TextBlock>
    </StackPanel>
</Window>

As you can notice, I’ve already started to create the English version of the application.

First thing we need to do is add localization support for our project. To do this, just open the WpfLocalization.csproj file and add the following line

<UICulture>en-US</UICulture>

in the <PropertyGroup> section of the file. Reload the project and let’s continue. We need also to set the NeutralResourceLanguage assembly attribute. The simplest way to do this is open the AssemblyInfo.cs file in the Properties Folder and uncomment the following line:

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

By setting this we tell the CRL loader how to find the correct resource DLL for the selected culture. If it can’t find the indicated culture he will use the neutral one.Run the application and go to the Debug folder. In there we have the exe file and a folder en-US which contains the US culture resource DLL.

All the controls that need to be localized must have the Uid attribute. This means that we must modify in our .xaml files, all the controls. Pretty awful stuff to do manually. But we are lucky, Microsoft thought of this and helps us by offering the msbuild tool that will do the job automatically for us.

Start a command prompt and go to the following path: C:\Windows\Microsoft.net\Framwork\v3.5 in here is the msbuild.exe which we will use.

Run the following command:

msbuild /t:updateuit C:\WpfLocalization\WpfLocalization.csproj

The result should look like this:

image

And if you reload the project, in window1.xaml the stack panel is updated and it should have the Uid attribute for it’s elements.

<StackPanel x:Uid="StackPanel_1">
        <Button x:Uid="button1" Margin="30" Name="button1">Save</Button>
        <Button x:Uid="button2" Margin="30" Name="button2">Cancel</Button>
        <TextBlock x:Uid="TextBlock_1" Margin="30"> The item was saved succesfully</TextBlock>
    </StackPanel>

If you think that this was hard to do, wait to see what’s coming. Now we must extract the resources as a .csv file and translate the text as wanted. To do this we will use the Locbaml.exe tool. Copy the LocBaml.exe to bin\Debug\en-US folder of the project. Also copy the executable file WpfLocalized.exe to the same folder. Open a command prompt and go to the en-US folder and run the following command.

Locbaml /parse WPFLocalization.resources.dll /out:translate.csv

The result of this command will be the creation of a translate.csv file. Open this file and replace the English words with the German translation:

  • Save -> Sichern
  • Cancel -> Abbrechen
  • The item was saved succesfully! -> Posten erfolgreich gespeichert

After the translation is done, we need to generate the localized satellite DLL file and install it. Create a sister directory to en-US and name it de-DE. To do this we need to use the Locbaml file again. Open a command window and navigate to the folder where translate.csv is. Run the command:

Locbaml /generate /trans:translate.csv /out:..\de-DE /cul:de-DE WpfLocalization.resources.dll

We managed to complete the localization of the code. The result isn’t visible yet. Because we don’t want to change the Windows region settings to see the localized version, we will programmatically make the switch in the app.xaml.cs file.

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //string sCulture = "de-DE";
            string sCulture = "en-US";
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(sCulture);
            System.Threading.Thread.CurrentThread.CurrentCulture =
                new System.Globalization.CultureInfo(sCulture);
            base.OnStartup(e);
        }    
    }

Final result is encouraging :)  Happy coding.

image image

The video lesson: (sorry about the video, it’s quality is absolutely horrible, I uploaded at high quality but it looks really rubish)

 

May 28, 2009

WPF – the almighty Button Tooltip

Filed under: Uncategorized — alinberce @ 20:28
Tags: , ,

A very brief tutorial about making a Button Tooltip that contains not just text, but images too. WFP Rocks!