Software Architecture-Sliverlight 4.0-New Feature-FullScreenOptions property

September 28, 2010

Silverlight supports full screen mode. So there are 2 modes, one embedded mode and second full screen mode.
Full screen mode looks like as:

Embeded screen mode looks like as:

Full screen mode support was already there in previous versions of Silverlight. But in Sliverlight 4.0, we have a new feature by FullScreenOptions property.

Lets suppose a sample with following three radio button with xaml as:

First button code use IsFullScreen property to false (default value is false- So it can be avoided for embedded code mode) as:

private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rdo = sender as RadioButton;
            if (rdo.IsChecked==true)
            {
                App.Current.Host.Content.IsFullScreen = false;
            }
        }

Second button code set the full screen mode to true. Secondly FullScreenOptions
is set to None-means that full screen mode will not be maintained when screen is not active.[It is default]

private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rdo = sender as RadioButton;
            if (rdo.IsChecked == true)
            {
                App.Current.Host.Content.FullScreenOptions = System.Windows.Interop.FullScreenOptions.None; // default value-No need to specify.
                App.Current.Host.Content.IsFullScreen = true;
            }
        }

Thrid button code set full screen mode to true. Secondly, FullScreenOptions is set to StaysFullScreenWhenUnfocused means that full screen mode will be maintained when screen is not active.

private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rdo = sender as RadioButton;
            if (rdo.IsChecked == true)
            {
                App.Current.Host.Content.FullScreenOptions = System.Windows.Interop.FullScreenOptions.StaysFullScreenWhenUnfocused;
                App.Current.Host.Content.IsFullScreen = true;
            }
        }

An option to save setting for FullScreenOptions is asked by silverlight as:

You can see the setting info by right clicking the Silverlight page as below:

Limitation of Full screen mode:
1. No Drag-and-drop support
2. No OpenFileDialog and SaveFileDialog dialog boxes support.
3. No Multitouch input support.
4. Most keyboard events are disabled.


Software Architecture-Silverlight 4-New Feature-Implicit Style

September 28, 2010

Silverlight 3 Style:
In Silverlight 3, style was supported as by specifying the Style property in the control.
One Style can be based on other Style as shown the code below.

<Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <Style x:Key="ButtonStyle" TargetType="Button">
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="Background" Value="Red" />                
            </Style>
            <Style x:Key="NewButtonStyle" TargetType="Button" BasedOn="{StaticResource ButtonStyle}">
                <Setter Property="FontSize" Value="20" />
                <Setter Property="FontFamily" Value="Times New Roman"></Setter>
            </Style>

        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="500"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" ></RowDefinition>
            <RowDefinition Height="100" ></RowDefinition>
            <RowDefinition Height="100" ></RowDefinition>
        </Grid.RowDefinitions>
        <Button Name="btnClick" Grid.Column="0" Grid.Row="0" Width="300" Height="40" Content="Hello Sarwar" Style="{StaticResource ButtonStyle}" ></Button>
        <Button Name="btnClick2" Grid.Column="0" Grid.Row="1" Width="300" Height="40" Content="Hello Sarwar" Style="{StaticResource NewButtonStyle}" ></Button>
        <Button Name="btnClick3" Grid.Column="0" Grid.Row="2" Width="300" Height="40" Content="Hello Sarwar"></Button>
    </Grid>

So Output will be as:


First button is attached with style named “ButtonStyle”. Second button is attached with style named “NewButtonStyle” which is based on parent style named “ButtonStyle”.
Third button is not attached to any style, so it is with any style.

Sliverlight 4 Style:
With Silverlight 4, now we have implicit style feature which provide functionality to style control by specifying the TargetType property.

So Style for Button control can be defined as:

<Grid x:Name="LayoutRoot" Background="White">        
        <Grid.Resources>
            <Style TargetType="Button" >
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="Background" Value="Red" />
                <Setter Property="FontSize" Value="15" />
                <Setter Property="FontFamily" Value="Times New Roman"></Setter>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="600"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" ></RowDefinition>
            <RowDefinition Height="100" ></RowDefinition>
            <RowDefinition Height="100" ></RowDefinition>
        </Grid.RowDefinitions>
        <Button Name="btnClick" Grid.Column="0" Grid.Row="1" Width="300" Height="40" Content="Hello Sarwar"></Button>
    </Grid>

It will apply to all the Buttons inside this Grid scope and will look like as:


If Style tag is added with x:Key=”MyStyle”, then implicit style feature will no longer work.

WPF was having this implicit style feature previously(Before WPF 4.0), but now Silverlight 4 also has this feature.


Software Architecture-Silverlight 4-New Feature-Flow for Arabic and Hebrew

September 28, 2010

Silverlight 4 has introduced a new support for Right to Left Flow for Arabic and Hebrew.
All UI elements derived from frameworkElement has property of FlowDirection.
Default value is LeftToRight.
For Arabic, it can be changed to RightToLeft.

For arabic, TextBox will be placed as:

<TextBox Name="textBox1"  Width="300" Height="30" FlowDirection="RightToLeft"/>

Enjoy Silverlight 4.0………..


Software Architecture – New Features in C# / .Net 4.0 – Blog Series

September 18, 2010

In last few weeks, I have posted many posts related to new features in .Net 4.0, following are some of the important things.
Layout Rounding
https://softarchitect.wordpress.com/2010/09/15/software-architect-new-feature-wfp-4-0-and-silverlight-4-0-layout-rounding/

Enum – Hasflag
https://softarchitect.wordpress.com/2010/09/15/software-architecture-c-sharp-4-0-new-feature-enum-hasflag/

Memory Mapped File
https://softarchitect.wordpress.com/2010/08/15/software-architecture-c-sharp-4-0-new-feature-memory-mapped-file/

Synchronized Data Structures
https://softarchitect.wordpress.com/2010/08/15/software-architecture-c-sharp-4-0-new-feature-synchronized-data-structures-thread-safe-queue/

Tuple
https://softarchitect.wordpress.com/2010/08/15/software-architecture-c-sharp-4-0-new-feature-tuple/

Managed Extensibility Framework
https://softarchitect.wordpress.com/2010/08/14/software-architecture-c-sharp-4-0-new-feature-whywhathow-of-managed-extensibility-framework-mef-sample-calculator-code/

Lazy Initialization
https://softarchitect.wordpress.com/2010/08/14/software-architecture-c-sharp-4-0-new-feature-lazy-initialization/

Dynamic Type
https://softarchitect.wordpress.com/2010/08/08/software-architecture-c-sharp-4-0-new-feature-dynamic-type/

Covariant, contravariant and invariant:
https://softarchitect.wordpress.com/2010/08/06/software-architecture-confusion-everywhere-covariant-contravariant-and-invariant/

Named Parameters and Optional Parameters
https://softarchitect.wordpress.com/2010/07/25/software-architecture-c-4-0-new-feature-named-parameters-and-optional-parameters/

Best of luck for coding in .Net 4.0


Software Architecture – Sync Data between offline/Disconnected databases – SSIS – BIDS

September 18, 2010

Problem definition:
ERP Product is installed in head office and 5 branch offices such that each location installation has its on database. Locations are not connected via Internet that is they are acting as diconnected databases.

We want to sync databases of different branches.

Solution:
Some possible solutions to problem are as:
• Replication
• ETL
• Sync Providors
• Sync Framework
• Change Data Capture
We will compare and analyze pros and corns of these method in separate post. But here we will use ETL approach to solve this problem.
At closing of each working day, data is fetched from each branch database and new data is posted to other branches and head office database Server via email. So each database read email and executes the database differences from other databases.
So we can break whole process in 4 programming Steps.
1. Extract data from source database and convert them into a text file with data/time stamp attached.
[Implemented as SSIS Package and run via C# Code]

For details see post here

2. Read files from a folder in step 1, and send them as attachment in email via Outlook.
[Implemented by interop of MS Outlook API]

For details see post here

3. On other side, emails are pulled automatically via outlook and attachments are downloaded and saved in a local folder in hard disk.
[Implemented by interop of MS Outlook API]
For details see post here

4. Text files s are read from folder and loaded in to database.
[Implemented as SSIS Package and run via C# Code]

For details see post here

Future consideration:

1. How to identify the differential of data between the daily move of data.
2. This solution is working for one table how to handle more tables for data sync process.
3. There are many hardcoded parameters that needs to be fetched from configuration files.

Source: download from here Rename the file to ssis-solution-toupload.zip to extract.
Feedback is welocme


Software Architecture – To Load the data from files of a folder to table of SQL Server via SSIS in SQL Server Business Intelligence Development Studio

September 18, 2010

Objective:
Objective of this post is to load the data from all the files in a folder to a database of SQL Server via SSIS in SQL Server Business Intelligence Development Studio 2008 .

Environment:
SSIS + SQL Server Business Intelligence Development Studio 2008 + C# 4.0

Steps:

1. Create a new Package named “LoadData.dtsx” in BIDS 2008.
2. Create a new variable with name “FileName”.
3. Drag and drop a “Foreach loop container” to SSIS designer.
Set the Enumerator type and configuration settings as shown below.

Set “variable mapping” as shown below.

4. Drag a data flow task on SSIS Designer.
5. On the Data Flow tab, drag and drop the “Flat File Source” from the Data Flow Source in the Data Flow Tool Box. Click on New to create the New Flat File Source Connection Manager. Uses a property expression on the ConnectionString property of a Flat File Connection Manager to point to the file indicated by User::FileName

6. On the Data Flow tab, drag and drop the “OLE DB Designation” from the Data Flow Designation in the Data Flow Tool Box. Create the new OLE DB Designation connection manager by clicking on “New” button on the OLE DB Designation Editor.
Configuration will look like as:

7. So at the end of these changes, Control Flow will look like as:

And Data Flow will look like as:

8. Run the Package from Solution explorer to see the output.
9. Package can be run by following c# code.

string pkgLocation = @"F:\\e-pros\\testbest\\SSISFirst\\SSISFirst\\SSISFirst\\ExtractData.dtsx"; 
 
        Package pkg; 
        Microsoft.SqlServer.Dts.Runtime.Application app; 
        DTSExecResult pkgResults; 
        Variables vars; 
 
        app = new Microsoft.SqlServer.Dts.Runtime.Application(); 
        pkg = app.LoadPackage(pkgLocation, null); 
 
      //  vars = pkg.Variables; 
      //  vars["A_Variable"].Value = "Some value";                
 
        pkgResults = pkg.Execute(null, null, null, null, null);
 
        if (pkgResults == DTSExecResult.Success)
        {
            Console.WriteLine("Package ran successfully");
 
        }
        else
            Console.WriteLine("Package failed"); 

Related Post: https://softarchitect.wordpress.com/2010/09/18/software-architecture-sync-data-between-offlinedisconnected-databases-ssis-bids/


Software Architecture – To read emails with attachments via C# 4.0 + Office Interop Outlook

September 18, 2010

Objective:
Objective of this code is to read attachments from emails in MS outlook and save in a folder on hard disk.

Environment:
MS Visual Studio 2010 + C# 4.0 + Office Interop Outlook
Solutiuon:
The types and members of the Microsoft.Office.Interop.Outlook namespace provide support for interoperability between the COM object model of Microsoft Outlook and managed applications that automate Outlook.

 | oApp = new Microsoft.Office.Interop.Outlook.Application();
            oNameSpace = oApp.GetNamespace("MAPI");
            oNameSpace.Logon(null, null, true, true);
            oInboxFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            foreach (var item in oInboxFolder.Items)
            {
                Microsoft.Office.Interop.Outlook._MailItem oMailItem = item as Microsoft.Office.Interop.Outlook._MailItem;                
                Microsoft.Office.Interop.Outlook.Attachments ii = oMailItem.Attachments;
                for (int j = 1; j <= ii.Count; j++)
                {
                Microsoft.Office.Interop.Outlook.Attachment aa = (Microsoft.Office.Interop.Outlook.Attachment)ii[j];
                string mypath = @"F:\\FileOutbox\\" + aa.FileName;
                mypath = @mypath;
                aa.SaveAsFile(mypath);
                }
                oMailItem.Delete();
            }
            

Related Post: https://softarchitect.wordpress.com/2010/09/18/software-architecture-sync-data-between-offlinedisconnected-databases-ssis-bids/


Software Architecture – To send email with attachments via C# 4.0 + Office Interop Outlook

September 18, 2010

Objective:
Objective of this code is to read files from a folder, compose an email via MS Outlook and send email with all the files attached to it.

Environment:
MS Visual Studio 2010 + C# 4.0 + Office Interop Outlook
Solutiuon:
The types and members of the Microsoft.Office.Interop.Outlook namespace provide support for interoperability between the COM object model of Microsoft Outlook and managed applications that automate Outlook.

 oApp = new Microsoft.Office.Interop.Outlook.Application();
            oNameSpace = oApp.GetNamespace("MAPI");
            oNameSpace.Logon(null, null, true, true);
            oOutboxFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox);
            Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            oMailItem.To = "shahzad@comsoftuae.com";
            oMailItem.Subject = "Daily Backup Dated -" + DateTime.Now;
            oMailItem.Body = "Daily Backup Dated -" + DateTime.Now;
            oMailItem.SaveSentMessageFolder = oOutboxFolder;
            //uncomment this to also save this in your draft      
            oMailItem.Save();
            /// Send all files in Backup Folder
            DirectoryInfo dir = new DirectoryInfo("F:\\e-pros\\testbest\\SSISFirst\\SSISFirst\\bin\\");
            foreach (var item in dir.GetFiles())
            {
                oMailItem.Attachments.Add(item.FullName, Type.Missing, Type.Missing, Type.Missing);
            }
            // Clear all files from backup folder
            foreach (var item in dir.GetFiles())
            {
                item.Delete();
            }
            
            //adds it to the outbox      
            oMailItem.Send();

Related Post: https://softarchitect.wordpress.com/2010/09/18/software-architecture-sync-data-between-offlinedisconnected-databases-ssis-bids/


Software Architecture – To extract the data from a table of SQL Server and convert it to text file via SSIS in SQL Server Business Intelligence Development Studio

September 18, 2010

Objective:
Objective of this post is to extract the data from a table of SQL Server and convert it to text file having name as datetime appended via SSIS in SQL Server Business Intelligence Development Studio 2008 .

Environment:
SSIS + SQL Server Business Intelligence Development Studio 2008 + C# 4.0
Steps:

1. Create a new Package named “ExtractData.dtsx” in BIDS 2008.
2. Create a new variable with name “FileLocation” and set its value to folder name, lets say “F:\FileInbox”
3. Append variable “FileLocation” value with datetime stamp by adding a “Script Task” as:
Dts.Variables[“User::FileLocation”].Value = Dts.Variables[“User::FileLocation”].Value.ToString() + “\\data_” + DateTime.Now.ToString(“MMddyyyy_hhmmss”) ;
This command will define the name of data file to be generated.
4. Drag a data flow task on SSIS Designer.
5. On the Data Flow tab, drag and drop the “OLE DB Source” from the Data Flow Sources in the Data Flow Tool Box. Create the new OLE DB source connection manager by clicking on “New” button on the OLE DB Source Editor. Specify the command that will fetch the data from the table as:

SELECT TOP 1000 [AreaId]
,[AreaCode]
,[Description]
,[Active]
,[CreatedDate]
,[ModifiedDate]
,[BranchId]
FROM [GeneralAreaMaster]

6. On the Data Flow tab, drag and drop the “Flat File Destination” from the Data Flow Destinations in the Data Flow Tool Box. Click on New to create the New Flat File Destination Connection Manager. Configure properties accordingly.
7. So at the end of these changes, Control Flow will look like as:

And Data Flow will look like as:

8. Run the Package from Solution explorer to see the output.
9. Package can be run by following c# code.

string pkgLocation = @"F:\\e-pros\\testbest\\SSISFirst\\SSISFirst\\SSISFirst\\ExtractData.dtsx"; 
 
        Package pkg; 
        Microsoft.SqlServer.Dts.Runtime.Application app; 
        DTSExecResult pkgResults; 
        Variables vars; 
 
        app = new Microsoft.SqlServer.Dts.Runtime.Application(); 
        pkg = app.LoadPackage(pkgLocation, null); 
 
      //  vars = pkg.Variables; 
      //  vars["A_Variable"].Value = "Some value";                
 
        pkgResults = pkg.Execute(null, null, null, null, null);
 
        if (pkgResults == DTSExecResult.Success)
        {
            Console.WriteLine("Package ran successfully");
 
        }
        else
            Console.WriteLine("Package failed"); 

Related Post: https://softarchitect.wordpress.com/2010/09/18/software-architecture-sync-data-between-offlinedisconnected-databases-ssis-bids/


Software Architect – New Feature – WFP 4.0 and Silverlight 4.0 – Layout Rounding

September 15, 2010

Layout Rounding is a new concept in WFP 4.0 and Silverlight 4.0
Which means that the layout rendering engine rounds off non integer values during layout pass.

UIElement Class has a UseLayoutRounding property. The default value of this property is true, which means that layout rounding is on by default.

Example:

Lets put some xaml on silverlight page as:

<Grid x:Name="LayoutRoot" Background="White" UseLayoutRounding="False" Width="388">
        <Button Content="Shahzad Sarwar"  Height="150.5" HorizontalAlignment="Left" Margin="12,64,0,0" Name="button1" VerticalAlignment="Top" Width="363.5"  BorderBrush="Black"  BorderThickness="4" Background="White" FontSize="38"/>
    </Grid>

Output of appears as shown in image below:

[Zoom till 600% to view the real details]

Note that Firstly, the border of button [Bottom + Right] has a shade of light grey color. Secondly, 4 corner of border has some shade effect. Thirdly, characters of text have some shade effect.
First shade effect is not required, but second shade effect is required to show the corner shape and third shade effect is required to show the edges of characters.
These effects are called “antialiasing effect”

Before version 4.0, there was no concept of LayoutRounding. So effect was same as false.

Change UseLayoutRounding to True as:

<Grid x:Name="LayoutRoot" Background="White" UseLayoutRounding="True" Width="388">
        <Button Content="Shahzad Sarwar"  Height="150.5" HorizontalAlignment="Left" Margin="12,64,0,0" Name="button1" VerticalAlignment="Top" Width="363.5"  BorderBrush="Black"  BorderThickness="4" Background="White" FontSize="38"/>
    </Grid>

Output will be as:


[Zoom till 600% to view the real details]

Note that First antialiasing effect at the border of button is gone which was causing bad visual effect. Second and Third antialiasing effects are still there which are usefull to have a better visual effect.