No path is too difficult. No destination is too far !
Posts tagged SilverLight
Graceful Degrading to HTML or ASP.Net page
Feb 4th
Have you ever wonder how Microsoft checks if the Silverlight plugin is not unstalled on your your system then the default ASP.Net website should appear otherwise new Silverlight website must be presented ?
here is the trick called Graceful Degrading
In case no Silverlight plugin has been installed on client’s machine, just present asp.net or HTML version of your page in the Silverlight Tag of your ASP.Net host page.
<asp:Silverlight ID=”xamlApp” runat=”server” Source=”~/ClientBin/SLApp.xap”
MinimumVersion=”2.0.31005.0″>
<PluginNotInstalledTemplate>
<div id=”someHTML” >
<!– Some HTML website code or ASP.Net webpage code –>
</div>
</PluginNotInstalledTemplate>
</asp:Silverlight>
How to convert from IEnumerable to EntitySet to get Entity back in action
Nov 27th
One way of achieving is to use what most google search results find on web i.e.
public static EntitySet<T> ToEntitySet<T> (this IEnumerable<T> source) where T : class
{
var es = new EntitySet<T> ();
es.AddRange (source);
return es;
}
Then you could write your query as follows:
var questions = from q in e.Element(“Questions”).Elements(“Question”)
select new Question
{
Text = q.Attribute(“title”).Value,
Point = (decimal) q.Attribute(“point”),
Choices = (
from c in q.Elements(“Choices”).Elements(“Choice”)
select new Choice
{
Text = c.Attribute(“title”).Value,
Correct = (bool) c.Attribute(“isCorrect”)
}
).ToEntitySet();
};
But to achieve this, one may need to add reference to System.Data.Linq and even then there are sometimes when you don’t find the solution, in cases where only one Entity is required.
A very simple and effective trick for a work around of this problem is to use the following :
var FirstQuestion = Questions.Where(q=>q.ID==someIDParameter).ToList().FirstorDefault();
Now use this and do the work peacefully.
How to solve LoadFromRemoteSources error in VS 2010
Oct 20th
I was facing an issue during the migration of my silverlight application to Visual Studio 2010. The issue was something like there are some references into the application from the network shared folder. When compiling the application there was a weird error saying :
The “ValidateXaml” task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly ‘file://\\xxxxxxxx\Assemblies\xxxxx.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0×80131515)
File name: ‘file://\\xxxxxxxx\Assemblies\xxxxx.dll’ —> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)
Navigating the microsoft link for this problem, it is bit confusing that we have to add the LoadFromRemoteSources switch to some app.config file or web.config file !! So here is the process to accomplish the task and get rid of thsi ridiculous error:-
- Goto this path in your windows explorer :
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE - Open the devenv.exe.config file in notepad or any editor of your choice.
- Search for this element closing tag in the file:
</runtime> - Before this element closing tag, Copy and paste the below configuration switch
<loadFromRemoteSources enabled=”true“/> - Save and close the config file
- It’s done !!
How to pass InitParameters to Silverlight in HTML object control
Mar 3rd
There are three different ways in which we can pass parameters to Silverlight from the HTML side:
1. Pass parameters using the HTML object control.
This may be the case using pure HTML page to call silverlight control in it or may be the simple ASp.Net page as usual.
<object width=”100%” height=”100%”>
<param name=”source” value=”ClientBin/SomethingFunny.xap” />
<!– you can pass InitParameters to it. Pass anything like EndPointURI, EndPointName or Even StartPage –>
<param name=”InitParameters” value=”startPage=UnRegisteredLandingPage, EndPointURI=http://www.sehajpal.com/testservice.svc” />
</object>
2. Using ASP.Net page to call silverlight control in it, we can easily pass parameters to it in anyway. One of the old tricks is to have an asp-literal control on the HTML side i.e. MarkUp side, and set its text to the desired HTML output fom code behind.
In the markup:
<asp:Literal ID=”litParam” runat=”server” />In the code behind:
this.litParam.Text = String.Format(“<param name=\”ParamInitParameters\” value=\”{0}\” />”, sInitParms);
3. Using asp-silverlight control to host Silverlight application in an ASP.net website as usual. This is pretty neat approach in terms of playing around with parameters and properties of control hosting the Silverlight app.
In the markup:
<asp:Silverlight ID=”SLControl” runat=”server” Width=”100%” Height=”100%” />
In the code behind:
SLControl.InitParameters = “EndPointURI=” + endPointURI + “,EndPointName=commonservice, UserCredentials=” + CredentialString;
Hope it works for you in different scenarios.
Bind with Enum in Silverlight
Feb 23rd
Silverlight doesn’t have any kind of ObjEctDataProvider with it. So the famous example of binding the controls with an ObjectDataProvider doesn’t work with silverlight as it works with WPF
<UserControl.Resources>
<ObjectDataProvider MethodName=“GetValues” ObjectType=”{x:Type sys:Enum}“ x:Key=“AlignmentValues”> <ObjectDataProvider.MethodParameters>
<x:Type TypeName=“HorizontalAlignment” />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
This can be done via code behind in the Silverlight. Just add a reference to the Reflection assembly.
using System.Reflection;
Use this simple function to get IEnumerable collection out of Enum type.
public IEnumerable<Enum> GetEnumValues(Enum enumeration)
{
return from gField in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)
select (Enum)gField.GetValue(enumeration);
}
Just bind the results of this function with your control and its done !
ComboFrom.ItemsSource = GetEnumValues(new SehajService.Currency());
Unable to debug the silverlight project
Feb 18th
I have some strange thing happening in my application. I was able to debug anything but silverlight code !!
I did so much of RnD on net and tried various tricks, but only this thing worked : -
When you add a Silverlight project to a asp.net solution, you create 2 projects…
A. the startup project (interface starter)
B. the interface itself
1. right click on the startup project (SilverlightWeb — the one with default.aspx in it )…
2. click on ‘Property Pages’
3. open ‘Start Options’ Tab
4. enable Silverlight Debugger
5. OK
This was the solution for me
Versioning problem in reference.cs in WCF service consumption in Silverlight 2
Feb 18th
A starnge problem occured when I was trying to add reference to one of my WCF service in my silverlight project. When deeply researched I found that some of the data contracts have been references twice in the reference.cs i.e. some classes with version 3.0.0.0 and some with 2.0.0.0 version. Strange!!
A quick work around was to generate the reference via svcutil.exe and it worked well in case where the project was justa relay service and not the silverlight application. In silverlght app, it throws the error for async and completed methods. Then I used SLSvcutil.exe i.e. svcutil for silverlight which is bundled with silverlight 3 only. I used this and generated the refences.cs for me. One problem left is FAULTCONTRACTS in the code. That i removed manually and it is working now.