Sunday, September 18, 2011

Finding a child element in silverlight

Today I am going to share a simple code that will help us to find a child element from a silverlight control. In one of our control we had a requirement to find out the internal scrollviewer, by default we didn't had support to find the internal element right way.

So the only way is to try finding it with VisualTreeHelper. And do a recursive call to find out if the Element With the name exists or not. we can use couple of modification depending ton Name or Type, bellow I have shared a Extension method which demonstrate the idea.

public static class FrameworkElementExtensions
    {
        public static FrameworkElement FindChild(this FrameworkElement element, string targetName)
        {
            if (element == null || string.IsNullOrEmpty(targetName))
                return null;
            if (element.Name == targetName)
                return element;
            for (int index = 0; index < VisualTreeHelper.GetChildrenCount(element); index++)
            {
                var child = (FrameworkElement)VisualTreeHelper.GetChild(element, index);
                if (child.Name == targetName)
                    return child;
                child = FindChild(child, targetName);
                if (child != null)
                    return child;
            }
            return null;
        }

        public static object TryFindResource(this FrameworkElement element, object resourceKey)
        {
            var currentElement = element;

            while (currentElement != null)
            {
                var resource = currentElement.Resources[resourceKey];
                if (resource != null)
                {
                    return resource;
                }

                currentElement = currentElement.Parent as FrameworkElement;
            }

            return Application.Current.Resources[resourceKey];
        }


Wednesday, September 14, 2011

Event Aggregator in Prism 4.0 with MEF

Today I am going to share a simple trick about Event Aggregator, I was working in one of my project and I was trying to use event aggregator. I was stopped by a show stopper exception, and couldn’t find out what is going on. here is the expectation that I had.

image

Looks familiar? I bet some of you had encounter it. well the solution is pretty easy while subscribing an event you got to also set your thread option and off course a true value to keep reference alive parameter of the subscribe method.

public InboxViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
var myDemoEvent = _eventAggregator.GetEvent<MyDemoEvent>();
if (myDemoEvent != null)
myDemoEvent.Subscribe(OnEvent,ThreadOption.UIThread,true);
}
private void OnEvent(MyCustomObject obj)
{
//do some thing over here
}



I already assume that you know about how to make a custom event for Event aggregator. If not here is a code sample. The main catch is that it have to be inherited from CompositePresentationEvent<>. The code sample is given bellow.

 public class MyDemoEvent:CompositePresentationEvent<MyCustomObject>
{
//here goes thing about your cutom event
}

public class MyCustomObject
{
//here goes you custom event payload stuff
}



Till next post happy programming.

Sunday, September 11, 2011

Prism 4.0 is a complete application development framework for silverlight and WPF.

Today I am going to talk about Prism 4.0 today. We had a very satisfactory history of developing a large scale smart client application with prism 2.0. Prism 4.0 released on November 2010. And its more evolved and now also support application development with Silver-light.

As dependency container prism 4.0 have both unity and MEF. So user can choose to use anyone of them. Unity is been introduced since the first release of WPF Composite application block aka Prism.

You can download the prism distribution from code plex. http://compositewpf.codeplex.com/.

You can find a complete developer guide here http://msdn.microsoft.com/en-us/library/gg406140.aspx.
Bellow I have put down a screen shot of the distribution.


we have complete set of tutorial and samples in the downloadable distribution to get started with prism.
I myself recommend MEF samples and find it very useful to use.

I am planing to write a series of post for both enterprise library 5.0 and Prism. Till next post, this is Munna Signing out.

Sunday, September 4, 2011

Is the item is visible in scroll viewer view port?

Today I am going to share an interesting trick that I learned. Some time we need to find out in WPF items control weightier an item is visible in scroll viewer view port. I am not going to put down the xaml for you rather think that we have a Listbox and there are Items inside the Listbox, and while scroll we want to find out if a item is visible in viewport or not, we might want to decorate only the items that are visible. Bellow I have put down the code which is pretty self explanatory.

        private void HandleScrollChanged(object sender, ScrollChangedEventArgs e)
{
ShowVisibleItems(sender);
}

private void ShowVisibleItems(object sender)
{
var scrollViewer = (FrameworkElement)sender;
foreach (DataItem item in listbox.Items)
{
var listBoxItem = (FrameworkElement)listbox.ItemContainerGenerator.ContainerFromItem(item);
if (IsFullyOrPartiallyVisible(listBoxItem, scrollViewer))
{
//your code goes here
}
}
}

protected bool IsFullyOrPartiallyVisible(FrameworkElement child, FrameworkElement scrollViewer)
{
var childTransform = child.TransformToAncestor(scrollViewer);
var childRectangle = childTransform.TransformBounds(new Rect(new Point(0, 0), child.RenderSize));
var ownerRectangle = new Rect(new Point(0, 0), scrollViewer.RenderSize);
return ownerRectangle.IntersectsWith(childRectangle);
}



The above code works if “var listBoxItem = (FrameworkElement)listbox.ItemContainerGenerator.ContainerFromItem(item); “ return not null that is the virtualization not enabled but if virtualization is working then it wont work, this particular code works perfectly with ItemsPanel is been customized to fill with a warpPanel.


Hope this will help some one. Till next post, Happy programming.

Saturday, September 3, 2011

“Microsoft Enterprise Library 5.0” , Improved collection of application blocks.

Enterprise library 5.0 is been released in may 2011, and has new and improved set of Application blocks, And has all the necessary application blocks that is necessary for you application, specially   “Cryptography Application Block” and “Security Application Block”. Bellow I have listed all the application block and the data is taken from original pattern and practice site

Microsoft Enterprise Library 5.0 contains the following application blocks:

  • Caching Application Block. Developers can use this application block to incorporate a cache in their applications. Pluggable cache providers and persistent backing stores are supported.
  • Cryptography Application Block. Developers can use this application block to incorporate hashing and symmetric encryption in their applications.
  • Data Access Application Block. Developers can use this application block to incorporate standard database functionality in their applications, including both synchronous and asynchronous data access and returning data in a range of formats.
  • Exception Handling Application Block. Developers and policy makers can use this application block to create a consistent strategy for processing exceptions that occur throughout the architectural layers of enterprise applications.
  • Logging Application Block. Developers can use this application block to include logging functionality for a wide range of logging targets in their applications. This release further improves logging performance.
  • Policy Injection Application Block. Powered by the Interception mechanism built in Unity, this application block can be used to implement interception policies to streamline the implementation of common features, such as logging, caching, exception handling, and validation, across a system.
  • Security Application Block. Developers can use this application block to incorporate authorization and security caching functionality in their applications.
  • Unity Application Block. Developers can use this application block as a lightweight and extensible dependency injection container with support for constructor, property, and method call injection, as well as instance and type interception.
  • Validation Application Block. Developers can use this application block to create validation rules for business objects that can be used across different layers of their applications.

 

References

http://msdn.microsoft.com/en-us/library/ff632023.aspx

Friday, September 2, 2011

Limitations of SQL Server Compact Edition 4.0

After some research on net I found few limitations of Microsoft new Embedded database i.e. SQL Server Compact 4.0 the findings are given bellow.

 

According to Scott Guthrie “Max size”

“It currently supports databases up to 4GB in size - although we are looking to hopefully increase that.  It supports foreign keys.  It does not supports sprocs or triggers.”

 

According to books online

SQL Server Compact 3.5 SP2 is the appropriate version for using the following features.

Data replication with SQL Server

SQL Server Compact 4.0 does not support data replication with SQL Server using Sync Framework, merge replication or remote data access (RDA). In order to use this feature, you should use SQL Server Compact 3.5 SP2 version.

SQL Server Integration Services

SQL Server Compact 4.0 does not support SQL Server Integration Services (SSIS). In order us this feature you should use SQL Server Compact 3.5 SP2.

SQL Server Management Studio

Starting from SQL Server Compact 4.0, SQL Server Compact does not support SQL Server Management Studio. The Transact-SQL Editor in the Visual Studio 2010 Service Pack 1 can be used to run T-SQL queries and to view the estimated and the actual query plans for a T-SQL query for the SQL Server Compact database.

LINQ to SQL

SQL Server Compact 4.0 does not support the LINQ to SQL functionality. You can use this feature in SQL Server Compact 3.5 SP2. For more information, see LINQ to SQL (SQL Server Compact).

Windows Mobile, Windows Phone and Windows CE devices

SQL Server Compact 4.0 does not have a release for Windows Mobile, Windows Phone or for Windows CE devices.

Looking at documentation I also found this “SSCE_M_RECORDTOOBIG” error token is raised when “The table definition or the row size exceeds the maximum row size of 8060 bytes” that means a row can have max 8k of data, sounds stupid.

Its has support of “Number of concurrent connections” unto 256.

I still need to know few information's, and looking forward to get those information in near future.

  1. What are the max number of column in a table?
  2. What are the max number of row in a table?
  3. What are the max number of table in database?

If any one know this information please  share.