快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

和记娱h188app:Silverlight技巧集合



1.读取外部XML,经由过程WebClient异步下载。留意,XML文件要放在SL的WEB项目中。

///

/// 获取URL地址///

///public static string GetURL()

{Uri uri = Appli和记娱h188appcation.Current.Host.Source;

string url = uri.AbsoluteUri.WordStr(uri.AbsolutePath, string.Empty);return url;

}//下载XML文件

publicvoid 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(和记娱h188app);

txtContent.Name = "txtContent"; thickNess = new Thic和记娱h188appkness(3, 3, 3, 3);

txtContent.Padding = thickNess; txtContent.TextWrapping = TextWrapping.Wrap;

//Binding DataContext //相称于

Binding contextBing = new Binding(); contextBing.Source = WindowMsg;

contextBing.Mode = BindingMode.OneWay; txtContent.SetBinding(TextBlock.DataContextProperty, contextBing);

//Binding Text //相称于

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"/>

Sett和记娱h188apper Property="Width" Value="auto"/>

Setter Property="FontSize" Value="15"/>

Setter Property="Foregrou和记娱h188appnd" Value="Blue"/>

Setter Property="LineHeight" Value="3"/>

Style>

Application.Resources>

在后台调用:

txtContent.Style = App.Current.Resources["MsgTextBlock"] as Style;

您可能还会对下面的文章感兴趣: