[译]LightSwitch 如何实现:在查询中创建和使用全局值(Eric Erhardt)

原文标题:How Do I: Create and Use Global Values In a Query (Eric Erhardt)

原文链接地址:http://blogs.msdn.com/b/lightswitch/archive/2010/09/16/how-do-i-create-and-use-global-values-in-a-query-eric-erhardt.aspx

 

在查询中使用全局值

在Visual Studio LightSwitch中可以使用的全局值基本上都是基于Date和DateTime类型的:
•Now
•Today
•End of Day
•Start of Week
•End of Week
•Start of Month
•End of Month
•Start of Quarter
•End of Quarter
•Start of Year
•End of Year

 

定义全局值

改为文件视图,在Data文件夹中使用 “XML (Text) Editor”对ApplicationDefinition.lsml进行编辑添加如下代码,注意要做好备份。

<?xml version="1.0" encoding="utf-8" ?>
<ModelFragment xmlns="http://schemas.microsoft.com/LightSwitch/2010/xaml/model"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <GlobalValueContainerDefinition Name="GlobalDates">
    <GlobalValueDefinition Name="ThirtyDaysAgo" ReturnType=":DateTime">
      <GlobalValueDefinition.Attributes>
        <DisplayName Value="30 Days Ago" />
        <Description Value ="Gets the date that was 30 days ago." />
      </GlobalValueDefinition.Attributes>
    </GlobalValueDefinition>
  </GlobalValueContainerDefinition>
然后再 “Common” 项目中添加新类库文件,名称有上面代码中定义的相同,如GlobalDates。
using System;
namespace ContosoSales  // The name of your application
{
    public class GlobalDates  // The name of your GlobalValueContainerDefinition
    {
        public static DateTime ThirtyDaysAgo()  // The name of the GlobalValueDefinition
        {
            return DateTime.Today.AddDays(-30);
        }
    }
}
以上工作完成后,切换到逻辑视图,在应用程序解决方案上右击选择 “Reload Designer”.随后即可想系统内建的全局值一样使用自定义的全局值。
enjoy!
posted @ 2011-10-24 15:47  kissu  阅读(233)  评论(0编辑  收藏  举报