介绍
当你在客户端层中编写代码时,你对大多数应用程序具有编程访问权限。 使用Silverlight自定义控件时也适用这里情况。 这很重要,因为它节省了编写大量代码的能力。 一个专业开发人员在创建doctype应用程序时比编码Silverlight应用程序时要高得多。
你可以选择使用一个空白的shell 命令运行一个LightSwitch应用程序。 这样做时,你仍然可以访问本文中描述的所有特性。
本文将对 LightSwitch API进行随机遍历,让你了解从 LightSwitch Silverlight定制控件中可以编程访问的概念。
这里页面描述了 LightSwitch数据模型: 这里有很多类,图是非常广泛的。 但是,它描述了主要部分之间的关系。
注释: 必须使用本文介绍的方法创建 Visual Studio 控件,以创建Silverlight控件。
LightSwitch资源管理器应用程序
我们将通过创建示例LightSwitch应用程序来对LightSwitch进行随机遍历。 我们将创建一个名为"lightswitch资源管理器"的Silverlight自定义控件,它将显示显示的屏幕集合。引发方法和 switch 屏幕。
我们创建了一个简单的LightSwitch应用程序,它允许管理产品和订单。
产品的屏幕。
单屏幕订购。
Silverlight控件
向解决方案中添加新项目。
我们添加一个Silverlight类库。
我们选择 Silverlight 4.
项目将在解决方案资源管理器中显示,我们将右击右键单击 Class1.cs 文件并删除它。
我们向SilverlightControlLibrary项目添加一个新项目。
我们添加一个Silverlight用户控件,并将它的称为 LightSwitchExplorer.xaml。
右键单击References引用,然后选择Add引用"。
我们将引用添加到:
- Microsoft.LightSwitch
- Microsoft.LightSwitch.Base.Client
- Microsoft.LightSwitch.Client
这将允许对 LightSwitchExplorer.xaml 控件中文件后面代码中的LightSwitch的访问。
我们打开 LightSwitchExplorer.xaml.cs 文件,并添加以下 using
语句:
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Client;
using Microsoft.LightSwitch.Details;
using Microsoft.LightSwitch.Details.Client;
using Microsoft.LightSwitch.Model;
using Microsoft.LightSwitch.Presentation;
using System.Windows.Data;
using System.Collections;
我们打开 LightSwitchExplorer.xaml 文件,并将标记更改为以下内容:
这将添加一个 Grid
和三个 ListBox
es。
<UserControlx:Class="SilverlightControlLibrary.LightSwitchExplorer"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"d:DesignHeight="300"d:DesignWidth="400"><Gridx:Name="LayoutRoot"Background="White"><Grid.ColumnDefinitions><ColumnDefinitionWidth="122*"/><ColumnDefinitionWidth="122*"/><ColumnDefinitionWidth="122*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinitionHeight="20*"/><RowDefinitionHeight="100*"/><RowDefinitionHeight="177*"/></Grid.RowDefinitions><ListBoxHorizontalAlignment="Stretch"VerticalAlignment="Stretch"Margin="5,5,5,5"Name="lstMethods"Grid.Row="1"Grid.Column="1"/><ListBoxHorizontalAlignment="Stretch"VerticalAlignment="Stretch"Margin="5,5,5,5"Name="lstActiveScreens"Grid.Row="1"/><ListBoxHorizontalAlignment="Stretch"VerticalAlignment="Stretch"Margin="4,5,5,5"Name="lstApplicationDetailsProperties"Grid.Column="2"Grid.Row="1"/></Grid></UserControl>
我们在页面上拖放一个 DataGrid
。
这将自动添加需要的程序集以支持 DataGrid
。
我们点击并拖动网格的角度来调整它的大小,以便它填充整个页面的底部。
在 DataGrid
的属性中,我们选中 AutoGenerateColumns
旁边的框。
我们将 Label
拖放到第一个框之上,并将它的Content
更改为"活动屏幕"。
我们再增加两个 Label
,然后把它们和 Collections
。
向Silverlight控件添加代码
打开 LightSwitchExplorer.xaml.cs 文件,并添加以下代码:
// This property is created because it allows us to raise //"OnExplorerControlDataContextPropertyChanged"// When the DataContext is updatedpublicstaticreadonly DependencyProperty ExplorerControlDataContextProperty =
DependencyProperty.Register("DummyProperty", typeof(IContentItem),
typeof(LightSwitchExplorer),
new PropertyMetadata(OnExplorerControlDataContextPropertyChanged));
接下来,在 InitializeComponent();
行后面添加此行:
this.SetBinding(ExplorerControlDataContextProperty, new Binding());
这将设置 ExplorerControlDataContextProperty
的绑定。
添加以下方法:
#region ShowMethodsOnScreenprivatestaticvoid ShowMethodsOnScreen(LightSwitchExplorer EC,
IContentItem contentItem)
{
// Fill the lstMethods ListBox with// a list of all Methods on the current Screenforeach (var item in contentItem.Screen.Details.Commands.All())
{
EC.lstMethods.Items.Add(item.Name);
}
}#endregion#region ShowActiveScreensprivatestaticvoid ShowActiveScreens(LightSwitchExplorer EC,
IContentItem contentItem)
{
// Fill the lstActiveScreens ListBox with a list of all Active Screensforeach (var item in contentItem.Screen.Details.Application.ActiveScreens)
{
EC.lstActiveScreens.Items.Add(item.Screen.Name);
}
}#endregion#region ShowScreenCollectionsprivatestaticvoid ShowScreenCollections(LightSwitchExplorer EC,
IContentItem contentItem)
{
// Fill the lstApplicationDetailsProperties ListBox with a list of all // Collections on the current Screenforeach (var item in contentItem.Screen.Details.Properties.All())
{
EC.lstApplicationDetailsProperties.Items.Add(
String.Format("{0}", item.Name));
}
}#endregion
最后,添加以下方法,当设置或者更改 DataContext
时,将引发前面的方法:
privatestaticvoid OnExplorerControlDataContextPropertyChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
LightSwitchExplorer EC = (LightSwitchExplorer)d;
IContentItem contentItem = (IContentItem)EC.DataContext;
ShowActiveScreens(EC, contentItem);
ShowMethodsOnScreen(EC, contentItem);
ShowScreenCollections(EC, contentItem);
}
将Silverlight控件插入到应用程序中
解决方案。
打开EditableProductsGrid屏幕,并添加一个新的自定义控件。
单击添加引用按钮。
创建SilverlightControlLibrary项目的项目引用。
我们现在可以选择我们创建的定制控件。
在控件的属性中,将标签位置设置为 none。
运行应用程序时,我们看到控件显示活动屏幕。当前屏幕上的方法以及当前屏幕上的集合。
使资源管理器控件处于交互式状态
返回到Silverlight控件,双击活动屏幕列表框上的双击。
方法将被连接。
将以下代码用于该方法:
privatevoid lstActiveScreens_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the LightSwitch DataContext IContentItem contentItem = (IContentItem)this.DataContext;
// The selected screenstring strScreenName = (sender as ListBox).SelectedValue.ToString();
// Get the selected Screenvar SelectedScreen = (from ActiveScreens in
contentItem.Screen.Details.Application.ActiveScreens.AsQueryable()
where ActiveScreens.Screen.Name == strScreenName
select ActiveScreens).FirstOrDefault();
if (SelectedScreen!= null)
{
// Activate the selected Screen SelectedScreen.Activate();
}
}
双击方法列表框上的。
将以下代码用于该方法:
privatevoid lstMethods_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the LightSwitch DataContext IContentItem contentItem = (IContentItem)this.DataContext;
// The selected Methodstring strMethodName = (sender as ListBox).SelectedValue.ToString();
var Method = (from Commands in contentItem.Screen.Details.Commands.All()
where Commands.Name == strMethodName
select Commands).FirstOrDefault();
if (Method!= null)
{
// Get a reference to the LightSwitch Screenvar Screen =
(Microsoft.LightSwitch.Client.IScreenObject)contentItem.Screen;
Screen.Details.Dispatcher.BeginInvoke(() => {
Method.Execute();
});
}
}
双击收藏框上的双击。
将以下代码用于该方法:
privatevoid lstApplicationDetailsProperties_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
// Get the LightSwitch DataContext IContentItem contentItem = (IContentItem)this.DataContext;
// The Property Namestring strPropertyName = (sender as ListBox).SelectedValue.ToString();
var Property = (from Properties in contentItem.Screen.Details.Properties.All()
where Properties.Name == strPropertyName
select Properties).FirstOrDefault();
dataGrid1.ItemsSource = (IEnumerable)Property.Value;
}
资源管理器控件
将资源管理器控件添加到应用程序的其他页。
- 你可以点击屏幕上的switch 屏幕
- 你可以单击执行它们的方法
- 你可以点击集合查看它们的内容( 并编辑它们)
这只是在表面上
这个例子几乎没有涉及到LightSwitch开发者所能提供的全部。 有关用户。完整数据库模式和数据以及整个应用程序的信息。
通常需要数百行代码才能在Silverlight应用程序中手动创建的操作在使用LightSwitch时可以实现1 倍。
特别鸣谢
如果微软的 Sheel特别感谢,否则这篇文章将不可能被 possible。
进一步阅读
更多LightSwitch内容
你可以在 http://lightswitchhelpwebsite.com/ 上找到大量关于 advanced Visual Studio的文章。