Silverlight技巧贴
这篇文章是为了收集Silverlight中的一些技巧,会不定时更新。
1.读取外部XML,通过WebClient异步下载。注意,XML文件要放在SL的WEB项目中。
/// <summary> /// 获取URL地址 /// </summary> /// <returns></returns> public static string GetURL() { Uri uri = Application.Current.Host.Source; string url = uri.AbsoluteUri.Replace(uri.AbsolutePath, string.Empty); return url; } //下载XML文件 public void GetMsgXML() { Uri uri = new Uri(GetURL() + "/DataSource/MessageSource.xml", UriKind.Absolute); WebClient Appclient = new WebClient();//使用WebClient下载config.xml文件,进行异步读取。 Appclient.OpenReadAsync(uri); Appclient.OpenReadCompleted += new OpenReadCompletedEventHandler(Appclient_OpenReadCompleted); } void Appclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { Stream stream = e.Result; XElement ele = XElement.Load(e.Result); }
通过IsolatedStorageFile独立存储
创建并保存:
XDocument doc = new XDocument( new XComment("This is a comment"), new XElement("Root", new XElement("Child1", "data1"), new XElement("Child2", "data2") ) ); using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("myFile.xml", FileMode.Create, isoStore)) { doc.Save(isoStream); } }
读取:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("myFile.xml", FileMode.Open, isoStore)) { XDocument doc1 = XDocument.Load(isoStream); OutputTextBlock.Text = doc1.ToString(); } }
2 后台设置Foreground
txtGrid.Background = new SolidColorBrush(Color.FromArgb(255, 0, 150,255)); txtGrid.Foreground = new SolidColorBrush(Colors.White);
3 后台设置Binding
TextBlock txtContent = new TextBlock(); txtContent.Name = "txtContent"; thickNess = new Thickness(3, 3, 3, 3); txtContent.Padding = thickNess; txtContent.TextWrapping = TextWrapping.Wrap; //Binding DataContext //相当于<TextBlock x:Name="txtContent" DataContext="{Binding MessageTip}"/> Binding contextBing = new Binding(); contextBing.Source = WindowMsg; contextBing.Mode = BindingMode.OneWay; txtContent.SetBinding(TextBlock.DataContextProperty, contextBing); //Binding Text //相当于<TextBlock Text="{Binding ElementName=txtContent, Path=DataContext.Message}"/> Binding textBind = new Binding(); textBind.Path = new PropertyPath("DataContext.Message"); textBind.ElementName = "txtContent"; txtContent.SetBinding(TextBlock.TextProperty, textBind);
4 后台设计控件的样式Style
先将样式定义在App.xaml中
<Application.Resources> <Style x:Key="MsgTextBlock" TargetType="TextBlock"> <Setter Property="Height" Value="auto"/> <Setter Property="Width" Value="auto"/> <Setter Property="FontSize" Value="15"/> <Setter Property="Foreground" Value="Blue"/> <Setter Property="LineHeight" Value="3"/> </Style> </Application.Resources>
在后台调用:
txtContent.Style = App.Current.Resources["MsgTextBlock"] as Style;
或:
txtContent.Style = Application.Current.Resources["MsgTextBlock"] as Style;
5 用Path设计箭头样式
效果如图:
XAML代码:
<Grid HorizontalAlignment="Center" Height="16" VerticalAlignment="Center" Width="10" Background="#00FFFFFF"> <Path Data="M8.3122921,0 C8.8241329,0 9.3359737,0.19526052 9.7264957,0.58578253 C10.50754,1.3668256 10.50754,2.6331477 9.7264957,3.4141917 L4.8205633,8.3201237 L9.7264977,13.226058 C10.507541,14.007101 10.50754,15.273424 9.7264957,16.054468 C8.9454527,16.835512 7.6791306,16.835512 6.898087,16.054468 L0.58578575,9.7421665 C0.19526458,9.3516455 3.8146973E-06,8.8398037 4.0531158E-06,8.3279629 L0.00018811226,8.3202133 L0,8.3122921 C0,7.8004503 0.195261,7.2886086 0.58578253,6.898087 L6.8980865,0.585783 C7.2886086,0.195261 7.8004498,0 8.3122921,0 z" Stretch="Fill" UseLayoutRounding="False" RenderTransformOrigin="0.5,0.5"> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF0096FF" Offset="1" /> <GradientStop Color="#FF19A0EA" Offset="0.228" /> <GradientStop Color="#FF188AC9" /> </LinearGradientBrush> </Path.Fill> </Path> </Grid>
上面生成的是左边的箭头样式,如果要生成右边的样式,将刚刚的反转180度即可:
<Grid HorizontalAlignment="Center" Height="16" VerticalAlignment="Center" Width="10" Background="#00FFFFFF"> <Path Data="M8.3122921,0 C8.8241329,0 9.3359737,0.19526052 9.7264957,0.58578253 C10.50754,1.3668256 10.50754,2.6331477 9.7264957,3.4141917 L4.8205633,8.3201237 L9.7264977,13.226058 C10.507541,14.007101 10.50754,15.273424 9.7264957,16.054468 C8.9454527,16.835512 7.6791306,16.835512 6.898087,16.054468 L0.58578575,9.7421665 C0.19526458,9.3516455 3.8146973E-06,8.8398037 4.0531158E-06,8.3279629 L0.00018811226,8.3202133 L0,8.3122921 C0,7.8004503 0.195261,7.2886086 0.58578253,6.898087 L6.8980865,0.585783 C7.2886086,0.195261 7.8004498,0 8.3122921,0 z" Stretch="Fill" UseLayoutRounding="False" RenderTransformOrigin="0.5,0.5"> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF0096FF" Offset="1" /> <GradientStop Color="#FF19A0EA" Offset="0.228" /> <GradientStop Color="#FF188AC9" /> </LinearGradientBrush> </Path.Fill> <Path.RenderTransform> <CompositeTransform Rotation="180"/> </Path.RenderTransform> </Path> </Grid>
6 字符串格式设置:StringFormat
StringFormat=c2, 美元,小数点保留两位 $22,123.89
StringFormat=n2, 小数点保留两位 22,123.89
StringFormat=yyyy/MM/dd\,HH:mm:ss 2013/11/15 20:23:20
时间格式:
7 Grid设置背景图片
设置背景图片很简单,使用ImageBrush即可。如下:
<Grid.Background> <ImageBrush ImageSource="bg.png" Stretch="Fill"></ImageBrush> </Grid.Background>
注意:Silverlight目前只支持png和jpg格式的图片。
8 后台代码设置属性值
double left =400; double top =20; double radius =20; rectangle1.SetValue(Canvas.LeftProperty, left); rectangle1.SetValue(Canvas.TopProperty, top); rectangle1.SetValue(Rectangle.RadiusXProperty, radius); rectangle1.SetValue(Rectangle.RadiusYProperty, radius);
如果我的文章对你有帮助,就点一下推荐吧.(*^__^*)