如何解決Silverlight對中文字型的問題

如何解決Silverlight對中文字型的問題

大家在開發silverlight的過程中,應該會遇到中文字型的問題吧...
最近剛好聽了一場silverlight的課,有介紹到如何解決,就分享給大家吧...

先介紹最簡單且實用的方法:
假設有一個TextBlock裡面輸入中文字型→"中文字"
xaml code 如下:

1 <TextBlock Width="188" Height="62" Canvas.Left="182" Canvas.Top="116" TextWrapping="Wrap" ursor="None"> <Run Text="中文字"/> </TextBlock>

 

結果會變成一些亂碼的文字

利用Expression Blend2的[Convert To Path]來將TextBlock轉換成Path
Object→Path→Convert to Path


xaml code變成如下:

01 <Path RenderTransformOrigin="0,0" Cursor="None" Width="41.938" Height="13.979" Fill="#FF000000" Stretch="Fill" Canvas.Left="167.49" Canvas.Top="248.682" Data="M7.5625,8.2916679 L7.5625,12.875001 L12.260417,12.875001 L12.260417,8.2916679 z M2.2916667,8.2916679 L2.2916667,12.875001 L6.875,12.875001 L6.875,8.2916679 z M6.7747397,4.682292 C7.3476563,4.7586813 7.8203125,4.8684902 8.192708,5.0117197 C8.3168402,5.0690107 8.3789063,5.140626 8.3789063,5.2265635 C8.3789063,5.3220491 8.3168402,5.3888893 8.192708,5.427084 C8.0017357,5.5130215 7.7916665,5.5655384 7.5625,5.5846357 L7.5625,7.947917 L12.117188,7.947917 L12.589844,7.3033857 C12.962239,7.5134554 13.229601,7.723525 13.391927,7.9335947 C13.468316,8.0004349 13.506511,8.062501 13.506511,8.1197929 C13.506511,8.2152786 13.320313,8.3728304 12.947917,8.5924492 L12.947917,11.557293 C12.947917,12.273438 12.962239,12.74132 12.990886,12.960938 C13.009983,13.237848 13.019531,13.457466 13.019531,13.619792 C13.019531,13.77257 12.967014,13.891928 12.861979,13.977865 C12.709202,14.035157 12.58507,14.063803 12.489583,14.063803 C12.336805,14.063803 12.260417,14.001737 12.260417,13.877604 L12.260417,13.218751 L7.5625,13.218751 L7.5625,17.315105 L7.591146,17.759115 C7.6006947,17.921442 7.6054688,18.059896 7.6054688,18.17448 C7.6054688,18.499132 7.4479165,18.661459 7.1328125,18.661459 C6.8845487,18.661459 6.7604165,18.575521 6.7604165,18.403646 C6.7604165,18.203125 6.7795138,17.945313 6.8177085,17.630209 C6.8559027,17.305555 6.875,17.019098 6.875,16.770834 L6.875,13.218751 L2.2916667,13.218751 L2.2916667,13.877604 C2.2916667,14.001737 2.2105036,14.063803 2.048177,14.063803 C1.9431424,14.063803 1.8094618,14.035157 1.6471355,13.977865 C1.5421007,13.891928 1.4895834,13.77257 1.4895834,13.619792 C1.4895835,13.457465 1.5039064,13.237847 1.532552,12.960938 C1.5802951,12.74132 1.6041666,12.273438 1.6041665,11.557293 L1.6041665,8.3203125 C1.6041666,7.9288197 1.5659722,7.5898447 1.4895834,7.3033857 L2.4348958,7.947917 L6.875,7.947917 L6.875,5.8424482 C6.875,5.3268232 6.8415799,4.9401045 6.7747397,4.682292 z M21.55599,5.026042 C22.128906,5.1024313 22.601563,5.2122402 22.973959,5.3554697 C23.098091,5.4127607 23.160156,5.484376 23.160156,5.5703135 C23.160156,5.6657991 23.098091,5.7326393 22.973959,5.770834 C22.782986,5.8567715 22.572918,5.9092884 22.34375,5.9283857 L22.34375,7.718751 L26.869793,7.718751 L27.571615,6.859376 C27.963108,7.145834 28.33073,7.4275179 28.67448,7.7044277 C28.760418,7.7617197 28.803387,7.8237858 28.803387,7.890626 C28.803387,8.005209 28.722221,8.062501 28.559896,8.062501 L25.552719,8.062501 C25.416155,9.0743198 25.18597,9.9853649 24.878906,10.798178 C24.621094,11.667101 24.186632,12.445313 23.575521,13.132813 L22.555876,14.27289 C23.013311,14.773304 23.600271,15.206259 24.30599,15.582032 C25.003038,16.030817 25.733507,16.374567 26.497396,16.613281 C27.203995,16.880644 28.015625,17.124132 28.932293,17.34375 L28.903646,17.630209 C28.311632,17.630209 27.939236,17.82118 27.786459,18.203125 C26.907986,17.868925 26.058159,17.47743 25.23698,17.028646 C24.40625,16.570313 23.704428,16.083334 23.131512,15.567709 L22.205404,14.664755 C22.093603,14.870529 21.927795,15.021193 21.756512,15.166667 C21.069012,15.758681 20.214409,16.302952 19.192709,16.79948 C18.132813,17.334202 16.853298,17.82118 15.354167,18.260418 L15.125,17.959637 C16.796007,17.386719 18.108942,16.823351 19.063803,16.269531 C20.037762,15.715713 20.849392,15.152345 21.498699,14.579428 L21.793707,14.263349 C21.633358,14.170471 21.521517,14.033514 21.412762,13.891928 C20.925781,13.290365 20.510418,12.617188 20.166668,11.872396 C19.775173,11.194445 19.469618,10.349393 19.25,9.3372402 C19.154514,9.6905384 18.977865,10.082031 18.720053,10.511719 C18.490887,10.865018 18.180555,11.289931 17.789063,11.786459 C17.769966,12.111112 17.717449,12.273438 17.631512,12.273438 C17.554996,12.273314 17.311527,12.029824 16.901043,11.542969 C17.464411,11.151476 17.898872,10.755209 18.204428,10.354168 C18.567274,9.9913197 18.825087,9.5377617 18.977865,8.9934902 C19.01606,8.8311634 19.101997,8.75 19.235678,8.75 C19.426649,8.75 19.531685,8.8311634 19.550781,8.9934902 C19.569878,9.5282125 19.708334,10.067709 19.966146,10.61198 C20.185764,11.165799 20.534288,11.776911 21.011719,12.445313 L22.178978,13.850556 C22.537401,13.550344 22.833342,13.208813 23.102865,12.860678 C23.551651,12.23047 23.93837,11.495226 24.263021,10.654949 C24.480183,9.9582214 24.667332,9.094305 24.819931,8.062501 L16.628906,8.062501 C16.046442,8.0815983 15.650174,8.1102438 15.440104,8.1484375 L15.182292,7.5755215 C15.545139,7.6710072 16.027344,7.718751 16.628906,7.718751 L21.65625,7.718751 L21.65625,6.1861982 C21.65625,5.6705732 21.622829,5.2838545 21.55599,5.026042 z M39.86068,8.5638027 C40.213974,8.8598099 40.509983,9.1510429 40.748699,9.4375 C40.79644,9.4947929 40.820313,9.552084 40.820313,9.609375 C40.82021,9.6667252 40.734272,9.7144337 40.5625,9.7526054 L40.01823,9.78125 C39.464409,10.144098 38.896267,10.463976 38.313805,10.740887 L37.131172,11.282464 C37.231899,11.301585 37.317226,11.343244 37.397137,11.385418 C37.492622,11.442709 37.540367,11.509549 37.540367,11.585938 C37.540367,11.662327 37.478298,11.714844 37.354168,11.74349 C37.191841,11.800781 37.039063,11.858074 36.895836,11.915365 L36.895836,13.104167 L41.421875,13.104167 L42.152344,12.216146 C42.562935,12.512154 42.940105,12.798612 43.283855,13.075521 C43.379341,13.123264 43.427086,13.18533 43.427086,13.26172 C43.427086,13.385851 43.345921,13.447917 43.183594,13.447917 L36.895836,13.447917 L36.895836,17.157553 C36.895836,17.482206 36.814671,17.730469 36.652344,17.902344 C36.509113,18.064672 36.308594,18.188803 36.050781,18.27474 C35.831165,18.379776 35.635418,18.432293 35.463543,18.432293 C35.463543,18.222223 35.401478,18.064671 35.277344,17.959637 C35.143658,17.845051 34.914494,17.744791 34.589844,17.658855 C34.217445,17.563368 33.802086,17.501303 33.34375,17.472656 L33.34375,17.100262 C34.078995,17.24349 34.737846,17.315105 35.320313,17.315105 L36.022137,17.229168 C36.146267,17.171875 36.208336,17.033421 36.208336,16.813803 L36.208336,13.447917 L31.238281,13.447917 C30.655817,13.467014 30.25955,13.495661 30.04948,13.533854 L29.791668,12.960938 C30.154514,13.056424 30.636719,13.104167 31.238281,13.104167 L36.208336,13.104167 L36.208336,12.459636 C36.208336,11.934463 36.160591,11.404514 36.065105,10.869793 L36.874496,11.183106 C37.324173,10.921105 37.716938,10.671653 38.055992,10.440105 L39.259117,9.4375 L33.386719,9.4375 C32.727867,9.4375 32.331596,9.4661465 32.197918,9.5234375 L31.96875,8.9934902 C32.302952,9.0603304 32.775608,9.09375 33.386719,9.09375 L39.273438,9.09375 z M36.108074,4.911459 C36.680992,4.9878483 37.153648,5.0976572 37.526043,5.2408857 C37.650173,5.2981777 37.712242,5.369792 37.712242,5.4557295 C37.712242,5.5512161 37.650173,5.6180563 37.526043,5.656251 C37.335068,5.7421885 37.125,5.7947054 36.895836,5.8138027 L36.895836,6.802084 L41.851563,6.802084 L42.324219,6.2578135 C42.677517,6.5251741 43.002171,6.8116322 43.29818,7.1171885 C43.336372,7.145834 43.355469,7.1792545 43.355469,7.2174482 C43.355469,7.2842884 43.250435,7.3415804 43.040367,7.3893232 C42.887589,7.4179697 42.730034,7.4513893 42.567711,7.489584 C42.386284,7.6996551 42.176216,7.9192719 41.9375,8.1484375 C41.679688,8.3871536 41.421875,8.6067715 41.164063,8.8072929 L40.90625,8.6067715 C41.173611,8.348959 41.378906,8.0959206 41.522137,7.8476572 L41.880211,7.145834 L31.399862,7.145834 C31.40744,7.317049 31.384527,7.4835505 31.352865,7.6471357 C31.314671,7.9240456 31.25738,8.1627617 31.18099,8.3632813 C31.123699,8.5160599 31.042536,8.6497402 30.9375,8.7643242 C30.861113,8.8789063 30.779949,8.9505215 30.694012,8.9791679 C30.617622,9.0173616 30.546007,9.036459 30.479168,9.036459 C30.211805,9.036459 30.078125,8.9457474 30.078125,8.7643242 C30.078127,8.6783838 30.13542,8.57335 30.25,8.4492188 C30.498264,8.1532125 30.679688,7.8954 30.794271,7.6757822 C30.899305,7.4752607 30.990019,7.2508688 31.066406,7.0026045 C31.133247,6.7543411 31.171442,6.4201393 31.18099,6.000001 L31.395834,6.0429697 C31.42448,6.2816849 31.438803,6.5108509 31.438803,6.7304697 L31.432089,6.802084 L36.208336,6.802084 L36.208336,6.0716152 C36.208336,5.5559902 36.174915,5.1692715 36.108074,4.911459 z">
02   <Path.RenderTransform>
03     <TransformGroup>
04       <ScaleTransform ScaleX="1" ScaleY="1"/>
05       <SkewTransform AngleX="0" AngleY="0"/>
06       <RotateTransform Angle="0"/>
07       <TranslateTransform X="0" Y="0"/>
08     </TransformGroup>
09   </Path.RenderTransform>
10 </Path>



最後結果如下:


還有其它更好的解決方案如下:

作者:奚江華
http://www.microsoft.com/taiwan/msdn/columns/hsi_chiang_hua/Silverlight_cht_solutions.htm

作者:黃忠成
http://www.microsoft.com/taiwan/msdn/columns/huang_jhong_cheng/Silverlight_cht_solutions.htm