: +91-265-2775555
 : +1-856-831-0505
 : +44-788-443-25-28
 : +45-4052-3137
Prakash software blogs - software development,custom sharepoint,MOSS 2007,Telerik development,C#.net,sharepoint webparts > Tag Clouds
Printing in silverlight

Problem: Printing in silverlight.

Answer: One of the important missing features in Silverlight has been printing support. If you have ever tried to print a web page containing Silverlight content, what you saw on the printed page may be skewed or even missing altogether!  

In one of the projects we are working, we need to print information from silverlight application for a user. We have resolved this problem using HTML Bridge.

First of all, Place one div element in your .aspx or .html page which holds your silverlight object as shown below. Look at printDiv tag. It will hold printing content of silverlight application.

“<div id="silverlightControlHost">

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"

            width="100%" height="100%">

            <param name="source" value="ClientBin/XXXXX.xap" />

            <param name="onError" value="onSilverlightError" />

            <param name="background" value="white" />

            <param name="minRuntimeVersion" value="3.0.40624.0" />

            <param name="autoUpgrade" value="true" />

            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none">

                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"

                    style="border-style: none" />

            </a>

        </object>

        <iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;

            border: 0px"></iframe>

    </div>

    <div id="printDiv" style="display: none; height: 0; width: 0; z-index: -1; position: absolute">

    </div>

Add this JavaScript function as below in same aspx or html page. These will pop up a print dialog to print dynamically generated content in printDiv element.

//        Function to print itinarary layout

        function CallPrint() {

            var prtContent = document.getElementById("printDiv");

            var WinPrint = window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');

            WinPrint.document.write(prtContent.innerHTML);

            WinPrint.document.close();

            WinPrint.focus();

            WinPrint.print();

            WinPrint.close();

            prtContent.innerHTML = "";

        }

 

Now question is how we can add elements and set values from silverlight in this div tag. Here Is the solution. For HtmlElement and HtmlPage object please add statement

 using System.Windows.Browser;

//get printDiv element of html or aspx page as HTMLElement

HtmlElement printDiv = HtmlPage.Document.GetElementById("printDiv");

 

//setting printDiv's innerHTML property to empty string

printDiv.SetProperty("innerHTML", string.Empty);

 

//creating table and tbody instance

HtmlElement table = HtmlPage.Document.CreateElement("table");

HtmlElement tbody = HtmlPage.Document.CreateElement("tbody");

 

//setting table width and Vertical align properties

table.SetStyleAttribute("width", "70%");

table.SetStyleAttribute("verticalAlign", "top");

 

 

//creating TripName(Header of page) label in top

HtmlElement trTrip = HtmlPage.Document.CreateElement("tr");

 

HtmlElement tdTrip = HtmlPage.Document.CreateElement("td");

tdTrip.SetStyleAttribute("width", "20%");

tdTrip.SetStyleAttribute("verticalAlign", "top");

 

// add input element and set it’s properties and value

HtmlElement tripinput = HtmlPage.Document.CreateElement("input");

tripinput.SetAttribute("value", _dashboard.lblControlName.Text);

tripinput.SetStyleAttribute("width", "100%");

tripinput.SetAttribute("type", "text");

tripinput.SetAttribute("readonly", "readonly");

tripinput.SetStyleAttribute("border", "0");

tdTrip.AppendChild(tripinput);

trTrip.AppendChild(tdTrip);

tbody.AppendChild(trTrip);

 

//adding tbody tablebody to table

table.AppendChild(tbody);

//adding table to printDiv Div

printDiv.AppendChild(table);

 

//call to javascript Function which will fire print command to print Printdiv contents

HtmlPage.Window.CreateInstance("CallPrint", null);

 

That’s it.

 

Thanks,

Posted By: Darshana Rana & Jigar patel

Pass class object to silverlight from web page

I am working with one application named Database Report. Here is the brief description about it.

 

The database report system is design to facilitate web based ad-hoc reporting.  End users can use a Silverlight 2 interface to select tables, table columns, related tables, related table fields, and run and save reports based on those selections. End user can open existing reports and add charts and many other things.

 

During development, In silverlight object, I need to pass context object from asp.net web page which contains information related to user such as username, access rights for different features etc.

 

Between these I was looking for solution that how to pass object to silverlight instead of strings. But I did not get any solution over internet.

 

Then, I come up with new idea. I think I should share that with others. So, I am writing down this blog.

 

 

First,

 

I serialize context object:

 

            ContextObject mContext = new ContextObject();

            string contextstreamstrEnc = string.Empty;

 

System.Security.Principal.IPrincipal ip = HttpContext.Current.User;

            //set windows identity user name to context object property Name

            if (ip.Identity.IsAuthenticated)

            {

                mContext.Name = ip.Identity.Name;

            }

           

            //set of features for particualr user name // add it over here for example only

            mContext.Features = new List< Fetaure>();

 

            mContext.Features.Add(Fetaure.SearchReport);

            mContext.Features.Add(Fetaure.ViewReport);

            mContext.Features.Add(Fetaure.CreateReport);

            mContext.Features.Add(Fetaure.EditReport);

            mContext.Features.Add(Fetaure.ScheduleReport);

            mContext.Features.Add(Fetaure.GenerateReport);

            mContext.Features.Add(Fetaure.ExportReport);

 

            if (mContext != null)

            {

                //Serialize context object using BinaryFormatter

                using (MemoryStream ms = new MemoryStream())

                {

                    BinaryFormatter bf = new BinaryFormatter();

                    bf.Serialize(ms, mContext);

                    //Encrypt string with using encryption key to send it via InItParams

                    contextstreamstrEnc = EncryptString(Convert.ToBase64String(ms.ToArray()));

                }

                

            }

 

Finally, set InItParams property of Silverlight Object in code behind file…

 

            databaseReportHost1.InitParameters = "MemoryStream=" + contextstreamstrEnc;

 

Alternative to this is Querystring.

You can pass this in querystring as we do in normal web page.

           

 

Now, how to retrieve MemoryStream in silverlight object

 

1. QueryString:

 

In silverlight, you can retrieve QueryString as:

 

IDictionary<string, string> queryStrings = System.Windows.Browser.HtmlPage.Document.QueryString;

 

Here in Dictionary collection it stores parameters with values.

 

Now we can read value like

string memoryStream = queryStrings["MemoryStream"];

 

That’s it.

 

 

2. InItParams

 

 

You can retrieve stream of context object in Silverlight’s App.xaml.cs (In App’s Startup Event):

 

this.Startup += (s, a) =>

                {

                  string memoryStream = string.Empty;

 

                  if (e.InitParams.Count > 0)

                  {

                        memoryStream = e.InitParams["MemoryStream"];

                  }

 

                  Page page = new Page(memoryStream);

 

                  // Load the main control

                  this.RootVisual = page;

                }

 

 

Now, First decrypt context stream and then deserialize it and you have your object.

I have done this in Web serice method to get rights and username from context stream.

 

That’s it…

 

 

another thing which I want to share with you :

 

How to set and get values from text box or other controls placed in html page?

 

Set:

 

HtmlDocument doc = HtmlPage.Document;

doc.GetElementById("txtName").SetProperty("value", "Jigar");

 

Get:

HtmlDocument doc = HtmlPage.Document;

string userName = doc.GetElementById("txtName").GetProperty("value").ToString();

What is Silverlight?

Silverlight 2 includes a cross-platform, cross-browser version of the .NET Framework, and enables a rich .NET development platform that runs in the browser.  Developers can write Silverlight applications using any .NET language (including VB, C#, JavaScript, IronPython and IronRuby).  Visual Studio 2008 and Expression Studio tool support that enables great developer / designer workflow and integration when building Silverlight applications.

Silverlight 2 provides a rich set of features for RIA application development. 

These include:

  • WPF UI Framework: Silverlight 2 includes a rich WPF-based UI framework that makes building rich Web applications much easier.  In includes a powerful graphics and animation engine, as well as rich support for higher-level UI capabilities like controls, layout management, data-binding, styles, and template skinning.  The WPF UI Framework in Silverlight is a compatible subset of the WPF UI Framework features in the full .NET Framework, and enables developers to re-use skills, controls, code and content to build both rich cross browser web applications, as well as rich desktop Windows applications.
  • Rich Controls: Silverlight 2 includes a rich set of built-in controls that developers and designers can use to quickly build applications.  Silverlight 2.0 includes core form controls (TextBox, CheckBox, RadioButton, etc), built-in layout management panels (StackPanel, Grid, Panel, etc), common functionality controls (Slider, ScrollViewer, Calendar, DatePicker, etc), and data manipulation controls (DataGrid, ListBox, etc).  The built-in controls support a rich control templating model, which enables developers and designers to collaborate together to build highly polished solutions.
  • Rich Networking Support: Silverlight 2 includes rich networking support.  It includes out of the box support for calling REST, WS*/SOAP, POX, RSS, and standard HTTP services.  It supports cross domain network access (enabling Silverlight clients to directly access resources and data from resources on the web).  Silverlight 2 also includes built-in sockets networking support.
  • Rich Base Class Library: Silverlight 2 includes a rich .NET base class library of functionality (collections, IO, generics, threading, globalization, XML, local storage, etc).  It includes rich APIs that enable HTML DOM/JavaScript integration with .NET code.  It also includes LINQ and LINQ to XML library support (enabling easy transformation and querying of data), as well as local data caching and storage support.  The .NET APIs in Silverlight are a compatible subset of the full .NET Framework.

Silverlight 2 does not require the .NET Framework to be installed on a computer in order to run.  The Silverlight setup download includes everything necessary to enable all the above features on a vanilla Mac OSX or Windows machine. 

Once Silverlight 2 tool is installed you can browse the Web and automatically run rich Silverlight applications within your browser of choice (IE, Firefox, Safari, etc).