Tuesday, November 16, 2010

a downloaded sample Silverlight app gave me "ValidateXaml" task failed error

If you download a sample silverlight app and try to build it, and get below error

"Error The "ValidateXaml" task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly 'file:///C:\MyTestingProjects\Silverlight4Upload\SilverlightFileUploadSrc\FileUpload\Bin\Debug\XX.XXXX.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
...
"

No need to try to de-bug it. This is simply because Windows protects you from downloaded files by default

Fixing this is pretty quick only if you know how/where to look. Below are the steps:

1.Right-click the downloaded zip file and choose “Properties”
2.At the bottom of the “General” tab, there is a “Security” section, saying “This file came from another computer and might be blocked to help protect this computer.” Click the “Unblock” button.
4.Unzip and open the solution.
5.Build, and the error message will go away!

Something good to note down for myself.

Friday, November 5, 2010

writing extentions

1) add an extention to a string to check if this string is a valid email address
public static class StringExtensions
{
public static bool IsValidEmailAddress(this string s)
{
Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}

it takes one static class and a method to pass in the original object as "this". Done.

To use the above extention
Using StringExtensions;
string email="kelly.qu@123.com"
if (email.IsValidEmailAddress()) //this method would also show in the intellisense
...

2) more advanced extention - check and see if this object is in a collection?
using System;
using System.Collections;
public static class ListExtensions
{
public static bool in(this Object o, IEnumerable c)
{
return c.Any(x=>x.equals(o));
}
}

To test above extention:
using System;
Using ListExtensions;
public class test
{
public void testExtension()
{
string[] vals = {"baltimore", "toronto", "shanghai"};
if ("Portland".in(vals)) // check if Portland is in the list?
}
}

converting to LINQ

1) Change a for loop to LINQ:
before:
for (int i = 0; i < questions.answers.Count; i++)
{
if (questions.answers[i] == 0 && string.IsNullOrEmpty(questions.answers.Text))
{
questionAnswered = false;
break;
}
}

after:
if (questions.answer.Any(t => t == 0 && string.IsNullOrEmpty(questions.answers.Text)))
{
questionAnswered = false;
}


2) Convert a list of type T to a list of type U:
List<String> strList="....";
List<Object> objList= strList.Cast<object>().ToList();


3) get back a list of people with first name started with Q; last name contains "Cute", and the average and the minimum ages
public class person
{
public string FirstName;
public string LastName;
public int Age;
}

List<person> people = new List<person>{
new person {FirstName="DD", LastName="Cute", Age=35}
new person {FirstName="QQ", LastName="NotCute", Age=40}
new person {FirstName="Qing", LastName="Cool", Age=38}
}
"

IEnumerable<person> peopleQ=people.Where(p=>p.FirstName.StartWith("Q"));

IEnumerable<person> peopleCute=people.Where(p=>p.LastName.Contains("Cute"));

int MinAge=people.Min(p=>p.Age());

int AverageAge=people.Average(p=>p.Average());