65.9K
CodeProject 正在变化。 阅读更多。
Home

随机漫步 LightSwitch 数据模型

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (20投票s)

2011年9月10日

Ms-PL

4分钟阅读

viewsIcon

43566

downloadIcon

583

我们将创建一个名为“LightSwitch Explorer”的Silverlight自定义控件,该控件将显示其所在屏幕的集合信息,触发方法并切换屏幕。

image

引言

当你在 **LightSwitch** 客户端层编写代码时,你可以对应用程序的大部分内容进行编程访问。在使用Silverlight自定义控件时也是如此。这一点很重要,因为它能为你节省大量代码。专业开发人员在创建LightSwitch应用程序时,其生产力要远高于在不使用LightSwitch的情况下编写Silverlight应用程序。

你可以选择 使用空白 Shell 运行LightSwitch应用程序。这样做时,你仍然可以访问本文档中描述的所有功能。

在本文中,我们将对LightSwitch API(应用程序编程接口)进行一次漫游,让你了解从LightSwitch Silverlight自定义控件(这也适用于 LightSwitch 控件扩展)可用的编程访问方式。

image

此页面 描述了 **LightSwitch 数据模型**。其中涵盖了许多类,图表也提供了一个非常宽泛的概述。但是,它确实描述了主要部分之间的关系。

注意:要使用本文所述方法创建Silverlight控件,你必须拥有Visual Studio Professional(或更高版本)。

LightSwitch Explorer 应用程序

我们将通过创建一个示例LightSwitch应用程序来漫游LightSwitch API。我们将创建一个名为“LightSwitch Explorer”的Silverlight自定义控件,该控件将显示其所在屏幕的集合信息,触发方法并切换屏幕。

image

我们创建了一个简单的LightSwitch应用程序,允许管理 `Products` 和 `Orders`。

image

一个用于 `Products` 的屏幕。

image

一个用于 `Orders` 的屏幕。

Silverlight 控件

image

向解决方案添加新项目。

image

我们添加一个Silverlight类库。

image

我们选择Silverlight 4。

image

项目将显示在解决方案资源管理器中,我们右键单击 `Class1.cs` 文件并将其删除。

image

我们向 `SilverlightControlLibrary` 项目添加一个新项。

image

我们添加一个Silverlight用户控件,并将其命名为 *LightSwitchExplorer.xaml*。

image

我们右键单击“引用”,然后选择“**添加引用**”。

image

我们添加对以下程序集的引用:

  • Microsoft.LightSwitch
  • Microsoft.LightSwitch.Base.Client
  • Microsoft.LightSwitch.Client

这将允许在 *LightSwitchExplorer.xaml* 控件的代码隐藏文件中访问LightSwitch API。

image

我们打开 *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;

image

我们打开 *LightSwitchExplorer.xaml* 文件,并将标记更改为以下内容:

这将添加一个 `Grid` 和三个 `ListBox`。

<UserControl x: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">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="122*" />
            <ColumnDefinition Width="122*" />
            <ColumnDefinition Width="122*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20*" />
            <RowDefinition Height="100*" />
            <RowDefinition Height="177*" />
        </Grid.RowDefinitions>
        <ListBox HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" Margin="5,5,5,5"                   
           Name="lstMethods" Grid.Row="1" 
           Grid.Column="1"  />
        <ListBox HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" Margin="5,5,5,5"
           Name="lstActiveScreens" Grid.Row="1"  />
        <ListBox HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" Margin="4,5,5,5"                  
           Name="lstApplicationDetailsProperties" 
           Grid.Column="2" Grid.Row="1"  />
   </Grid>
</UserControl>

image

我们将一个 `DataGrid` 拖放到页面上。

这将自动添加支持 `DataGrid` 所需的程序集。

image

我们单击并拖动网格的角以调整其大小,使其填满页面的下半部分。

image

在 `DataGrid` 的属性中,我们选中“`AutoGenerateColumns`”旁边的复选框。

image

我们将一个 `Label` 拖放到第一个框上方,并将其 `Content` 更改为“**Active Screens**”。

image

我们添加另外两个 `Label`,并将其命名为 `Methods` 和 `Collections`。

向Silverlight控件添加代码

image

打开 *LightSwitchExplorer.xaml.cs* 文件,并添加以下代码:

// This property is created because it allows us to raise 
// "OnExplorerControlDataContextPropertyChanged"
// When the DataContext is updated
public static readonly DependencyProperty ExplorerControlDataContextProperty =
    DependencyProperty.Register("DummyProperty", typeof(IContentItem),
    typeof(LightSwitchExplorer),
    new PropertyMetadata(OnExplorerControlDataContextPropertyChanged));

image

接下来,在 `InitializeComponent();` 行之后添加此行:

this.SetBinding(ExplorerControlDataContextProperty, new Binding());

这会设置 `ExplorerControlDataContextProperty` 的绑定。

添加以下方法:

#region ShowMethodsOnScreen
private static void ShowMethodsOnScreen(LightSwitchExplorer EC, 
                    IContentItem contentItem)
{
    // Fill the lstMethods ListBox with
    // a list of all Methods on the current Screen
    foreach (var item in contentItem.Screen.Details.Commands.All())
    {
        EC.lstMethods.Items.Add(item.Name);
    }
}
#endregion

#region ShowActiveScreens
private static void ShowActiveScreens(LightSwitchExplorer EC, 
                    IContentItem contentItem)
{
    // Fill the lstActiveScreens ListBox with a list of all Active Screens
    foreach (var item in contentItem.Screen.Details.Application.ActiveScreens)
    {
        EC.lstActiveScreens.Items.Add(item.Screen.Name);
    }
}
#endregion

#region ShowScreenCollections
private static void ShowScreenCollections(LightSwitchExplorer EC, 
               IContentItem contentItem)
{
    // Fill the lstApplicationDetailsProperties ListBox with a list of all 
    // Collections on the current Screen
    foreach (var item in contentItem.Screen.Details.Properties.All())
    {
        EC.lstApplicationDetailsProperties.Items.Add(
                         String.Format("{0}", item.Name));
    }
}
#endregion

最后,添加以下方法,该方法将在 `DataContext` 设置或更改时触发之前的方法:

private static void OnExplorerControlDataContextPropertyChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    LightSwitchExplorer EC = (LightSwitchExplorer)d;
    IContentItem contentItem = (IContentItem)EC.DataContext;
    ShowActiveScreens(EC, contentItem);
    ShowMethodsOnScreen(EC, contentItem);
    ShowScreenCollections(EC, contentItem);
}

将Silverlight控件插入应用程序

image

**构建**解决方案。

image

打开 `EditableProductsGrid` 屏幕,并添加一个新自定义控件。

image

单击“**添加引用**”按钮。

image

创建对 `SilverlightControlLibrary` 项目的项目引用。

image

现在我们将能够选择我们创建的**自定义控件**。

image

在控件的属性中,将**标签位置**设置为**无**。

image

当我们运行应用程序时,我们看到控件显示了活动屏幕、当前屏幕上的方法以及当前屏幕上的集合。

使 Explorer 控件具有交互性

image

返回Silverlight控件,并双击“Active Screens”ListBox。

image

该方法将被连接

为该方法使用以下代码

private void lstActiveScreens_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Get the LightSwitch DataContext
    IContentItem contentItem = (IContentItem)this.DataContext;
    // The selected screen
    string strScreenName = (sender as ListBox).SelectedValue.ToString();
    // Get the selected Screen
    var 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();
    }
}

image

双击“**Methods**”`ListBox`。

为该方法使用以下代码

private void lstMethods_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Get the LightSwitch DataContext
    IContentItem contentItem = (IContentItem)this.DataContext;
    // The selected Method
    string 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 Screen
        var Screen =
        (Microsoft.LightSwitch.Client.IScreenObject)contentItem.Screen;
        Screen.Details.Dispatcher.BeginInvoke(() =>
        {
            Method.Execute();
        });
    }
}

image

双击“**Collections**”`ListBox`。

为该方法使用以下代码

private void lstApplicationDetailsProperties_SelectionChanged(object sender, 
                                  SelectionChangedEventArgs e)
{
    // Get the LightSwitch DataContext
    IContentItem contentItem = (IContentItem)this.DataContext;
    // The Property Name
    string 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;
}

Explorer 控件

image

将 Explorer 控件添加到应用程序的其他页面。

  • 你可以单击**活动屏幕**来切换屏幕。
  • 你可以单击**方法**来执行它们。
  • 你可以单击**集合**来查看它们的内容(并编辑它们)。

这仅仅是冰山一角

此示例几乎没有触及LightSwitch开发人员可用的所有功能。有关用户、完整数据库架构和数据以及整个应用程序的信息是可用的。

在Silverlight应用程序中手动创建通常需要数百行代码的操作,在使用LightSwitch时,仅用六行代码即可完成。

特别感谢

特别感谢微软的 **Sheel Shah**,否则本文将无法完成。

延伸阅读

更多LightSwitch内容

你可以在 http://lightswitchhelpwebsite.com/ 上找到大量关于高级 **Visual Studio LightSwitch** 编程的文章。

历史

  • 2011年9月10日:初始版本
© . All rights reserved.