Tuesday, February 28, 2017

Center WPF window in Win32 window

I needed to add a new WPF window, while developing Visual Studio Package. And it would be good for it to be centered in parent.
I've found an old msdn blog post that was supposed help in this. However it is not ideal and has some missing things. Let's fix that.

Sunday, March 24, 2013

Show 'translating' message while translating using Microsoft Translation Widget

Recently I had a task to translate exisiting website using Microsoft Translator Widget. One of the tasks was to display something that will indicate that translation is in progress.

Friday, December 16, 2011

Stopping save operation from Silverlight control inside CRM 2011

CRM 2011 allows to insert Silverlight controls inside forms. And when control is not just to show data, but to interact with user sometimes you may need to prevent form being saved until user finished some operation. It is quite easy to achieve in javascript, but not that obvious in Silverlight.
Let’s start. CRM 2011 SDK help gives us first clue – Xrm.Page.data.entity has addOnSave method. That’s exactly what we need. But how can we attach event to that object?
First thing to do is to get that object. As CRM 2011 SDK and Training Kit recommend us following way:

Xrm.Page.data.entity
  1. dynamic Xrm = HtmlPage.Window.GetProperty( "Xrm" ) as ScriptObject;
  2. dynamic entity = Xrm.Page.data.entity as ScriptObject;

Next step is to call addOnSave method. Handlers of OnSave method returns boolean value so lets attach some method returning boolean like this

  1. entity.Invoke( "addOnSave", new Func<bool>( OnSave ) );

But just returning bool is not enough to stop save operation. And again SDK helps us here: ExecutionObj.getEventArgs().preventDefault(). It ‘Cancels the save operation, but all remaining handlers for the event will still be executed’. ExecutionObj is something passed to the save event handler. Since we don’t know exact type of this object we will use dynamic:

Attach event
  1. entity.Invoke( "addOnSave", new Func<dynamic, bool>( OnSave ) );

And after this event handler looks like this:

Event handler
  1. private bool OnSave( dynamic eventData )
  2. {
  3.   if( !IsReadyToSave )
  4.   {
  5.     eventData.getEventArgs().preventDefault();
  6.     return false;
  7.   }
  8.  
  9.   return true;
  10. }

That’s it.
P.S. You may choose to return true instead of false when stopping save operation depending on whether you need other handlers to be executed or not. But usually false is enough
P.P.S. When your Silverlight control is inside html webresource that is after that placed on crm 2011 form, you will have to change way how you are getting Xrm reference to something like this

  1. dynamic parent = ( ScriptObject )HtmlPage.Window.GetProperty( "parent" );
  2. Xrm = parent.Xrm;

Thursday, October 20, 2011

Internet Synchronized Clock using C#. Part 2

In previous part we got some methods just to get time from internet. But this may be not enough for the real clock. Let’s tell us current UTC time and will synchronize with internet servers from time to time to ensure accuracy.
First of all, lets put all methods from previous part inside some class, let it be called InternetTimeGetter.
We want to have clock which is synchronized with internet. Since synchronization may take some time or even fail we don’t want our application to freeze while performing synchronization. This means that we need background thread for this. We may use Task class for this. Also we will need some variables to keep current time value and last synchronization time. Let’s start

Friday, July 1, 2011

Internet Synchronized clock using C#. Part 1

Task – show clock that is synchronized with some internet time server.

In this part I’ll just try to get UTC time from the server and in Part 2 I’ll use this code to create simple clock.
Some search pointed me to the http://tf.nist.gov/tf-cgi/servers.cgi page. It contains list of servers placed in US which provide UTC time on demand. Another page from that site (http://www.nist.gov/pml/div688/grp40/its.cfm) gives information about supported protocols. One of the is Network Time Protocol (RFC-1305) and another one is Daytime Protocol (RFC-867). Network Time Protocol (NTP) is a little bit more complicated and it provides more features. Daytime Protocol is simpler, but it should be just enough for this task.

Tuesday, May 17, 2011

Writing into Visual Studio Output Window. Part 2


In Part 1 we set up OutputWindowWriter class which performs writing to the Visual Studio output window. What else can we add here?
It would be great if everything worked fine from the first attempt, but this not always happens, especially when dialing with someone’s else code. In our situation we have problem - when output window is initially hidden (by auto hide option at the top right corner of the window next to the cross) and we are writing something there we may get something like this
image

Sunday, May 1, 2011

Selecting first item in WPF ListView and keyboard navigation

I have been playing with WPF ListView recently and I’ve met its following not quite
comfortable behavior – when ListView is focused and I’m trying to navigate inside it using keyboard (in my case it was down arrow) I had to press down key twice to move to the second item and then once for each next item. My goal was to fix that twice pressing (and also enable correct navigation when some other item is selected, not the first one). I thought that it can be archieved by some simple property like SelectedIndex, but despite setting it to necessary value it still didn’t work.