Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 12 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
12
Dung lượng
1,43 MB
Nội dung
Data Binding. Isolated Sto rage. XmlReader LINQ to XML . Ngoà Web service, WCF và ADO.Net Data Service. Data Binding Isolated Storage Mã XAML: <TextBox x:Name="MyTextBox" Text="Text" Foreground="{Binding Brush1, Mode=OneWay}"/> Mã C# : INotifyPropertyChanged. MyColors textcolor = new MyColors(); // Brush1 textcolor.Brush1 = new SolidColorBrush(Colors.Red); // MyTextBox.DataContext = textcolor; OneTime: OneWay: TwoWay: 1. Change Notification namespace DataBinding { //Tao mot class thua ke interface INotifyPropertyChanged public class MyColors : INotifyPropertyChanged { private SolidColorBrush _Brush1; // Khai bao su kien PropertyChanged. public event PropertyChangedEventHandler PropertyChanged; //Tao thuoc tinh cua SolidColorBrush public SolidColorBrush Brush1 { get { return _Brush1; } set { _Brush1 = value; // Goi NotifyPropertyChanged khi thuoc tinh nguon duoc cap nhap NotifyPropertyChanged("Brush1"); } } public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } 1. e using System.Windows.Controls; using System.IO.IsolatedStorage; using System.IO; using System; namespace Samples_Chap7 { public partial class Page : UserControl { public Page() { InitializeComponent(); //Luu du lieu vao file dulieu.txt SaveData("Day la du lieu cua toi", "dulieu.txt"); //Lay du lieu tu file dulieu.txt string test = LoadData("dulieu.txt"); } private void SaveData(string data, string fileName) { //Lay Isolated Storage cua nguoi dung danh cho ung dung using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Cr eate, isf)) { using (StreamWriter sw = new StreamWriter(isfs)) { //Luu du lieu sw.Write(data); sw.Close(); } } } } private string LoadData(string fileName) { string data = String.Empty; //Lay Isolated Storage cua nguoi dung danh cho ung dung using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Op en, isf)) { using (StreamReader sr = new StreamReader(isfs)) { string lineOfData = String.Empty; while ((lineOfData = sr.ReadLine()) != null) //Doc du lieu data += lineOfData; } } } return data; } } } x M y. Trong 2. <UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Canvas x:Name="LayoutRoot" Background="White"> </Canvas> </UserControl> Trong using System.Windows.Markup; using System.Xml.Resolvers; using System.Xml; using System.Xml.Linq; using System.IO; using System.Text; i // Tao XElement de luu thong tin lien he ban be XElement contacts = new XElement("Contacts", new XElement("Contact1", new XElement("Ten", "Pham Chi Cuong"), new XElement("DienThoai", "0906 123 480"), new XElement("DiaChi", new XElement("DuongPho", "Nguyen Hong"), new XElement("ThanhPho", "HaNoi") ) ), new XElement("Contact2", new XElement("Ten", "Tran Duy Bien"), new XElement("DienThoai", "0904 252 161"), new XElement("DiaChi", new XElement("DuongPho", "Pham Van Dong"), new XElement("ThanhPho", "HaNoi") ) ) ); // Tao TextBlock1 // Luu y rang Element nay phai khai bao 2 XAML namespace XElement textBlock1 = XElement.Parse( @"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' TextWrapping= 'Wrap' Width = '400' Canvas.Top = '10' Text=''/>"); // Lay child Element cua contact1 XElement contact1 = contacts.Element("Contact1"); // Gan gia tri vao thuoc tinh Text o cuoi cung voi noi dung cua cac contact trong xml textBlock1.LastAttribute.SetValue(contact1.ToString()); // Lay child element thu hai XElement contact2 = contacts.Element("Contact2"); // Tao TextBlock2 // Luu y rang Element nay phai khai bao 2 XAML namespace XNamespace xmlns = "http://schemas.microsoft.com/client/2007"; XElement textBlock2 = new XElement(xmlns + "TextBlock", new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"), new XAttribute("Canvas.Top", 250), new XAttribute("Width", "600"), new XAttribute("Text", contact2.ToString()) ); // Them TextBlock1 vao trong trang LayoutRoot.Children.Add(XamlReader.Load(textBlock1.ToString()) as UIElement); // Them TextBlock2 vao trong trang LayoutRoot.Children.Add(XamlReader.Load(textBlock2.ToString()) as UIElement); WebClient wc = new WebClient(); wc.OpenReadCompleted += wc_OpenReadCompleted; wc.OpenReadAsync(new Uri(uriString)); wc_OpenReadCompleted private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { //Kiem tra thuoc tinh Error cho cac loi if (e.Error != null) { OutputTextBlock.Text = e.Error.Message; return; } //Neu khong co loi, Lay du lieu stream ve va phan tich chung toi XDocument thong qua phuong thuc Load using (Stream s = e.Result) { XDocument doc = XDocument.Load(s); OutputTextBlock.Text = doc.ToString(SaveOptions.OmitDuplicateNamespaces); } } WCF 1. y là SQLData ScottGu's tutorial Thêm LINQ to SQL Classes này có tên là DataClasses1.dbml public interface IService1 { [OperationContract] void DoWork(); } public interface IService1 { [OperationContract] List<Customer> GetCustomersByLastName(string lastName); } public List<Customer> GetCustomersByLastName(string lastName) { DataClasses1DataContext db = new DataClasses1DataContext(); var matchingCustomers = from cust in db.Customers where cust.LastName.StartsWith(lastName) select cust; return matchingCustomers.ToList(); } <services> <service behaviorConfiguration="SQLData_Web.Service1Behavior" name="SQLData_Web.Service1"> <endpoint address="" binding="wsHttpBinding" contract="SQLData_Web.IService1"> <identity> <dns value="localhost"/> </identity> </endpoint> </service> </services> sau <endpoint address="" binding="basicHttpBinding" contract="SQLData_Web.IService1"> [...]... Margin="2,0,0,4" VerticalAlignment="Bo ttom"/> Grid . } } x M y. Trong 2. <UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/client/20 07& quot; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml". System.Windows.Controls; using System.IO.IsolatedStorage; using System.IO; using System; namespace Samples_Chap7 { public partial class Page : UserControl { public Page() { InitializeComponent(); //Luu. textBlock1 = XElement.Parse( @"<TextBlock xmlns='http://schemas.microsoft.com/client/20 07& apos; xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' TextWrapping= 'Wrap'