Silverlight 4: New features overview (Part 3) – Elevated Out-of-browser applications

November 18, 2009

Silverlight 4 enhances the out-of-browser feature introduced in previous version of the technology. Silverlight 3 added a feature to use application in Out-of-browser mode – install application shortcut to desktop or start menu, and launch it within application launcher (instead of the browser). Along with it, the application still was sandboxed application, with the same set of restrictions, like in browser.

Silverlight 4 extends this feature and allows to relax some of the sandbox restrictions while running application with elevated permissions.

First, let’s see what will be allowed under elevated permissions…

The runtime will allow:

  • Cross domain network access with policy file check
  • Getting full file path from Open/Save file dialog
  • Access to users folders, like MyDocuments
  • COM Interop
  • HTML hosting support

Now, let’s see how this elevated permissions could be set.

In order to request elevated permissions while installing the out-of-browser application, the ApplicationManifest.xaml should has following section:

  <OutOfBrowserSettings.SecuritySettings>
    <SecuritySettings ElevatedPermissions="Required" />
  </OutOfBrowserSettings.SecuritySettings>

This section could be set by direct editing of OutOfBrowserSettings.xml in project or by selecting a relevat checkbox in updated  “Out-of-Browser Settings” window in project properties.

image

At the runtime, while installing the application the user will get following prompt:

image

instead of non-elevated one:

image

If user will allow to install the application, at the runtime the presence (or absence) of elevated permissions could be checked:

if (App.Current.IsRunningOutOfBrowser && App.Current.HasElevatedPermissions)
{
  //...
}

To demonstrate some of functionalities of elevated OOB application I’ve created sample project. It is pretty simple:

image

COM Interop

Now let’s see how to use COM interop from Silverlight. In order to show the usage it, I’ll show how to export some data to Excel, will create the chart, and copy this chart to Word document. Also, I’ll use new keyword in C# 4.0 – dynamic.

In my sample application I have sample class “SalesPerson”, which holds data for salesmen, and “SalesReport” class, which has a bunch of “SalesPersons”:

public class SalesPerson
{
  public string Name { get; set; }
  public double GrossSales { get; set; }

  public SalesPerson(string name, double grossSales)
  {
    Name = name;
    GrossSales = grossSales;
  }
}

public class SalesReport : ObservableCollection<SalesPerson>
{ }

Also I have some code to initialize the collection of salespersons… Now let’s see how to export this data to excel – I’ll do it at “COM Interop” button event handler.

First we need to check, that application is running Out-of-Browser, has elevated permissions and COM automation is operational on the current platform or at a certain point in the life of the application:

private void btnInterop_Click(object sender, RoutedEventArgs e)
{
  if (App.Current.IsRunningOutOfBrowser && App.Current.HasElevatedPermissions)
  {
    if (ComAutomationFactory.IsAvailable)
    {
      //...
    }
  }
}

Now the COM interop stuff: I’ll use new feature in C# 4 – the dynamic keyword. Little bit about dynamic keyword (from the documentation):

The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).

Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.

I’ll use the dynamic to create the instance or Excel and Word applications and operate them. Inside the function body:

dynamic excel = ComAutomationFactory.CreateObject("Excel.Application");

excel.Visible = true;
excel.Workbooks.Add();
excel.Cells[1, 1] = "Salesperson Name";
excel.Cells[1, 2] = "Gross Sales";

//Initialize dummy data
SalesReport sales = new SalesReport();
InitializeSalesData(sales);

//convert/prepare dummy data for excel
string[,] processes = InitializeExcelData(sales);

int i = 2;
for (int j = 0; j < processes.GetLength(0); j++)
{
     excel.Cells[i, 1] = processes[j, 0];
     excel.Cells[i, 2] = processes[j, 1];
     i++;
}
dynamic range = excel.Cells[1, 1];
dynamic chart = excel.ActiveWorkbook.Charts.Add(After: excel.ActiveSheet); //Optional parameter
chart.ChartStyle = 45; //Bar chart - in therop this represented by enum
chart.CopyPicture(1, 2, 1); //parameters here are enum values - see documentation for CopyPicture parameters

I’ve started a new instance of Excel, created a new workbook, added my data to the cells of this workbook. At the end of this part I’ve created a new bar chart and copied it to clipboard. In the chart object creation I’ve used another new feature of C# 4 – optional parameter. See the “Add” function call.

Now I will create the instance of Word application with a new document and will paste prepared chart to this document

dynamic word = ComAutomationFactory.CreateObject("Word.Application");
word.Documents.Add();
word.Selection.Paste();
word.Visible = true;

At the end of this operation we have Excel and Word opened with data and bar chart. From here we could save/print the workbook/document using same automation mechanisms to automate office applications.

 

HTML Hosting

Additional functionality available for elevated OOB application is HTML hosting. This functionality being exposed by WebBrowser user control (new in Silverlight 4). This control allow to navigate to HTML string or any URI. In non-OOB application or non-elevated OOB the control has following UI:

image

But in elevated OOB application it able to render any HTML page (including applications which requires additional plug-ins to work, like Silverlight or Flash):

image

Pure HTML application

image

HTML with Silverlight application.

In my case I’ll use “HTML Hosting” button event handler to navigate. As input I’m using TextBox named “txtURL” (the one with blue background on the screenshots):

browser.Navigate(new Uri(txtURL.Text));

In addition to WebBrowser control, Silverlight 4 provides HTMLBrush – the new brush class, which enables painting any UI element with the content rendered by WebBrowser control. I’ll use it to paint the rectangle:

image

HtmlBrush brush = new HtmlBrush();
brush.SetSource(browser);
brush.Stretch = Stretch.Uniform;
rectToFill.Fill = brush;

Applied as a brush to the rectangle:

image

 

Sample code (along with an additional sample code from next part) is here.

 

Stay tuned for next part – Notification Windows

Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

4 comments

  1. jordygouwyJune 16, 2010 ב 20:26

    i have a silverlight 3 project, i converted it to silverlight 4 to use OOB, but it won’t work. Any idea why it doesn’t (i’ve set the options like you showed)

    Reply
  2. Alex GoleshJune 17, 2010 ב 14:58

    jordygouwy:

    What exactly is not working? It is not elevated? It is not OOB? Did you installed it?

    Alex

    Reply
  3. Best of Baton RougeJune 21, 2010 ב 16:03

    Great article. Helped a lot with some struggling I’ve had going on with saving files. Would you know when the savefiledialog path will be able to be set? Here we are in 4 with a problem that’s been complained about since 2 and we can’t do this basic task.

    Reply
  4. gponkyogeavAugust 23, 2011 ב 16:34

    , , , ,

    Reply