HowTo: Insert new line and spacing at content of WPF TextBlock control

While adding more voice commands at SpeechTurtle, I had to update the active legend where the available commands are displayed and their text is highlighted as the respective commands are recognized by the speech recognition engine (using .NET’s managed Speech API).

A problem I faced was how to add a newline and prefixing the 2nd line with some spaces to align with the first command at the 1st line…

First of all, adding a new line was easy, inserted a <LineBreak /> tag in the XAML inside the TextBlock tag’s content, as shown in the screenshot below:

image

Then I had the issue that I needed to add some spacing so that “Pen up” at the 2nd line starts exactly under the start of “Forward” at the 1st line (see screenshot above)…

Tried to add a Run tag, with its Text set to some spaces, but couldn’t get an exact positioning:

image

So I tried using a Separator instead, since I could define a Width for it, however, it was drawing as a gray line:

image

So I either had to change its color to White or Transparent, or use a Null Foreground Brush on it (one difference of a Null brush from a Transparent one is that the element ignores Mouse events in that case from what I remember), or just set its Visibility mode to Hidden:

image

Do note that WPF has another visibility mode apart from Visible and Hidden, that is Collapsed, where the respective control disappears from the layout flow (that value is not supported in Silverlight from what I remember), which is not what we wanted in this case (Hidden was the correct option to choose):

image

Suggestion: property and event setting block attached to C# type instance

Instead of having to write in C#

      speechRecognizer = CreateSpeechRecognizer();
      if (speechRecognizer != null)
      {
        speechRecognizer.SomeProperty = someValue;
        speechRecognizer.SomeOtherProperty = someOtherValue;
        speechRecognizer.SpeechRecognized += SpeechRecognized;
        speechRecognizer.SpeechHypothesized += SpeechHypothesized;
        speechRecognizer.SpeechRecognitionRejected += SpeechRecognitionRejected;
      }

I’d prefer to write:

      speechRecognizer = CreateSpeechRecognizer() {
        SomeProperty = someValue,
        SomeOtherProperty = someOtherValue,
        SpeechRecognized += SpeechRecognized,
        SpeechHypothesized += SpeechHypothesized,
        SpeechRecognitionRejected += SpeechRecognitionRejected
      }

that is after any Type I’d like to be able to add {…} block with assignments to its properties, like I can do when I create a new type.

Do note that I also would like this syntax and the existing new Type(…) {…} syntax (which is a subset of this one) to support assignment and removal of event handlers, not just setting of properties, as shown in the example above

If you like this suggestion, vote it up at:

http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/9479250-property-and-event-setting-block-attached-to-c-ty

Managed .NET Speech API links

(this is my answer at http://stackoverflow.com/questions/14771474/voice-recognition-in-windows)

I’m looking into adding speech recognition to my fork of Hotspotizer Kinect-based app (http://github.com/birbilis/hotspotizer)

After some search I see you can’t markup the actionable UI elements with related speech commands in order to simulate user actions on them as one would expect if Speech input was integrated in WPF. I’m thinking of making a XAML markup extension to do that, unless someone can point to pre-existing work on this that I could reuse…

The following links should be useful:

http://www.wpf-tutorial.com/audio-video/speech-recognition-making-wpf-listen/

http://www.c-sharpcorner.com/uploadfile/mahesh/programming-speech-in-wpf-speech-recognition/

http://blogs.msdn.com/b/rlucero/archive/2012/01/17/speech-recognition-exploring-grammar-based-recognition.aspx

https://msdn.microsoft.com/en-us/library/hh855387.aspx (make use of Kinect mic array audio input)

http://kin-educate.blogspot.gr/2012/06/speech-recognition-for-kinect-easy-way.html

https://channel9.msdn.com/Series/KinectQuickstart/Audio-Fundamentals

https://msdn.microsoft.com/en-us/library/hh855359.aspx?f=255&MSPPError=-2147217396#Software_Requirements

https://www.microsoft.com/en-us/download/details.aspx?id=27225

https://www.microsoft.com/en-us/download/details.aspx?id=27226

http://www.redmondpie.com/speech-recognition-in-a-c-wpf-application/

http://www.codeproject.com/Articles/55383/A-WPF-Voice-Commanded-Database-Management-Applicat

http://www.codeproject.com/Articles/483347/Speech-recognition-speech-to-text-text-to-speech-a

http://www.c-sharpcorner.com/uploadfile/nipuntomar/speech-to-text-in-wpf/

http://www.w3.org/TR/speech-grammar/

https://msdn.microsoft.com/en-us/library/hh361625(v=office.14).aspx

https://msdn.microsoft.com/en-us/library/hh323806.aspx

https://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.requestrecognizerupdate.aspx

http://blogs.msdn.com/b/rlucero/archive/2012/02/03/speech-recognition-using-multiple-grammars-to-improve-recognition.aspx

Suggestion: implement ignore keyword or allow missing catch block in C#

This is a suggestion I’ve just sent in via Visual Studio’s “Send a frown” feature (rewritten here a bit differently from memory, since that feedback channel doesn’t allow you to access your previous feedback as the Microsoft Connect or the Uservoice site does) :

Instead of having to write

try
{
  …
}
catch (SomeExceptions)
{
  //NOP (ignored)
}

I’d like to be able to write

try
{
  …
}
ignore (SomeExceptions);

 

and similarly if a finally block is used, allow:

try { … }

ignore(SomeExceptions)

finally { … };

That is, I suggest adding an “ignore” keyword, introduced to C# in order to ignore specific exceptions.

Alternatively, could allow a missing catch block, so that the catch (…) clause would be able to be followed directly by a “;” to end the try/catch statement, or by a “finally” clause and its code (sub)block.

try
{
  …
}
catch (SomeExceptions);

 

and when finally block is used:

try {…}

catch(SomeExceptions)

finally { … }

Suggestion: Allow new in C# without giving Type

This is a suggestion I’ve just sent in via Visual Studio’s “Send a frown” feature:

Instead of writing statements like:

  List<CultureInfo> result = new List<CultureInfo>();

in C# I’d prefer to be able to write

  List<CultureInfo> result = new ();

inside the () one would be able to pass contructor parameters and also they should be able to use

SomeClass v = new (someParam) { someProperty = …, otherProperty = … };

syntax, that is be able to initialize properties of the class

 

What I mean is that I want to omit the type name after the new, since it is deduced from the type I have given to the new var.

Doing

  Type v = new (…){…};

is more general from the alternative of doing

  var v = new Type(…){…};

since you would be also able to even do

  someCollectionType.Add(new (…){…});

to add a new member to a typed collection

and also would be able to do

  SomeMethod(new (…){…})

where the Type would be deduced from the param of the method (obviously if it is not overloaded with more versions with just 1 param)

 

Update:

In fact, now that I think of it again, the concept of anonymous types in C# could be merged with the suggested one, making the (…) optional if no constructor parameters are to be used (in anonymous types you only give the {…} part with the properties’ initialization). 

The compiler would then resort to making a new anonymous type only if it can’t deduce a type from the usage context. 

Of course if the (…) part (constructor parameters) are given, it would never try to make an anonymous type if it can’t find a matching type to instantiate with the respective constructor signature to call.

HowTo: Free up some disk space by disabling hibernation on Windows 10

I have a small older Tablet PC (Lenovo S10-3t) that I’m running Windows 10 on, having replaced its internal classic SATA hard disk with an SSD one, and since SSD space is scarce (being more expensive I got a smaller in size disk than the original one), I needed to make free space.

Since that machine is always connected to power, connected to a monitor mounted on the wall (and turned into tent mode in front and under the main monitor, to use it as a second touch-enabled screen for app testing), I don’t really need to use hibernation (after all its battery has gone dead, so hibernation won’t get a chance to occur in the case of an electrical network power down anyway).

That could save some extra disk space, since when hibernation is enabled, you end up with a hiberfil.sys file in the root folder of the boot disk that has around the size of the memory on your computer (or at least that was the case before Windows 10, since I was seeing an 850MB file although the computer has 2GB memory).

To cut it short, I looked it up and found this relevant Microsoft article:
https://support.microsoft.com/en-us/kb/920730

However, the automated way it suggests (the FixIt app) doesn’t seem to work on Windows 10 (probably neither on Windows 8, it doesn’t list it at the bottom of the page anyway) and one has to use the manual way. On Windows 10 the quickest way (based a bit on the suggestions in that article), is to right click the start menu button at the bottom-left of your screen and select “Command Prompt (Admin)” at the popup menu. Then reply affirmatively at the User Access Control (UAC) prompt shown and at the command prompt (a dark console window) that appears, type powercfg.exe /hibernate off and press the ENTER key. Then just close the console window and check the “This PC” node at the File Explorer to confirm that you saved some extra hard disk space.

Categories: Posts Tags: , , , ,

Fix: Cleanup after upgrading from Windows 10 technical preview

I recently replaced the internal hard disk of my old Lenovo S10-3t Tablet PC with an SSD and installed Windows 10 technical preview, but recently realized the hard disk had almost run out of space.

Trying to figure out why, I realized that upgrading from the Windows 10 technical preview version to the final Windows 10 version (this happened automatically via Windows Update), left back a “Windows.old” folder at the hard disk root taking up 3.14GB, as if I had upgraded from Windows 7 or Windows 8 via Microsoft’s free OS upgrade offer (that offer is valid for a year btw, so make sure you don’t miss it).

You’ll notice the Disk Cleanup tool (you can find it by pressing the search icon (magnifying glass) at the Windows taskbar and writing “cleanup”, temporary Windows installation files are also mentioned as taking up an extra 3.22 GB, but there is some double counting there, since I ended up with 5.30GB free after cleanup, from around 200MB I had left on the hard disk before I run Disk Cleanup (note that I already had run CCleaner, but I hadn’t selected the option there to cleanup files from previous Windows installation, since I didn’t expect to have any such).

image

Note, that Disk Cleanup will even warn you that you won’t be able to restore the machine back to the previous Windows version (aka the technical preview), but why should you care to do so anyway?

image

Update:

To make some extra disk space and since I use a fast SSD, I had set Windows to compress the hard disk contents (one can set this option by right clicking the disk and selecting Properties), so probably there is no double-counting by Disk Cleanup dialog, it just must be showing the uncompressed space those things it cleans take up. So it could indeed be 3.14GB + 3.22GB of useless space taken up by updating Windows technical preview to the official Windows 10 release via the normal automatic Windows Update process, which is quite a lot.

Microsoft should show some warning to the user about all this extra space taken up (right away after updating and offer to remind them in the future again if they opt to keep the files till they’re confident the latest version works OK) and offer them the choice of cleaning this up

Another interesting thing I notice is that although I had selected the option to compress the drive and it had applied respective attribute to all files (showing a progress dialog), it didn’t remember that setting (not sure if I had set it before the upgrade), so probably it wasn’t compressing newer files.

image

Categories: Posts Tags: , , , , ,
Follow

Get every new post delivered to your Inbox.

Join 1,022 other followers

%d bloggers like this: