WPF PreviewKeyDown Fired Multiple Times

I want to implement such feature: In a barcode text box, when a “=” key (or “+” key) is pressed, the “=” (or “+”) character will not append to barcode textbox, and program will use textbox text as barcode to search product item.

 

To achieve this effect, we need use handle PreviewKeyDown event. Listening for KeyDown event is not working here, because when KeyDown event is fired, the new character is already appended to the textbox. (PreviewKeyDown is a tunneling event and KeyDown is a bubbling event)

But when I pressed “=” key, the PreviewKeyDown event is triggered multiple times.

 

By debugging I found only one event’s IsDown property is true, while other triggered events’ IsDown property is false and their IsUp property is true. To fix this solution we can check whether e.IsDown is true.

C# WPF Generate and Display QR Code

Download QrCode.Net

Download and install QrCode.Net from NuGet, there are two ways to install it.

Right click your project, select Manage NuGet Packages, in newly opened NuGet Package Manager tab, choose Browse tab and type QrCode.Net in Search text box, and install the QrCode.Net package by clicking the download icon at right side.

Or

Run following command in Package Manager Console

 

Create Container Window

Create a window with name MainWindow and add an Image control named QrCodeImage into it.

MainWindow.xaml

 

Display QR Code in Window

In MainWindow.xaml.cs MainWindow constructor function, add following code (should be located after InitializeComponent() method)

 

Running the project you will see the QR Code is displayed in the MainWindow.

 

 

RavenDB NotSupportedException

When I run the code

 

I got following exception

RavenDB NotSupportedException

An exception of type ‘System.NotSupportedException’ occurred in Raven.Client.Lightweight.dll but was not handled in user code

Additional information: Could not understand expression: .Where(p => p.ProductSn.Contains(value(YSPOS.MainWindow+<>c__DisplayClass10_0).barCode))

 

It’s because there is no index for ProductSn attribute is created.

 

Solution

To fix this issue, we need create index fo ProductSn attribute.

 

Create a class extending AbstractIndexCreationTask class

 

Then execute this index creation task after DocumentStore initialization.

 

WPF Set StartupUri in code

Introduction

StartupUri is a property of Application class, which sets the first UI to show when application starts. By default it’s set in App.xaml file, e.g.

In above code the StartupUri value is MainWindow.xaml, and using default value is OK for most of times.

 

Why Set StartupUri in Code?

But what if you want to show other windows according to different conditions? Assume our application can open .proj files, so the file name will be passed in as command line arguments.  Now we want to show a file chooser dialog at startup if user run our application directly (without double clicking .proj files), and open another window (call it Project Explorer) when user start application by double clicking .proj file.

In this case we need to check command line arguments, then set corresponding StartupUri in code.

 

How to Do it

First in App.xaml.cs, add an override method OnStartup for Application class.

This method will be called at startup of application.

Setting StartupUri in OnStartup method is just a recommended way, you can set it in constructor as well. The execution order is: Constructor -> XAML Parsing -> OnStartup, so setting StartupUri in constructor will be overridden by XAML and OnStartup, and StartupUri in XAML will be overriden by OnStartup

 

Next we set the StartupUri according to whether filename is passed in.

In above code we call System.Environment.GetCommandLineArgs method to get command line arguments. You can see the StartupUri is actually an Uri object, its UriKind is UriKind.Relative. (If we don’t specify the UriKind parameter, we will get an UriFormatException)

 

 

C# Int (Integer) Infinity

Background

I’m implement a data structure which is used to convert a time range to human-readable text, e.g. 0-7 days to “Within 1 Week”, 7-30 days to “1 Week to 1 Month”. Here I use Tuple<int,int> to represent the time range but not two integers, because we can define a Dictionary<Tuple<int, int>, String> to represent a map from time range to text.

But how could we represent “Over 2 Weeks”? Apparently the second parameter of the Tuple should be infinity.

Infinity

To represent infinity for an integer, we can use int.MaxValue (it’s actually Int32.MaxValue)

So we can solve above problem using following code

 

IKVM.NET Convert JAR to .NET (C#, VB.NET) DLL

Why Convert JAR to .NET DLL

Imagine your company builds an online file converter, and its backend programming language is Java because of its position in web development. And in this project you build a file parsing library (jar format certainly) which is used to parse different types of files.

After this project is done, your boss decides to create a Windows desktop version for this software. Now the best choice is .NET, but how can you reuse your java parsing library? That’s why IKVM.NET existed.

IKVM.NET Introduction

IKVM.NET is a .NET implementation of Java which allows us to run Java code in .NET (C#, VB.NET, etc.) easily. It includes following three components:

  • JVM in .NET (which means you can run java byte code in .NET)
  • Java class library implementation
  • A compiler converting java byte code to .NET IL

There are many famous Java projects converted to .NET using IKVM.NET already, such as Tika, Curator, etc. Even Mono has include it as a built-in component.

Start Converting

This will generate a DLL file named libpst.dll from libpst.jar in current directory.

Without -target:library option, it will generate an exe file.

 

WPF ModernUI Tutorial Part 1

Introduction

ModernUI is a nice WPF theme and a styling framework which let you build a modern, beautiful application easily. Are you poor in designing skill and have problem with designing GUI for your WPF applicaiotn? If so, you have to try ModernUI, it is the best choice for you. In web design, there is Bootstrap for building great UI. And for WPF, it is Modern UI.

 

Screenshots

 

Installation

The installation is really easy, just search “ModernUI” in Manage NuGet Package Window, and select “ModernUI for WPF” item and click “Install”

Install ModernUI for WPF in NuGet Package Manager

Install ModernUI for WPF in NuGet Package Manager

Or run following command in Package Manager Console

 Install-Package ModernUI.WPF

 

Create a ModernUI Window

First create a new WPF Application Project named “ModernUISample”. After creating project, select MainWindow.xaml, change the XAML as following:

The ModernWindow class is defined under FirstFloor.ModernUI.Windows.Controls, so add following line to MainWindow.xaml.cs to import this namespace

Then let MainWindow inherit from class ModernWindow

 

Click “Debug” button to run the program, we will see a black rectangle with a light blue border is shown. What’s wrong with it? It’s because that we have not applied the theme files to our application.

Open App.xaml, and add following code:

 

 

Run the program again, you will see a modern window is shown

Modern UI blank window

Umbraco Admin URL

After installing Umbraco following Installation Tutorial on official website, I cannot found how to go to the admin panel. And Umbraco installation finish page only asks me to go to front page.

Then I need to ask google. After searching a while, I got that the default admin url is /umbraco, if the website url is redino.net, then the admin url is redino.net/umbraco

And this value can be modified in web.config file under appSettings section: