Pixytech

Category: C#

  • Nano ViewModel Support

    Nano view model is very small library to build MVVM enabled Silverlight/WPF/WP7 based application.MVVM becomes more and more popular in fast few years and there are many implementations  like MVVM Light toolkit, Prism(Last Version), nRoute, or Caliburn however these libraries have too many features and keep on growing which could have adverse effect on you project in terms of performance etc.For example communication between view in MVVM Light Toolkit is based on silverlight messaging which may not be best if you working ithin single silverlight application.It can be used when you page have more than one silverlight application (xap files) which requires communication and what about if you are not intead to use some of the feature from the toolkits i.e you want just the MVVM but you end up with adding complete set of library in your project.What if you need some features from MVVM Toolkit and you too want to use prism library.

    There are a number of benefits this pattern brings to both WPF and Silverlight development. Before you go on, ask yourself:

    • Do you need to share a project with a designer, and have the flexibility for design work and development work to happen near-simultaneously?
    • Do you require thorough unit testing for your solutions?
    • Is it important for you to have reusable components, both within and across projects in your organization?
    • Would you like more flexibility to change your user interface without having to refactor other logic in the code base?

    MVVM is not just about resuability,blendability & testability however its very power and if used properly you could create low latency WPF based applications.If you want to know about the theory you may follow the post here or google it and you will find many discussions and articles on MVVM.

    Below are the features supported by NanoVmSupport library

    • Minimum code in you view model.
    • No nead to have obvervable properties and extra code with each property.
    • Supports command implementation.
    • Command binding for all most all controls using EventToCommand implementation
    • Provision to set design time data for view model (supports blendability)
    • NanoVmSupport.Events libaray supports communication between two view models in you application.It is extracted from Prism library so you if you are familiar with prism its very easy for your.Please note that you don’t have to use prism to use this.If few months later support you want prism, you may exclude NanoVmSupport.Events library and your code will start using eventing from prism with just chnaging single line of initilization.
    • You can easyly write unit tests against your view models.

    The sample silverlight application below (MVVM & library MVVM.Events) is the implement of NanoVMSupport & NanoVMsupport.Events.The source code for sample project and NanoVMSupport library can be downloaded from here and if required binaries are here.
    [silverlight: http://www.pixytech.com/rajnish/uploads/code/NanoVM/MVVM.xap,650, 550]

    The sample folder in the source code also includes sample application code for silverlight and WPF.The WPF application is Auction Sniper for madbid which places your bids automaticaly based on some figures/process.
    For communication between viewmodels check the documentation from prism or send me your queries in this post,for Microsoft Unity Application block click here.

    MVVM common Issues & fixes in NanoVMSupport :

    • Standard Silverlight TextBox control is very useful but has one strange behavior: if you use TwoWay data binding and bind some property to controls Text property, when users type text into the control, this change is not propagated to the bound property until the control loses its focus. The AutoComit Behavior for textbox control is now part of NanoVMSupport Library (Lib for MVVM) for detail check this article “Silverlight TextBox AutoComit Behaviour“.
    • How to invalidate the CanExecute value : Suppose you have textbox and command button ,Can execute returns false if the length of text is zero otherwise it returns true and you have also used the above mentioned Auto Comit behaviour on text box.CanExecute will be re-evaluated when something interesting within the application’s window happens in WPF however in silverlight or with NanoVMSupport library you have to explictly update the ICommand.The one way of doing this is to change “RaisePropertyChange” method in ViewModelBase.cs as mentioned below
      protected void RaisePropertyChanged(string name)
      {
          PropertyChanged.Raise(this, name);
          PropertyChanged.Raise(this, "");
      
      }

      The code line PropertyChanged.Raise(this, “”); will refresh all the properties and icommands on view.But it may have performance issues if you have may properties and commands in viewmodal.The another recommended approach is to use following code in viewmodal

      public string SelectedText
      {
          get { return Get(() => SelectedText); }
          set
          {
              Set(() => SelectedText, value);
              base.RaisePropertyChanged("SearchCommand");
          }
      }

      The code base.RaisePropertyChanged(“SearchCommand”); will invalidate the SearchCommand.

  • Auction Sniper

    Auction Sniper is an eBay/Madbid sniper that automates the process of placing your eBay/madbid bid in the closing seconds of any eBay/Madbid bidding auction, dramatically increasing your chance of winning with huge saving to your bids.The sample application is WPF based windows application out of which as developer you will learn following tasks

    • NanoVMSupport Library for ViewModel (WPF & Silverlight).
    • Implementing MVVM in WPF.
    • Hooking web browser control for HTML events.

    Source code of sample project is available within NanoVMSupport article. Launch the application and navigate to any open bids (single bid – detailed view) and click on start button.If you are not logged-in the application will ask you to log-in or press cancel if you just want to test this.

    eBay implementation is not available in this project..

    *Please note that this is just an proff of concept and is not intead to for end users.The code has been developed from developer prospective and fun.

  • Face Detector

    Face detection from given picture has many applications including camera’s, scanners and other consumer application. The technique can be applied to improve the effectiveness of application like driving licence, passport generation, data entry applications etc. Recently one of my friends who is running his data entry business asked for solution for data entry of typical forms which consists candidate picture and relevant data. In tradition application usually one has to spent considerable amount of time in scanning, trimming picture. There is no such scanner or device available to do their job automatically and reduce the time spent in processing or increase the productivity. In this article i have written small .net based application which can be used in wide range of application and is based on open cv haar face detection to recognize front face from input picture.

    The application is just proof-off concept off-course fully working to detect face from camera. The application also implements motion detection from input picture and it will capture the picture only when motion is stabilized. Imaging if this could be integrated with-in scanner having auto feed tray. The application could be programmed to take pictures based of business specific parameters for e.g. for passports there is different specification as compared to driving licence photo’s. The concept could be integrated with Photo-Me (automatic photo machines) machines where current users has to position him self or move the camera using controller keys.
    The application will first wait for any motion if motion is detected and is stabilised for few seconds, it will capture the frame from camera and will be processed by application to detect face from picture and cut the face according to pre-set specification, Say if it detects two faces it will generate two pictures into strip. The concept can be further extended to automatically create data entry into some database with initial picture. The rest of the data will be manually entered by DTP operators. This process will improve the DTP eefficiency at least by 20 % and if it will be incorporated with scanner it may further improve the efficiency by 40 %.

    I have recently upgraded my OS to Windows 7 64 bit and the source code of this application is not working on this os.will publish the code once i have mannaged to fix the issue.

  • .Net Script Editor (C#,Vb.net Mini IDE)

    Complete Visual Studio IDE (source code included) in single user contol that can be used in your windows application to provide scripting feature in languages like c# abd vb.net.IDE have features like code intellisence,reference to .Net assemblies,COM libraries & web services.The code editor supports lot coloring for languages like c,c++ c#,vb.net,sql etc..Refrectoring features like converting c# code to vb.net code in single click.

    Add scripting functionality to your application,features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. Mini .Net IDE in single winform control.

    The source code / article published here is to provide features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. The control supports C# and VB.net language however can be easily modified to support any .net language. You can use COM references, .Net references & Web References. The control supports Visual studio IDE features like bookmark, highlighting interpreter errors & compiler errors, adding multiple classes and interfaces. You can imagine visual studio IDE build in single control with feature like AddObject from MS Script Control. To use this control you just need to drag and drop it to your winforms and add few lines of code to add instance of object which you want to access in the scripts.

    Source code at :
  • Silverlight WebSockets – Duplex Communication

    A duplex communication system is a system composed of two connected parties or devices that can communicate with one another in both directions. Duplex communication is required when you need to send some data in the reverse direction i.e. from Server to client. For scenario purpose assume that something happens on server (some event is received from source) and message from server should be sent to connected clients. Off-course the solution should be highly scalable with low latency client and server.

    Let’s talk about silverlight client in our scenario

     There could be couple of options with silverlight client demonstrated by Gill Cleeren In series of article “The duplex story: looking at duplex communication in Silverlight 4

    Silverlight 4.0 now supports 3 different types of duplex communications

    • HTTP Polling Duplex: Duplex requires that the server can itself initiate communication, which is not supported by HTTP, HTTP Polling duplex (HTTP long polling, or Comet-style) protocol allows us to do bi-directional communication over HTTP. What happens behind the scenes is the following:
      • The Silverlight client initiates the communication by sending an initial request to the service.
      • After the client is registered with the service, it continuously starts polling the service for updates.
      • Whenever the service has new messages to send to the client, they are queued up until a new poll arrives from the client.

    The HTTP Polling Duplex is therefore not real duplexing: it creates an illusion of duplex communication by polling (at network layer) so frequently that it looks as if the messages are pushed from the service to the client. If you still want to use this method here is the link for you to tune performance of http polling duplex.

    • Sockets: Socket is implemented through the use of a TcpListener, Being pure TCP endpoints, sockets are fast and true duplex communicators. However sockets in Silverlight can only work over ports between 4502 and 4534 making it unsuitable for duplex communication over the internet. Secondly you need to build policy server to provide clientaccesspolicy.xml for sockets.Note that Silverlight 4 also allows that the policy calls are made over HTTP.
    • net.tcp binding: this entirely new binding, added in Silverlight 4, also supports duplex communication. net.tcp combines advantages of both polling duplex and sockets. Its programming model is, just like polling duplex, based on WCF. That means that we can take advantage of automatic proxy generation inside Visual Studio.
    • Poll based communication: Not scalable, out of scope.

    So what’s next?

     HTML 5 – WebSockets

    Web socket protocol or WebSockets are “TCP for the Web,” a next-generation bidirectional communication technology for web applications being standardized in part of Web Applications 1.0.

    The Web Sockets API enables web applications to handle bidirectional communications with server-side process in a straightforward way. Once you get a Web Socket connection, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler. The protocol is not raw TCP because it needs to provide the browser’s “same-origin” security model. It’s also not HTTP because web socket traffic differers from HTTP’s request-response model. you do need a new server implementation to communicate with web sockets .

    web socket prototype is out now at http://html5labs.interoperabilitybridges.com/prototypes/available-for-download/websockets/html5protos_Download or websocket msi from here

     for more information see : http://tomasz.janczuk.org/2010/12/websockets-wcf-service-silverlight-and.html

    Please also find the published websocket transport binding (Incomplete project)  here which i was trying to build using custom duplex WCF channel.