Silverlight快速开发框架RapidSL新特性

开发 后端
已经实现了轻量级的权限管理,上传模块,内容管理,作为实例,涉及到了sl开发的各种技术难点和技巧,既可以作为学习,也可以作为项目开发的原型

对sl传统的开发方式进行了集成和封装,核心模块基于MVVM,通用的CRUD ViewModel,只需要定制自己的Xaml View,提供了非常便捷的快速开发方式; 采用了Silverlight 5.0 + EF4.1 Code First + Ria Service SP2 + Ria Service Toolkit + Silverlight Control Toolkit + Light MVVM;已经实现了轻量级的权限管理,上传模块,内容管理,作为实例,涉及到了sl开发的各种技术难点和技巧,既可以作为学习,也可以作为项目开发的原型

点击预览 | 源代码 

支持动态加载.xap,面向插件开发

RapidSL.SL.App.Portal提供主框架的UI逻辑,只需要开发自己的App,如RapidSL.SL.App.Main

 

然后配置菜单:

  1. <sdk:TreeViewItem Header="产品管理" IsExpanded="True">  
  2.                                 <controls:AdminMenuItem Id="1" Margin="2" Content="ProductEdit" NavigateView="RapidSL.SL.App.Main.xap/Product.Index" ViewPermission="ProductView"/>  
  3.                                 <controls:AdminMenuItem Id="2" Margin="2" Content="CategoryEdit" NavigateView="RapidSL.SL.App.Main.xap/Category.Index" ViewPermission="CategoryView"/>  
  4.                            </sdk:TreeViewItem> 

XapHost控件提供动态下载.xap及加载

  1. public XapHost(string xapUri, string viewName = null)  
  2.          {  
  3.              InitializeComponent();  
  4.              this.FileName = xapUri;  
  5.              var xapLoad = new XapLoader(xapUri);  
  6.              xapLoad.DownloadProgressChanged += (s, e) =>  
  7.              {  
  8.                  this.TotalSize = (e.TotalBytesToReceive * 1d / 1024 / 1024).ToString("0.00");  
  9.                  this.Percentage = e.ProgressPercentage;  
  10.              };  
  11.              xapLoad.LoadCompleted += (s, e) =>  
  12.              {  
  13.                  this.Content = e.Element;  
  14.              };  
  15.              xapLoad.LoadControl(null, viewName);  
  16.          } 

对Resource的支持

找到所有标识有 StaticResourceAttribute的类,然后创建相关实例,并注入到Application.Resources,相当于在 App.xaml里手写资源

实现了资源管理器对资源进行注入管理

  1. View Code   
  2.  public class ViewModelManager  
  3.      {  
  4.          private static Application app = Application.Current;   
  5.          public static void InjectViewModelsToResources()  
  6.          {  
  7.              foreach (AssemblyPart ap in Deployment.Current.Parts)  
  8.              {  
  9.                  var sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));  
  10.                  var assembly = new AssemblyPart().Load(sri.Stream);  
  11.    
  12.                  InjectViewModelsToResources(assembly);               
  13.              }   
  14.          }   
  15.          public static void InjectViewModelsToResources(Assembly assembly)  
  16.          {  
  17.              foreach (Type type in assembly.GetTypes())  
  18.              {  
  19.                  var attributes = type.GetCustomAttributes(false);  
  20.    
  21.                  foreach (var attribute in attributes)  
  22.                  {  
  23.                      if (attribute is StaticResourceAttribute)  
  24.                      {  
  25.                          var resourceKey = ((StaticResourceAttribute)attribute).Key;  
  26.                          if (string.IsNullOrEmpty(resourceKey))  
  27.                              resourceKey = type.Name;   
  28.                          var obj = Activator.CreateInstance(type);  
  29.                          if (!app.Resources.Contains(resourceKey))  
  30.                              app.Resources.Add(resourceKey, obj);  
  31.                      }  
  32.                  }  
  33.              }  
  34.          }  
  35.          public static T GetViewModelFromResources<T>()  
  36.          {  
  37.              var key = typeof(T).Name;  
  38.              if (app.Resources.Contains(key))  
  39.                  return (T)app.Resources[key];  
  40.              else 
  41.                  return default(T);  
  42.          }  
  43.      } 

键盘Enter键提交表单

使用AttatchProperty实现传统Html表单的键盘Enter提交

  1. <Grid x:Name="LayoutRoot" core:AttachProperties.SubmitButton="{Binding ElementName=submit}">  
  2. <Button x:Name="submit" Content="登录" Margin="20,0,20,0" Padding="20,0,20,0" Command="{Binding UserLogin}"/>  
  3. </Grid> 

具体绑定按钮和键盘事件

  1. #region SubmitButton AttachProperty  
  2.          public static object GetSubmitButton(DependencyObject obj)  
  3.          {  
  4.              return (object)obj.GetValue(SubmitButtonProperty);  
  5.          }  
  6.    
  7.          public static void SetSubmitButton(DependencyObject obj, object value)  
  8.          {  
  9.              obj.SetValue(SubmitButtonProperty, value);  
  10.          }  
  11.    
  12.          // Using a DependencyProperty as the backing store for SubmitButton.  This enables animation, styling, binding, etc...  
  13.          public static readonly DependencyProperty SubmitButtonProperty =  
  14.              DependencyProperty.RegisterAttached("SubmitButton", typeof(object), typeof(AttachProperties), new PropertyMetadata(SubmitButtonChanged));  
  15.    
  16.          private static void SubmitButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
  17.          {  
  18.              var button = (ButtonBase)e.NewValue;  
  19.              var form = d as UIElement;  
  20.              form.KeyDown += (s, se) =>  
  21.              {  
  22.                  if (se.Key == Key.Enter)  
  23.                  {  
  24.                      button.Focus();  
  25.                      if (button.Command != null)  
  26.                         button.Dispatcher.BeginInvoke(()=>  button.Command.Execute(null));  
  27.                  }  
  28.              };  
  29.          }  
  30.          #endregion 

原文链接:http://www.cnblogs.com/guozili/archive/2012/08/28/2659035.html

【编辑推荐】

  1. Silverlight 3D开源项目
  2. Silverlight 5 项目使用async/await
  3. 基于silverlight一个“树形结构图”控件
  4. 游戏人生Silverlight:贪吃蛇
  5. 游戏人生Silverlight:星际竞技场
责任编辑:张伟 来源: guozili的博客
相关推荐

2009-07-27 09:46:28

Silverlight

2010-12-06 09:33:21

Silverlight

2010-08-06 09:18:14

Flex

2011-11-08 10:36:42

Java

2010-07-29 09:03:18

Silverlight

2009-11-26 11:29:46

Silverlight

2014-10-20 13:57:59

JavaFX 8Java 8

2019-09-02 14:51:33

2012-05-15 15:44:00

Silverlight

2011-05-20 10:15:06

JDK7

2011-04-22 10:45:14

SimpleFrame

2009-12-31 17:21:41

Silverlight

2010-06-09 10:48:55

F#Silverlight

2013-07-22 11:18:02

Android开发框架Afinal

2023-10-06 16:58:57

2011-06-14 17:02:43

Xcode 4Cocoa TouchiOS

2022-12-25 10:53:47

2009-03-26 08:57:22

微软IE8浏览器

2012-02-02 16:32:23

Silverlight控件模型分类

2009-12-31 15:13:47

Silverlight
点赞
收藏

51CTO技术栈公众号