学SL有一段时间了,可是总感觉缺少点什么,那就是实际应用,怎么能把SL应用到我的项目中呢?一直是一个难题,我是个懒人喜欢用Dataset来 帮我做一些底层数据访问的工作,SL毕竟是表示层的技术,对数据访问是门外汉,只能用过wcf或webservice来实现异步调用,今天把我的学习笔记 给大家看看,没有太高深的技术,只是共享而已,大家如果有别的方法欢迎指教!
新建一个Silverlight应用程序,再选择web应用程序
在webapplication中添加一个数据集(Dataset),我用的是“norTHWND.MDF”大家可以去ms网站去下载。
在查询里面添加一个SELECT CustomerID,Companyname,Contactname,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax FROM Customers WHERE (City liKE '%' + @City + '%')
新建一个“CustomersClass.cs” 实体类
public class CustomersClass
{
public string CustomerID { get; set; }
public string Companyname { get; set; }
public string Contactname { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
}
为了 *** 作方便,我只选择其中几个字段内容,大家有需要可以自己添加;
下面开始添加webservice,这个是重点:
public class SLWebService : System.Web.Services.WebService
{
//实例化Dataset
SLDataSettableAdapters.CustomerstableAdapter Dal =
new SilverlightCnblogs.Web.SLDataSettableAdapters.CustomerstableAdapter();
[WebMethod]
public CustomersClass[] GetCustomersByCity(string City)
{
List<CustomersClass> CustomersC = new List<CustomersClass>();
SLDataSet.CustomersDatatable DT = new SLDataSet.CustomersDatatable();
DT = Dal.GetDataByCity(City);
//foreach循环将dataset中数据取出
foreach (SLDataSet.CustomersRow DD in DT.Rows)
{
CustomersClass CC = new CustomersClass();
CC.Address = DD.Address.ToString();
CC.City = DD.City.ToString();
CC.Companyname = DD.Companyname.ToString();
CC.Contactname = DD.Contactname.ToString();
CC.ContactTitle = DD.ContactTitle.ToString();
CC.CustomerID = DD.CustomerID.ToString();
CustomersC.Add(CC);
}
System.Threading.Thread.Sleep(2000); //延时一下为了看效果
return CustomersC.ToArray();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
最后别忘了把动态端口改成固定的!!
下面我来Silverlight,添加“服务引用”“发现”,这些大家都很熟了,不详细说了!
在Page.xaml文件中添加
<GrID x:name="LayoutRoot" Background="White">
<GrID.ColumnDeFinitions>
<ColumnDeFinition WIDth="*"/>
</GrID.ColumnDeFinitions>
<StackPanel Height="auto" margin="0,13,0" VerticalAlignment="top" OrIEntation="Horizontal"
HorizontalAlignment="Right" WIDth="385">
<TextBlock Height="21" WIDth="27" Text="City:" textwrapPing="Wrap" d:LayoutOverrIDes="WIDth,Height"
HorizontalAlignment="Center" VerticalAlignment="Center" margin="0,20,0"/>
<TextBox x:name="CityText" Height="21" WIDth="86" Text="" textwrapPing="Wrap"
HorizontalAlignment="Stretch" VerticalAlignment="Center" margin="0,22,0"/>
<button x:name="Seach" Height="auto" HorizontalAlignment="Right" margin="0,14,0"
VerticalAlignment="Center" WIDth="95" Content=" Seach" Click="Seach_Click"/>
<Progressbar x:name="Pbar" Height="13" WIDth="108" IsIndeterminate="True" Visibility="Collapsed"/>
</StackPanel>
<data:DataGrID x:name="DataGrID" HorizontalAlignment="Stretch" autoGenerateColumns="False"
margin="8,49,8,20" WIDth="auto" IsReadonly="True">
<data:DataGrID.Columns >
<data:DataGrIDTextColumn header="CustomerID" WIDth="auto" Binding="{Binding CustomerID}">
</data:DataGrIDTextColumn>
<data:DataGrIDTextColumn header="Companyname" WIDth="auto" Binding="{Binding Companyname}">
</data:DataGrIDTextColumn>
<data:DataGrIDTextColumn header="Contactname" WIDth="auto" Binding="{Binding Contactname}">
</data:DataGrIDTextColumn>
<data:DataGrIDTextColumn header="ContactTitle" WIDth="auto" Binding="{Binding ContactTitle}">
</data:DataGrIDTextColumn>
<data:DataGrIDTextColumn header="Address" WIDth="auto" Binding="{Binding Address}">
</data:DataGrIDTextColumn>
<data:DataGrIDTextColumn header="City" WIDth="auto" Binding="{Binding City}">
</data:DataGrIDTextColumn>
</data:DataGrID.Columns>
</data:DataGrID>
</GrID>
编写C#代码:
private voID Seach_Click(object sender,RoutedEventArgs e)
{
this.Seach.IsEnabled = false;
this.Pbar.Visibility = Visibility.Visible;//显示进度条
this.DataGrID.Opacity = 0.5; //修改透明度
//异步调用
SC.GetCustomersByCityAsync(this.CityText.Text.ToString());
SC.GetCustomersByCityCompleted +=
new EventHandler<SilverlightCnblogs.ServiceReference1.GetCustomersByCityCompletedEventArgs>(SC_GetCustomersByCityCompleted);
}
voID SC_GetCustomersByCityCompleted(object sender,SilverlightCnblogs.ServiceReference1.GetCustomersByCityCompletedEventArgs e)
{ //异步调用完毕
this.DataGrID.ItemsSource = e.Result;
this.Seach.IsEnabled = true;
this.Pbar.Visibility = Visibility.Collapsed;
this.DataGrID.Opacity = 1;
}
现存问题:当用"DataGrID"是,不知道怎么控制
字体及对齐,请高手指教总结
源代码下载
以上是内存溢出为你收集整理的Silverlight边学边写之一“Silverlight+Webservice+Dataset”综合应用全部内容,希望文章能够帮你解决Silverlight边学边写之一“Silverlight+Webservice+Dataset”综合应用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)