Sunday 30 October 2011

Supporting Multilanguage in Silverlight Application

To build up a multilanguage Silverlight Application follow the step by step tutorial and you will be working in minutes :)
Start MSVS2010 > New Project > Silverlight Business Application.


Step #1 (Defining supported languages in the SL project)
=============================================
Right click Silverlight Client Application > Unload Project.
Right click Silverlight Client Application > Edit (SilverlightBusinessApplication.csProj).
Find tag named: <SupportedCultures></SupportedCultures>
Add languages that your application will support like
<SupportedCultures>ar-LY,en-US</SupportedCultures>
Save and close file.
Right click Silverlight Client Application > Reload Project.


Step #2 (Adding the resource file for different languages)
==============================================
In Silverlight Client Applciation > (Assets) folder > (Resources) folder
copy (ApplicationStrings.resx) and paste it in the same folder.
Rename the new file following the language convention (example: (ApplicationStrings.ar-LY.resx) for Arabic-Libyan).
Delete any cs file created for ApplicationStrings.ar-LY.resx as we don't need it.
Open the file (ApplicationStrings.ar-LY.resx) by double clicking it and edit the (value) field of each application string.
Add new field (Name: AppFlowDirection)
    Value: (LeftToRight) in ApplicationStrings.resx
    Value: (RightToLeft) in ApplicationStrings.ar-LY.resx

Save file and close.


Step #3 (Solving a bug in MSVS2010)
=============================
In Silverlight Client Applciation > (Assets) folder > (Resources) folder
Open file ApplicationStrings.Designer.cs which is the cs file of the ApplicationStrings.resx file and change the ApplicationStrings constructor  modifier
from inner to Public.
Save file and close.


Step #4 (Handling the change language event)
======================================
In Silverlight Client Applciation > (Assets) folder > (Resources) folder
Open file ApplicationResources.cs and
Implement the INotifyPropertyChanged interface for the ApplicationResources class.
Update the public properties so they look like:
        public ApplicationStrings Strings
        {
            get { return applicationStrings; }
            set { OnPropertyChanged("Strings"); }
        }
        public ErrorResources Errors
        {
            get { return errorResources; }
            set { OnPropertyChanged("Errors"); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (propertyName!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
Save file and close.


Step #5 (Adding UI to change the language)
====================================
In Silverlight Client Applciation Open MainPage.xaml and insert a comboBox after the about link; paste the following:
            <ComboBox Name="Language" SelectionChanged="Language_SelectionChanged" Width="80">
                <ComboBoxItem Content="English" Tag="en-US" IsSelected="True" />
                <ComboBoxItem Content="عربي" Tag="ar-LY" />
            </ComboBox>
Navigate to the new event Language_SelectionChanged and paste the following:
            Thread.CurrentThread.CurrentCulture = new CultureInfo(((ComboBoxItem) ((ComboBox)sender).SelectedItem).Tag.ToString());
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(((ComboBoxItem) ((ComboBox)sender).SelectedItem).Tag.ToString());
            ((ApplicationResources)App.Current.Resources["ApplicationResources"]).Strings = new ApplicationStrings();
Save file and close.


Step #6 (Supporting Bidirectional)
=========================
In the MainPage.xaml add the following to the main UserControl tag:
FlowDirection="{Binding Path=Strings.AppFlowDirection, Source={StaticResource ApplicationResources}}"
Save file and close.


Step #7 (Test the application)
======================
Change the languages from the comboBox and the app will respond.

Step by step building a Silverlight Business Application

I will present a stright forward step by step tutorial to build a complete Silverlight Business Application and how to merge the Membership provider database with our application database.

  1. Create your SQL Server db (example: dbExample).
  2. Run aspnet_regsql.exe found in: (C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regsql.exe)
  3. Choose your SQL Server instance and your database (dbExample) and go.
  4. Check new tables add to your database (dbExample) by asnet_regsql.
  5. Run MSVS2010 > New Project > Silverlight Business Application.
  6. Add to Web Applicataiono ADO.net EDM.
  7. Connect to your database (dbExample).
  8. Choose your tables.
  9. Open file Web.Config in Web Application
  10. Copy ConnectionString for your database (dbExample) for the EDM and paste it in the ConnectionString named ApplicationService.
  11. Choose Web Application and Run ASP.NET Confiugration
  12. Test connection
  13. Add Roles (example: Admin).
  14. Add User (example: viewUser).
  15. Add User (example: AdminUser) member of Admin Role.
  16. Add DomainService to Web Application, choose your tables, name it (exampleDomainService).
  17. In your DomainService (exampleDomainService) before each IQuerable query add the following:
  18.  nothing    query can be accessed by any user.
  19.  [RequiresAuthentication]  query can be accessed by logged in users only.
  20.  [RequiresRole("Admin")]  query can be accessed by logged in users and member of Role: (Admin).
  21. In Client Application in (Views) folder add (Silverlight Page), name it examplePage.xaml.

Thursday 27 October 2011

Publishing Silverlight + WCF RIA Services Application on the web

My experience in publishing a silverlight WCF RIA services application in ISQSolutions hosting plan showed the following:

  1. Create new folder under the root folder www (example SLApp).
  2. Copy the Domain Service assemblies (3 files) to application bin folder after publish:
    • System.ServiceModel.DomainServices.EntityFramework.dll
    • System.ServiceModel.DomainServices.Hosting.dll
    • System.ServiceModel.DomainServices.Server.dll
  3. Upload application to created folder in the hosting plan (SLApp).
  4. From the hosting control panel, create application for the created folder (SLApp).
  5. Run you application.