Wednesday, March 28, 2007

Camtasia 4.01 for Vista is shipping

Finally, TechSmith released the final new version of their Camtasia Studio. Now, it's Microsoft Windows Vista compatible. I will now explain about this product, but just in case, this is the best tool for screen capturing ever. All those who had Camtasia used, know it.

Currently, I'm using their beta version, downloaded a couple of months ago. It's running fine on Vista, but still there are some issues.

For all those who have not purchased this tool, do it - it worth each penny of $299 individual license.

Grid animation

One of most common questions, I meet with my customers is how to animate grid size. Actually, there is no problem to animate doubles, ints, even sizes, but how to animate GridLength, which actually used to measuring the sizes in grid?

In order to do it, we'll create our own animation, named GridLengthAnimation and use it within our grid. So, let's start.

First of all we have to inherit base animation timeline in order to get the functionality we want.

 

public abstract class GridLengthAnimationBase : AnimationTimeline
    {
        protected GridLengthAnimationBase() { }
        public override sealed Type TargetPropertyType
        {
            get { return typeof(GridLength); }
        }
        public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
        {
            if (defaultOriginValue is GridLength == false)
                throw new ArgumentException("Parameter must be a GridLength", "defaultOriginValue");
            if (defaultDestinationValue is GridLength == false)
                throw new ArgumentException("Parameter must be a GridLength", "defaultDestinationValue");
            return GetCurrentValueCore((GridLength)defaultOriginValue, (GridLength)defaultDestinationValue, animationClock);
        }
        public abstract GridLength GetCurrentValueCore(GridLength defaultOriginValue, GridLength defaultDestinationValue, AnimationClock animationClock);
    }

Next step is to create actual animation. It's pretty simple, when we already have base type. We have to create some Dependency Properties used within timeline, such as By, From, To etc. and treat of some possible errors (we do not want our animation to be thrown :) )


 



public class GridLengthAnimation : GridLengthAnimationBase
{
    public static readonly DependencyProperty ByProperty = DependencyProperty.Register(
     "By",
     typeof(double?),
     typeof(GridLengthAnimation),
     new PropertyMetadata(null));
    public static readonly DependencyProperty FromProperty = DependencyProperty.Register(
     "From",
     typeof(double?),
     typeof(GridLengthAnimation),
     new PropertyMetadata(null));
    public static readonly DependencyProperty ToProperty = DependencyProperty.Register(
     "To",
     typeof(double?),
     typeof(GridLengthAnimation),
     new PropertyMetadata(null));
    public double? By
    {
        get { return (double?)this.GetValue(ByProperty); }
        set { this.SetValue(ByProperty, value); }
    }
    public double? From
    {
        get { return (double?)this.GetValue(FromProperty); }
        set { this.SetValue(FromProperty, value); }
    }
    public double? To
    {
        get { return (double?)this.GetValue(ToProperty); }
        set { this.SetValue(ToProperty, value); }
    }
    protected override Freezable CreateInstanceCore()
    {
        return new GridLengthAnimation();
    }
    public override GridLength GetCurrentValueCore(GridLength defaultOriginValue, GridLength defaultDestinationValue, AnimationClock animationClock)
    {
        if (From == null)
            throw new Exception("From must be specified in a GridLengthAnimation");
        double a_to;
        if (To != null)
            a_to = To.Value;
        else if (By != null)
            a_to = From.Value + By.Value;
        else
            throw new Exception("Either To or By must be specified in a GridLengthAnimation");
        return new GridLength(From.Value + ((a_to - From.Value) * animationClock.CurrentProgress.Value));
    }
}

That's all, folks. All we have to do now is to use our new animation type with our grid as we know to use any other type of animation. It's looks like this


 


<BeginStoryboard>
              <Storyboard>
                <ParallelTimeline>
                  <local:GridLengthAnimation
                    Storyboard.TargetName="l"
                    Storyboard.TargetProperty="(ColumnDefinition.Width)"
                    From="30"
                    To="300"
                    AutoReverse="False"
                    Duration="0:0:1"/>
                </ParallelTimeline>
              </Storyboard>
            </BeginStoryboard>

Hurrah, from now we know to build any type of animations and actually do not need Microsoft to provide us with premade one. Do't it looks like working with converters? The answer is "yes".


Source code for this article

Tuesday, March 20, 2007

Adding Web References to your VS project by code

Today one of my clients asked me to find a way to add web references to existing VS project without using Visual Studio wizard, to do it by code. I built VS add-ins before, so I know what Studio can do, but the question is if I can replace existing system dialogs, without writing a lot of code (I'm really lazy, if I'll write a lot, I'll have no time to write here). So I start to check the material. Really, there is very few information regarding build VS addins, much less regarding creation of references, and nothing about creation web references. So let's deep into VS to understand what's happens there.

While adding web references (the references to Web Services), Visual Studio performs a couple of action. First it checks and retrieves the WS information, then it checks if the current project has local reference to System.Web.Services (the default WinForms project has not). The it discovered the service, by using discovery client protocol, collects the information regarding it's methods by using their public descriptors, then reveals XML schema and based on this schema generates web proxy by using WSDL. Wow, a lot of stuff. How to get rid on it?

Let's discover the world of DiscoveryClientProtocol from Discovery namespace of Web.Services assembly. It has a couple of really interesting methods such as Discover... or Resolve... By using those methods we can create the web service protocol

DiscoveryClientProtocol protocol = new DiscoveryClientProtocol();
            protocol.DiscoverAny(url);
            protocol.ResolveOneLevel();

The next step is to get descriptors. In order to do it, we'll iterate References property of the protocol and create ContractReference and DiscoveryReference for each of methods. We'll add them to our services collection



ServiceDescriptionCollection services = new ServiceDescriptionCollection();
            foreach (DictionaryEntry entry in protocol.References)
            {
                ContractReference contractRef = entry.Value as ContractReference;
                DiscoveryDocumentReference discoveryRef = entry.Value as DiscoveryDocumentReference;
                if (contractRef != null)
                {
                    services.Add(contractRef.Contract);
                }
            }


Let's get the schema of our service in order to generate proxy later it's pretty easy, especially when we have our protocol ready



XmlSchemas schemas = new XmlSchemas();
            foreach (DictionaryEntry entry in protocol.References)
            {
                SchemaReference schemaRef = entry.Value as SchemaReference;
                if (schemaRef != null)
                {
                    schemas.Add(schemaRef.Schema);
                }
            }

Next step is proxy (code) generation. In order to use it, we'll have to ask CodeDom and CodeCompile classes to help up. Actually, we can also call WSDL.exe to generate it, but if we already deep in code, let's create it



ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
 
            foreach (ServiceDescription description in serviceDescriptions)
            {
                importer.AddServiceDescription(description, null, null);
            }
 
            foreach (XmlSchema schema in schemas)
            {
                importer.Schemas.Add(schema);
            }
 
            System.CodeDom.CodeNamespace codeNamespace = new System.CodeDom.CodeNamespace(proxyNamespace);
            CodeCompileUnit codeUnit = new CodeCompileUnit();
            codeUnit.Namespaces.Add(codeNamespace);
            ServiceDescriptionImportWarnings warnings = importer.Import(codeNamespace, codeUnit);
 
            CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                options.BracingStyle = "C";
                provider.GenerateCodeFromCompileUnit(codeUnit, sw, options);
            }

Now we have generated files (Reference.map with it's cs, Service.disco and Service.wsdl) all we have to do now is to put them together and reference somehow to our project.



string path = Path.GetDirectoryName(project.FullName)+@"\Web References";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
 
            Directory.CreateDirectory(path + @"\" + name);
 
            path += @"\" + name;
 
 
            protocol.WriteAll(path, "Reference.map");
 
            ServiceDescriptionCollection services = GetServiceDescriptionCollection(protocol);
            XmlSchemas schemas = GetXmlSchemas(protocol);
            GenerateWebProxy(name, path + @"\Reference.cs", services, schemas);

So far so good. In order to do something with our project we'll build add-in for Visual Studio, that can pass us the current project and work with it. I'll not explain how to build addins here, ask Google Live to help :)


Just the small explanation for how to get current project, while in DTE _applicationObject.ActiveDocument we only have a document). DTE Document has something, called ProjectItem (we'll meet them closer later) inside this Item we have property named ContainingProject, that actually holds the parent project of this item.



internal static Project GetCurrentProject(Document currDoc)
        {
            if (currDoc == null)
                return null;
 
            ProjectItem prjItem = currDoc.ProjectItem;
            if (prjItem == null)
                return null;
 
            Project currPrj = prjItem.ContainingProject;
            if (currPrj == null)
                return null;
 
            return currPrj;
        }

Good, but in order to add reference to the project we need Visual Studio Project in other words VSProject. This special project holt by secret property of the Project named Object and their namespace is VSLangProj, that dug in VSLangProj assembly in Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies (are they really so "public" ? :)


Now let's take a look on this project. Oh my g-d! It has method named ADDWEBREFERENCE, that received url string as input. Just throw all you did off and use it in order to add web reference to your project. Smart VSTS will do all the rest for you!



 



internal static ProjectItem AddWebReference(Project project, string url, string name)
        {
            VSProject vsProj = project.Object as VSProject;
            if (vsProj == null)
                return null;
 
            ProjectItem item = vsProj.AddWebReference(url);
            try
            {
                item.Name = name;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
                item.Remove();
            }
 
            return item;
 
        }

Have a nice day,  Visual Studio development and documentation team (this methods exists in MSDN, but researchable for some reason :))


Source code for this article

Monday, March 19, 2007

How to bind to Animation To and FROM properties

If you tried to bind to From and To values of Animation anything, you sure pay attention to the behavior, that the animation object just ignored you in the best case, in worth case, you couch very strange an annoying exception (if used from code-behind) "Cannot freeze the storyboard timeline tree for use across threads". What's the problem?

First of all, just note in your notepad: "When you start an animation, Timeline object copying itself frozen into internal boundary. There it creates Clock object and begin to run. So, how to change properties of the animation and make them occur immediately.

First of all, you can recreate whole storyboard. Rather costly and  very bad idea. You can make some woodoo on Completed event. For example, call invalidation of the animation to affect your changes. Good idea, but still a lot of mess. Another try is to work with FillBehavior. This one make you able to tell your animation to stop or to hold up for something else. Very good and very efficient method is to deal with internal clocks. For example, recreate or exchange them by your own. This is really hardcore. Maybe other day I'll explain this methods, but today, we'll just call new BeginAnimation to restart it. This method is not very efficient, but better then nothing. In order to do it, we'll deal today with EventTriggers.

So let's build the simple application, that continuously  changes the size of rectangle, based on values you provided with sliders. So, here we go.

First of all, let's create an animation

<Storyboard x:Key="myAnim">
      <ParallelTimeline>
        <DoubleAnimation
          Name="myAnimWidth"
          Storyboard.TargetName="myRect"
          Storyboard.TargetProperty="Width"
          From="100"
          To="500"
          Duration="0:0:5" 
          RepeatBehavior="Forever"
          AutoReverse="True"
                />
        <DoubleAnimation
          Name="myAnimHeight"
          Storyboard.TargetName="myRect"
          Storyboard.TargetProperty="Height"
          From="100"
          To="500"
          Duration="0:0:5"
          RepeatBehavior="Forever"
          AutoReverse="True"
                />
      </ParallelTimeline>
    </Storyboard>

We'll put in into resources in order us to be able to reuse and rerun it. The next step is to create two sliders, a couple of textblocks and the rectangle. I'll not rewrite a code here, 'cos it's really straight forward and we have nothing tricky with it. Now, I want to start the animation right after my page was loaded, so I'll add Page.Trigger to my Page.Loaded event. Pay attention, this method makes you able to almost any event from XAML. This is really cool feature. So, let's do it. 



<Page.Triggers>
    <EventTrigger RoutedEvent="Page.Loaded">
      <EventTrigger.Actions>
        <BeginStoryboard Storyboard="{StaticResource myAnim}"/>
      </EventTrigger.Actions>
    </EventTrigger>
  </Page.Triggers>

So far so good. The next step is to rerun the animation each time I'm changing the value of my sliders. As well as done with Page, Slider (as well as any other ContentPresenter has it's own event. So let's restart the animation on value change



<Slider Name="slFrom" Value="50" Width="150" Minimum="50" Maximum="600">
        <Slider.Triggers>
          <EventTrigger RoutedEvent="Slider.ValueChanged">
            <EventTrigger.Actions>
              <BeginStoryboard Storyboard="{StaticResource myAnim}"/>
            </EventTrigger.Actions>
          </EventTrigger>
        </Slider.Triggers>
      </Slider>

Rather simple, isn't it? We done, now we have our storyboard restarted and "invalidated" each time the bindings of From="{Binding ElementName=slFrom, Path=Value}" and To="{Binding ElementName=slTo, Path=Value}" where changed.


That's all, folks


Source code for this article

Sunday, March 18, 2007

Multithread logger in WPF

So, we already know to write very nice WPF application, but one of most common modules in every application is logger. How to use WPF capabilities to build smart logger. What to do with multithreading environment, when methods InvokeRequired is absent?

Today, we'll build logger, based on DataBinding of WPF, learn how to know what thread we are in and how to bind resources to ListView. Pretty enough? Let's start

First of all we have to build our XAML page. It sill consists of checkbox, binded to static variable that tells us if we have to log everything. So, we'll add simple boolean, named m_isDebug and bind it to the checkbox. But how? m_isDebug is simple variable, not dependency property, how to get it and bind to control. This part is really simple. We'll do it this way ("l" is our clr-namespace: xmlns:l="clr-namespace:WPFLogger")

<CheckBox IsChecked="{x:Static l:Window1.m_isDebug}">Debug view</CheckBox>

So far, so good. Now the main part of the application, our XmlDataSource - the log file. It's really simple to add the structure to our XAML

<Window.Resources>

  <XmlDataProvider x:Key="myDebugData" XPath="/myXmlData">
    <x:XData>
      <myXmlData xmlns="">
        <Item Date="00/00/00 00:00:00" Source="None" Message="Testing..."/>
      </myXmlData>
    </x:XData>
  </XmlDataProvider>
</Window.Resources>

Event simpler to get it from the code



protected void OnLoad(object sender, RoutedEventArgs e)


        {
            debug = this.Resources["myDebugData"] as XmlDataProvider;
            debug.IsAsynchronous = true;
            WriteLog(this, "Initialized...");            
        }


Let's build the static method, that knows to write to this Data Provider. This method should know where he called from in order us to be able to get debug information later. We do not have to leave our project in Debug mode to get StackTrace information. So, the method will looks like this:



if (debug != null && m_isDebug)
                {
                    System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(false);
 
 
                    XmlElement item = debug.Document.CreateElement("Item");
 
                    item.SetAttribute("Date", DateTime.Now.ToString("dd-MMM-yy HH:mm:ss.ffff"));
                    item.SetAttribute("Source", sender!=null?sender.ToString() + ".":Thread.CurrentThread.Name + stack.GetFrame(1).GetMethod().Name);
                    item.SetAttribute("Message", data);
 
                    debug.Document.SelectSingleNode(debug.XPath).AppendChild(item);
                }

StackTrace will tell us where we are together will caller attribute or thread information, if inaccessible. We are speaking about threads, is this method is thread safe? Not really. We are interacting with "debug" object, and updates ListView, so we have to be sure that we are in the right (STA) thread. There is no such thing InvokeRequired in WPF, due the new object, named Dispatcher tries to help us with thread synchronization. It knows what thread the caller exists and make us able to BeginInvoke and invoke our method within the right context. In order to get such information, we add a couple of things. First of all delegate to this method, next we'll add to the method check of context and then invoke or execute the rest



internal delegate void WriteLogDelegate(object sender, string data);
 
internal static void WriteLog(object sender, string data)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new WriteLogDelegate(WriteLog), sender, data);
            }
            else
            {
                if (debug != null && m_isDebug)
                {
                    System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(false);
 
 
                    XmlElement item = debug.Document.CreateElement("Item");
 
                    item.SetAttribute("Date", DateTime.Now.ToString("dd-MMM-yy HH:mm:ss.ffff"));
                    item.SetAttribute("Source", sender!=null?sender.ToString() + ".":Thread.CurrentThread.Name + stack.GetFrame(1).GetMethod().Name);
                    item.SetAttribute("Message", data);
 
                    debug.Document.SelectSingleNode(debug.XPath).AppendChild(item);
                }
            }
        }

The rest is really simple, we'll add listview, binded to our xml data provider, a couple of nice features, like saving, sorting etc within the data source. All the rest you'll see in the source code attached.


The usage of new WPF logger is really simple. Just call WriteLog(object, string) from any place in any thread you want and the result will appears in listbox immediately. The are a couple of FXes I'd add to the logger, such as priority, display or write flag and more, But all those are really application specific, so do it yourself :)


Source code for this article

Friday, March 16, 2007

Global Events Hooking

WPF has really great engines for global hooking. Dependency Properties/Objects are only small part of those engines. Today, we'll look into Routed Events and EventManager

Let's take a scenario, where I want to handle OnClosing event of all windows in my application. The propose is clear - popup an exclamation asking me to approve this action.

The "legacy" way is to create kind of BaseWindow, derived from Window Class and override onClosing event to popup my message. This approach means, that I have to inherit BaseWindow in all windows or instead of creation of Window instance, create Basewindow one.

But, I want to do it easily, and I'm in WPF world, so let's start new WPF project and put into App.xaml.cs class following lines

 

protected override void OnStartup(StartupEventArgs e)
        {
            EventManager.RegisterClassHandler(typeof(Window), Window.LoadedEvent, new RoutedEventHandler(WindowLoaded));
 
            base.OnStartup(e);
        }

Done. Now let's understand what we did. We register globally routed event Window.Loaded for each new instance of Window class, so each time this event will be fired (that not mention, if we have or will have an instance of window), EventManager will subscribe to it and execute WindowLoaded method. The rest is really simple. Let's implement WindowLoaded and subscribe the loaded instance to our message popping


 



void WindowLoaded(object sender, RoutedEventArgs e)
        {
            Window w = sender as Window;
            if (w != null)
            {
                w.Closing += new System.ComponentModel.CancelEventHandler(w_Closing);
            }
        }
 
void w_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Really [with EventManager]?", "Workaround window", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                e.Cancel = true;
        }

That's all, folks. Now each window (or window derived class) in my system, even those how I do not know will ask me about closing each time, I'll try to close them.


Nice, don't you think?

Tuesday, March 13, 2007

How to set your mobile modem to work without dialer

So, all those happy owners of Orange GSM modem Novatel Merlin U740 and Windows Vista already paid their attention to bugs of it's software dialer MobiLink. Someone called to Orange customer service to get answers, and understand, that their are not officially support Windows Vista. Someone get new drivers (via YouSendIt.com - please someone tell my why Orange can not just put those drivers to their website). But even the new version of Mobilink is really junk

I believe, that  most of you have problem while entering Stand by or Hibernate mode after using the MobiLink dialer, while the dialer can not  properly close itself at all this what you see (after the dialer close) in process manager

So, how to solve it? How to set your modem work with access points you want without using their dialer. No problem. Let's do it.

All we have to do is to create two regular dialers for two possible orange 3G access point - UInternet and UWap (as well as configured in your mobile phone). But before creating a dialer let's put all setting inside the modem? How it's possible? Novatel Merlin U740 is very good hardware modem. It has it's own memory able to save upto 100 network locations.

So, open HyperTerminal (oh, my godness, there is not hyper terminal in Windows Vista, so open Putty, IMHO, the best open source terminal, SSH, and telnet client) and connect the COM port your modem sits in. Obvious, that you should have you modem in and connected (see in phone and modem options what com port it sits on)

Now in terminal windows type "at+cgdcont=?" (without quotes) to get your modem capabilities. You can see, that you have 1 upto 99 "IP" locations. Let's use slot one and two. Don't you want check what the modem has saved before usage? Sure, type "at+cgdcont?" to get this information.

Now let's put our location in slot 1 and 2. Type (case sensitive, two separate lines):
at+cgdcont=1,"IP","uinternet";
at+cgdcont=2,"IP","uwap.orange.co.il";

Done, now you have UINTERNET access point configures in slot 1 and UWAP in slot 2. Check this by typing "at+cgdcont?" (no quotes). If you see following - you did it!

The only thing we have to do is to tell your dialer use first or second (or both) slot. To do it add to default service center phone number ***X#, where X - your slot number. So to use UWAP, enter "*99***2#" (without quotes).  Please remember to have you modem in and selected for this connection while setting it.

Dunno. Don't forget to set proxy server (192.118.11.55:8080) in IE's Internet Options for UWAP AP. For UInternet you don't need to do it.

That's all folks, now plug your modem in and dial the required connection by using Windows Vista Connection Manager and forget the buggy MobiLink.