30 March 2011

Silverlight - Isolated Storage

When using Silverlight Isolated Storage, you have two options:

Application or site

Application
Storage specific to the application and the user using it.
Site
Storage shared between all applications in a domain for the user.

Read from IS

using System.IO.IsolatedStorage;
using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite()){
  if (storage.FileExists(IS_FILE_NAME)) {
    using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream(IS_FILE_NAME, FileMode.Open, storage)){
      //Read the stream
    }
  }
}

Write to IS

using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite()){
  using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream(IS_FILE_NAME, FileMode.OpenOrCreate, storage)){
    stream.WriteLine("EGS rules!");
  }
}

28 March 2011

XAML - Value converter

In XAML, you can use value converters to convert a bound business data value into a GUI-usable value using a class like this one:

public class CurrencyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
        ((double)value).ToString("C");

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
        double.Parse((string)value);
}

A converter must be put in the resources like so:

<this:CurrencyConverter x:Key="CurrencyConverter" />

You can put it in App.xaml for project-wide usage.

To be then included in a binding like this.
You can also pass it a parameter if you like:

{Binding Amount, Converter={StaticResource CurrencyConverter}}
{Binding Amount, Converter={StaticResource CurrencyConverter}, ConverterParameter=EUR}

The parameter is passed as a string.
If you want to pass something else, you can use a markup extension.

25 March 2011

.NET | WPF - Localization

The following describes how I localized a WPF application:

I created a folder and put .NET .resx files in it. The default language was called Lan.resx; the localized one was called Lan.nl-BE.resx.

Then I added a class called Culture, containing a function called GetCulture() that returns a new Resources.Lan(), and a function called Set(CultureInfo culture) that sets the culture of the current thread by doing Thread.CurrentThread.Current(UI)Culture = culture.

In the App.xaml I added an application resource as follows:
<ObjectDataProvider x:Key="Lan" ObjectType="{x:Type this:Culture}" MethodName="GetCulture" />

To change the culture I can then do:
Culture.Set(culture);
((ObjectDataProvider)App.Current.FindResource("Lan")).Refresh();

Finally, controls are localized using statements like this:
{Binding Path=Options, Source={StaticResource Lan}}

23 March 2011

ASP.NET - Give trust to an assembly

One day SharePoint gave me the exception "Request for the permission of type 'System.Data.Odbc.OdbcPermission...". The sites trust level set in Web.config was:
<trust level="WSS_Minimal" originUrl="" />

Thus I had to edit the WSS_Minimal, the location of which is also indicated in the Web.config. In my case: 14\config\wss_minimaltrust.config.

In this file I added the following line to the SecurityClasses:
<SecurityClass Name="OdbcPermission" Description="System.Data.Odbc.OdbcPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

And this one under PermissionSet:
<IPermission class="OdbcPermission" version="1" Unrestricted="true" />

And I was back in business.

22 March 2011

SharePoint - Enable SessionState in SharePoint 2010

I had SharePoint 2010 giving me an exception telling me to enable SessionState, so here's how I did that:
  • Run the SharePoint 2010 Management Shell located in the Start menu as administrator,
  • In it, run the command "enable-SPSessionStateService",
  • Give this command some database name,
  • In the Web.config, set enableSessionState to true.

21 March 2011

SharePoint - Import a list from 2007 into 2010

I had myself a bit of a problem when trying to bring a SharePoint 2007 list with data to SharePoint 2010. I exported the lists as .iqy files and created new lists in 2010 using the Import Spreadsheet option. Of my 4 lists, 3 had the wrong column as their title field, and I couldn't get it right.

So I thought I'd try getting the list across via a list template .stp file. So done, my list template did show up in the Create window, but an error followed, stating:
Microsoft SharePoint Foundation version 3 templates are not supported in this version of the product

That's when I sought and found this solution:
  • Go to List Settings and choose Save list as template,
  • Rename the .stp file as a .cab file,
  • Extract the manifest.xml file in the .cab file,
  • In that file, set ProductVersion from 3 to 4,
  • Open a command prompt in C:\Windows\System32,
  • Run the command makecab ...\manifest.xml ...\ListName.cab,
  • Rename the .cab back to a .stp,
  • Import this template in SharePoint 2010, under Site Settings > List templates,
  • Create a list from the template, and all will be good!

08 March 2011

Internet Explorer - Open pop-ups as tabs

To get IE (7, 8, ...) to always open pop-ups in tabs, and not new windows, do as follows:
  1. Click Tools > Internet Options,
  2. On the tab General, under Tabs, click Settings,
  3. Under When a pop-up is encountered, select Always open pop-ups in a new tab, and click OK.
This was usefull for me when working with Microsoft CRM 2011 online.

04 March 2011

CRM - Silverlight web resource in host

To display a Silverlight control in a HTML host page in CRM (2011 online), you do this:
  1. Create a new web resource of type Silverlight and upload it,
  2. In the host page, change the source parameter to the Name you gave the Silverlight control in CRM; e.g. "syn_customers_per_company",
  3. Create a new web resource of type ''Web page'' and upload the host page.

02 March 2011

Silverlight - Preventing the build of localized dll's

When you build a Silverlight (4) project, the Bin folder get filled up with localized versions of the dll's referenced in the projects where these localized versions exist.
It turns out they are found here:
C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client
I suppose it's a nice feature in some cases, but it rather annoyed me, so I wanted to get rid of them. So I deleted all the localized sub folders in the SDK (de, fr, za-Hans, etc.) and yes!, at the next build localized dll's were no longer generated.

Silverlight - Dynamic service client and init params

Before, when I connected my Silverlight application to a web service, the web services URI was static. To make this dynamic I referenced System.ServiceModel, and implemented this class:
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Windows;
using EGS.KeyGen.QueryService;

namespace EGS.KeyGen {
  internal class DynamicServiceClient {
    internal static QueryServiceSoapClient GetClient() {
      BasicHttpSecurityMode securityMode = Application.Current.Host.Source.Scheme.Equals("https") ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None;
      BasicHttpBinding binding = new BasicHttpBinding(securityMode);
      binding.MaxReceivedMessageSize = int.MaxValue;
      binding.MaxBufferSize = int.MaxValue;
      return new QueryServiceSoapClient(binding, new EndpointAddress(new Uri(App.ServiceUrl)));
    }
  }
}

When a function wants to retrieve data, it no longer creates a SOAP client itself, but asks one from this class. The dynamic URI is a static property I defined in the App class that takes it's value from the init params on the Silverlight host page:
<param name="initParams" value="ServiceUrl=http://www.EGS.be/QueryService.asmx" />
and sets it like so:
private void Application_Startup(object sender, StartupEventArgs e) {
  if (e.InitParams.ContainsKey("ServiceUrl"))
    ServiceUrl = e.InitParams["ServiceUrl"];
...

And this way I can change the web service end point without having to rebuild the Silverlight application, even though the application still has a static end point from when the service reference was created.