返回首页
当前位置: 首页 > silverlight教程 >

Silverlight技巧贴

时间:2012-12-19 13:51来源:未知 作者:admin 点击:我要投稿  高质量的ASP.NET空间,完美支持1.0/2.0/3.5/4.0/MVC等

这篇文章是为了收集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;
本站推荐文章:
本站热点文章:
顶一下
(0)
0%
踩一下
(1)
100%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色 情、暴力、反动的言论。
评价:
表情:
用户名:密码: 验证码:点击我更换图片