Silverlight快速開發(fā)框架RapidSL新特性
對sl傳統(tǒng)的開發(fā)方式進(jìn)行了集成和封裝,核心模塊基于MVVM,通用的CRUD ViewModel,只需要定制自己的Xaml View,提供了非常便捷的快速開發(fā)方式; 采用了Silverlight 5.0 + EF4.1 Code First + Ria Service SP2 + Ria Service Toolkit + Silverlight Control Toolkit + Light MVVM;已經(jīng)實現(xiàn)了輕量級的權(quán)限管理,上傳模塊,內(nèi)容管理,作為實例,涉及到了sl開發(fā)的各種技術(shù)難點和技巧,既可以作為學(xué)習(xí),也可以作為項目開發(fā)的原型
支持動態(tài)加載.xap,面向插件開發(fā)
RapidSL.SL.App.Portal提供主框架的UI邏輯,只需要開發(fā)自己的App,如RapidSL.SL.App.Main
然后配置菜單:
- <sdk:TreeViewItem Header="產(chǎn)品管理" IsExpanded="True">
- <controls:AdminMenuItem Id="1" Margin="2" Content="ProductEdit" NavigateView="RapidSL.SL.App.Main.xap/Product.Index" ViewPermission="ProductView"/>
- <controls:AdminMenuItem Id="2" Margin="2" Content="CategoryEdit" NavigateView="RapidSL.SL.App.Main.xap/Category.Index" ViewPermission="CategoryView"/>
- </sdk:TreeViewItem>
XapHost控件提供動態(tài)下載.xap及加載
- public XapHost(string xapUri, string viewName = null)
- {
- InitializeComponent();
- this.FileName = xapUri;
- var xapLoad = new XapLoader(xapUri);
- xapLoad.DownloadProgressChanged += (s, e) =>
- {
- this.TotalSize = (e.TotalBytesToReceive * 1d / 1024 / 1024).ToString("0.00");
- this.Percentage = e.ProgressPercentage;
- };
- xapLoad.LoadCompleted += (s, e) =>
- {
- this.Content = e.Element;
- };
- xapLoad.LoadControl(null, viewName);
- }
對Resource的支持
找到所有標(biāo)識有 StaticResourceAttribute的類,然后創(chuàng)建相關(guān)實例,并注入到Application.Resources,相當(dāng)于在 App.xaml里手寫資源
實現(xiàn)了資源管理器對資源進(jìn)行注入管理
- View Code
- public class ViewModelManager
- {
- private static Application app = Application.Current;
- public static void InjectViewModelsToResources()
- {
- foreach (AssemblyPart ap in Deployment.Current.Parts)
- {
- 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實現(xiàn)傳統(tǒng)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>
具體綁定按鈕和鍵盤事件
- #region SubmitButton AttachProperty
- public static object GetSubmitButton(DependencyObject obj)
- {
- return (object)obj.GetValue(SubmitButtonProperty);
- }
- public static void SetSubmitButton(DependencyObject obj, object value)
- {
- 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
原文鏈接:http://www.cnblogs.com/guozili/archive/2012/08/28/2659035.html
【編輯推薦】