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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值