Category Archives: Silverlight Tips and Tricks

‘System.Text.Encoding.GetString(byte[])’ is inaccessible due to its protection level

今天嘗試在Silverlight 上把Byte Array 轉成String的時候出現了以下的錯誤信息
Error 1 ‘System.Text.Encoding.GetString(byte[])’ is inaccessible due to its protection level
'System.Text.Encoding.GetString(byte[])' is inaccessible due to its protection level

CSharp

 byte[] StringAsByteArray = Convert.FromBase64String(source);
            string OriginalStringText = Encoding.UTF8.GetString(StringAsByteArray);
            return OriginalStringText;

經過一會兒的Research之後找到了解決的方法了

解決方法 十分簡單

我們需要用以下的Code去把Byte 讀出來
CSharp

string OriginalStringText = Encoding.UTF8.GetString(StringAsByteArray,0, StringAsByteArray.Length);

Hope you find it useful

How to Add border on Image in Silverlight – 在Silverlight的Image圖上加入邊界

最近開始有更多的機會接觸Silverlight
發現在設計時出現有小小的煩惱..
就是Image Control 放到在Canvas
由於Image沒有Border的在Expression BlendDesign Mode 中是看不到的

所以如果要在Expression BlendLayout有用Image Control的 地方時..
會有些困難..

解決方法:
由於Image沒有Border的關係..
所以我們可以自行加入一個Border Control用來包圍這個Image..
之後把ImageMargin設定成0..Image便可以用這個Border Control的會部面積了
E.G.

<Border BorderThickness=”1,1,1,1″ BorderBrush=”#FF000000″ >
<Image x:Name=”Img_1″ Visibility=”Visible” Height=”200″ Width=”200″ />
</Border>
或者可以使用Expression Blend來做這個動作
可以 Right Click 這個Image Control.之後選擇”Group Into” ->”Border”
之後設定Image 的Margin 為0 和設定Border 的colour便可

Hope you find it useful

.Net ” is not a valid JSON primitive. This error can also occur when extraneous data is present after the JSON data.

今日又再開始接觸 Silverlight Application
最近學會了用PHP Codeigniter RESTFul Framework
可以很簡單地建立一個RESTFul Services

但當我嘗試到deserialize json 的時候..
我出現了以下的問題
” is not a valid JSON primitive. This error can also occur when extraneous data is present after the JSON data.

最後發現原來自己的RESTFul Output 是一個 XML 的Output…
所以便出現了這個問題了

如果大家有相同的問題時
可以查看JSON 的 結果是不是 正確的..
可能JSON 你想接收到的不是一個正確的JSON =P

Hope you find it useful

The type or namespace name ‘DataContractJsonSerializer’ could not be found (are you missing a using directive or an assembly reference?)

When I try to upgrade my application, which I created from Silverlight2 to Silverlight4.
I have received the following error.

The type or namespace name ‘DataContractJsonSerializer’ could not be found (are you missing a using directive or an assembly reference?)
I have spent ages, try to figure out which assembly refernce I have missed in order to get it work.
E.g. I have checked that I have got the following reference added to the application.
System.Runtime.Serialization;
System.Runtime.Serialization.Json;

Finally, I remembered which reference I am missing.

Here it is the assembly reference you need to import, when you receive error message about
The type or namespace name ‘DataContractJsonSerializer’ could not be found (are you missing a using directive or an assembly reference?)
System.ServiceModel.Web

1) Right click on your “Project” and select “Add Reference…
2) On the “.Net” tab
3) Select “System.ServiceModel.Web” and Click “OK

This error should be fixed.

Hope you find it useful.

Silverlight 4 Release 15-04-2010

Silverlight 4 has released finally. When I went to Microsoft MIX 2010. They show us lots of cool features and stuff that you can do on Silverlight 4. Hope I can have some spare time to develop some cool stuff in Silverlight.

Just find a annoying thing on Silverlight 4 release.
All the application that I developed via Silverlight Beta/RC is not working anymore [I guess it probably happen to most of the developer, who has developed some application by using the beta/RC version of SilverLight 4]
When I load up my blog today. The following message pop-up.

I would need to recompile all the application that I created before by using the RC/Beta version. In order to get it works again.

Hope I can upgrade all my app this weekend. =)

Google Translator VS Microsoft Translator

Finally,I have create another Silverlight application, which allow me to use the Google Language API and Microsoft Translator API together. So that we can compare which translator do better job.

In my opinion, Google Translate is a lot more powerful than Microsoft Translator, as the Google Translate can translate a lot more language then Microsoft one. On the other hand, Google Translate has ability to detect the input language, which is more convenient.

If you try to translate “bill gate” [All in lowercase] but using the application below, you will realise the different between two translator.

I would be grateful, if you could give me some suggestion on how to make this mini application better.

Your browser does not support iframes.

Keep your brain running — Silverlight Mini Maths Game

Just created a mini maths game, which contains 20 simple maths question. See how long will it take you to answer them correctly. You can select the level of difficulty from the drop down list. You can also choose what kind of question you want to have, E.g. +/- only by ticking the checkbox.

Hope you would enjoy the game. I would be grateful, if you could give me some feedback/suggestion of this mini game. =)

Your browser does not support iframes.