您的位置:首页 > 其它

基于Silverlight的快速开发框架RapidSL新特性解析

2012-08-28 08:18 543 查看


对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
View Code

1 public class ViewModelManager
2 {
3 private static Application app = Application.Current;
4
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));
var assembly = new AssemblyPart().Load(sri.Stream);

InjectViewModelsToResources(assembly);

}
}

public static void InjectViewModelsToResources(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
var attributes = type.GetCustomAttributes(false);

foreach (var attribute in attributes)
{
if (attribute is StaticResourceAttribute)
{
var resourceKey = ((StaticResourceAttribute)attribute).Key;
if (string.IsNullOrEmpty(resourceKey))
resourceKey = type.Name;

var obj = Activator.CreateInstance(type);
if (!app.Resources.Contains(resourceKey))
app.Resources.Add(resourceKey, obj);
}
}
}
}

public static T GetViewModelFromResources<T>()
{
var key = typeof(T).Name;
if (app.Resources.Contains(key))
return (T)app.Resources[key];
else
return default(T);
}
}


键盘Enter键提交表单



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

<Grid x:Name="LayoutRoot" core:AttachProperties.SubmitButton="{Binding ElementName=submit}">

<Button x:Name="submit" Content="登录" Margin="20,0,20,0" Padding="20,0,20,0" Command="{Binding UserLogin}"/>

</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);

}

// Using a DependencyProperty as the backing store for SubmitButton. This enables animation, styling, binding, etc...

public static readonly DependencyProperty SubmitButtonProperty =

DependencyProperty.RegisterAttached("SubmitButton", typeof(object), typeof(AttachProperties), new PropertyMetadata(SubmitButtonChanged));

private static void SubmitButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

{

var button = (ButtonBase)e.NewValue;

var form = d as UIElement;

form.KeyDown += (s, se) =>

{

if (se.Key == Key.Enter)

{

button.Focus();

if (button.Command != null)

button.Dispatcher.BeginInvoke(()=> button.Command.Execute(null));

}

};

}

#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: