以前做UWP开发都是使用MvvmLight,主要是简单易上手,同时也写了很多MvvmLight的开发系列文章:

UWP开发必备以及常用知识点总结

UWP开发之Mvvmlight实践九:基于MVVM的项目架构分享

UWP开发之Mvvmlight实践八:为什么事件注销处理要写在OnNavigatingFrom中

UWP开发之Mvvmlight实践七:如何查找设备(Mobile模拟器、实体手机、PC)中应用的Log等文件

UWP开发之Mvvmlight实践六:MissingMetadataException解决办法(.Net Native下Default.rd.xml配置问题)

UWP开发之Mvvmlight实践五:SuspensionManager中断挂起以及复原处理

UWP开发之Mvvmlight实践四:{x:bind}和{Binding}区别详解

UWP开发之Mvvmlight实践三:简单MVVM实例开发(图文详解付代码)

UWP开发之Mvvmlight实践二:Mvvmlight的核心框架MVVM与MVC、MVP的区别(图文详解)

UWP开发之Mvvmlight实践一:如何在项目中添加使用Mvvmlight(图文详解)

出于开发效率考虑,以后打算都使用Prism或者Template10开发,其中很多的实现原理与MvvmLight相似。此次基于Prism.Windows完成的UWP应用虽然功能不是那么复杂,但是基本上用全了Prism的优良特性,特写下次篇做备忘。

总结一:IOC控制反转容器

目前只支持Unity,SimpleInjector,Autofac三个,相比MvvmCross或者Mvvmlight框架则选择更加灵活。

使用方法(例如:Unity):

1,App类继承于IOC对应的Application类。

sealed partial class App : PrismUnityApplication
{}

2,依赖注入。(最好所有的注入都写在这个方法里面,后续的启动处理以及页面转移等可以立马使用。)

protected override Task OnInitializeAsync(IActivatedEventArgs args)
{
    Container.RegisterInstance<INavigationService>(NavigationService);
    Container.RegisterInstance<ISessionStateService>(SessionStateService);

Container.RegisterType<ISettingService, SettingService>(new ContainerControlledLifetimeManager());

return base.OnInitializeAsync(args);
}

3,调用。

  • App.cs内调用:

await Container.Resolve<IAppStartupService>().CreateDataBaseAsync();

  • ViewModel内使用:
private INavigationService _navigationService;
public HomePageViewModel(INavigationService navigationService)
{     _navigationService = navigationService;
}
 

总结二:自动绑定

自动绑定的命名规则:

View名:HomePage

ViewModel名:HomePageViewModel

其次Views与ViewModels文件夹最好在同一个程序集下,然后View的头部添加如下两行代码,就可以自动绑定ViewModel。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
Title="MainPage">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="{Binding Title}" />
</StackLayout>
</ContentPage>

如果View与ViewModel分层了,通过自定义ViewModelTypeResolver也可以实现自动绑定。

ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(
    viewType => {

// 自由设置
        return Type.GetType("");
    });

Prism.Mvvm下ViewModelLocationProvide.cs的默认设置如下:
        static Func<Type, Type> _defaultViewTypeToViewModelTypeResolver =

viewType =>

{

var viewName = viewType.FullName;

viewName = viewName.Replace(".Views.", ".ViewModels.");

var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;

var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";

var viewModelName = String.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);

return Type.GetType(viewModelName);

};

总结三:NavigationService

全部接口:

  1. bool Navigate(string pageToken, object parameter);
  2. void GoBack();
  3. bool CanGoBack();
  4. void GoForward();
  5. bool CanGoForward();
  6. void ClearHistory();
  7. void RemoveFirstPage(string pageToken = null, object parameter = null);
  8. void RemoveLastPage(string pageToken = null, object parameter = null);
  9. void RemoveAllPages(string pageToken = null, object parameter = null);
  10. void RestoreSavedNavigation();
  11. void Suspending();

有了它再也不用担心一些特殊的页面漂移问题。

1,App.cs内部使用。(类以及包含NavigationService成员属性):

// 启动页面表示
NavigationService.Navigate("Home", null);

2,ViewModel使用。

        private INavigationService _navigationService;

        public HomePageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
 
_navigationService.ClearHistory()等
 

注意:

由于10,11 的记录保存与复原都是使用DataContractSerializer序列化,所以页面漂移参数最好使用字符串或者数字。

总结四:中断还原处理

SessionStateService是类似SuspensionManager专门处理中断。

1,App.cs内部设置

依赖注入

protected override Task OnInitializeAsync(IActivatedEventArgs args)
{
    Container.RegisterInstance<ISessionStateService>(SessionStateService);

return base.OnInitializeAsync(args);
}

类型注册

protected override void OnRegisterKnownTypesForSerialization()
{
SessionStateService.RegisterKnownType(typeof(FavoriteInfo));
SessionStateService.RegisterKnownType(typeof(ServerInfo));
// 这个比较重要
SessionStateService.RegisterKnownType(typeof(ObservableCollection<FavoriteInfo>));
}

2,ViewPage类型设置

<prismMvvm:SessionStateAwarePage
 x:Class="PrismUnityApp1.Views.SettingPage"
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"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d">
 

3,ViewModel中数据成员属性设置

 
private ObservableCollection<FavoriteInfo> _favoriteFolders;
[RestorableState]
public ObservableCollection<FavoriteInfo> FavoriteFolders
{    
  get     {         return _favoriteFolders;     }     
  set     {         SetProperty(ref _favoriteFolders, value);     }
}
 

4,.net native相关的Default.rd.xml设置

<Type Name="PrismUnityApp1.DataModels.FavoriteInfo"
DataContractSerializer="Required Public" />       
<Namespace Name="System.Collections.ObjectModel" >        
  <TypeInstantiation Name="ObservableCollection"  Arguments="PrismUnityApp1.DataModels.FavoriteInfo"
DataContractSerializer="Required Public" />    
</Namespace>
 

总结五:ResourceLoader

1,依赖注入。

protected override Task OnInitializeAsync(IActivatedEventArgs args)

{

Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));

return base.OnInitializeAsync(args);

}

2,调用GetString()获取数据。

        public MenuViewModel(INavigationService navigationService, 

IResourceLoader

 resourceLoader)

        {

            _navigationService = navigationService;

            Commands = new ObservableCollection<MenuItemViewModel>

            {

                new MenuItemViewModel { DisplayName = resourceLoader.GetString("MainPageMenuItemDisplayName"), FontIcon = "\ue15f", Command = new DelegateCommand(NavigateToMainPage, CanNavigateToMainPage) },

                new MenuItemViewModel { DisplayName = resourceLoader.GetString("SecondPageMenuItemDisplayName"), FontIcon = "\ue19f", Command = new DelegateCommand(NavigateToSecondPage, CanNavigateToSecondPage) }

            };

        }

总结六:属性验证Validatable

类似Asp.net MVC支持以下Validation:

  • Required
  • CreditCard
  • EmailAddress
  • PhoneNumber
  • Range
  • MinLength
  • MaxLenght
  • RegularExpression
  • Enumeration
  • URL

使用方法:1,继承与ValidatableBindableBase

public class Address : 

ValidatableBindableBase

 

2,成员设置:

[Required(ErrorMessage = "Name is required.")]
public string Name
{
get { return name; }
set { SetProperty(ref name, value); }
} [RegularExpression(@"[ABCD]\d{2,5}", ErrorMessage="Location is Quadrant (A -> D) and Sector (2 -> 5 digits)")]
public string Location
{
get { return location; }
set { SetProperty(ref location, value); }
}
 

3,控件绑定

<TextBox Text="{Binding Name, Mode=TwoWay}" Header="Name" /> 
<TextBlock Text="{Binding Errors[Name][0]}" Foreground="Red" HorizontalAlignment="Right" />
 
<ItemsControl x:Name="ErrorList" ItemsSource="{Binding Errors.Errors}" />
 

备注:通过继承ValidationAttribute 可以自定义各种验证逻辑。

 

总结七:菜单设置时机

App.cs内部的CreateShell方法中设置:

        protected override UIElement CreateShell(Frame rootFrame)
{
var menuPage = Container.Resolve<MenuPage>();
menuPage.NavigationFrame.Content = rootFrame;
return menuPage;
}
 

总结八:数据库创建时机

以前我们一般都是在OnLaunched方法内部添加如下代码实现:

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame; // 不要在窗口已包含内容时重复应用程序初始化,
// 只需确保窗口处于活动状态
if (rootFrame == null)
{
// 创建要充当导航上下文的框架,并导航到第一页
rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: 从之前挂起的应用程序加载状态
} // 将框架放在当前窗口中
Window.Current.Content = rootFrame;
} if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// 数据库创建
createDBAsync();
// 当导航堆栈尚未还原时,导航到第一页,
// 并通过将所需信息作为导航参数传入来配置
// 参数
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// 确保当前窗口处于活动状态
Window.Current.Activate();
}
} Task createDBAsync()
{
// 略
return Task.FromResult(true);
}

由于Prism封装了Application类,所以代码添加的位置也变了:

protected override Task OnInitializeAsync(IActivatedEventArgs args)
{
createDBAsync();
return base.OnInitializeAsync(args);
}

基本就这些东西,总结一下方便以后开发,同时也希望对园友们有帮助。

基于Prism.Windows的UWP开发备忘的更多相关文章

  1. Windows 10 UWP开发:如何去掉ListView默认的选中效果

    原文:Windows 10 UWP开发:如何去掉ListView默认的选中效果 开发UWP的时候,很多人会碰到一个问题,就是ListView在被数据绑定之后经常有个默认选中的效果,就像这样: 而且它不 ...

  2. JS开发备忘笔记-- Javascript中document.execCommand()的用法

    document.execCommand()方法处理Html数据时常用语法格式如下:document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指令 ...

  3. 移动端web app开发备忘

    近期要做个手机html5的页面,做些知识储备,重要的点记录下来以备兴许. 1.devicePixelRatio:定义设备物理象素和设备独立象素的比例.css中的px能够看作是设备的独立象素.通过dev ...

  4. element-ui 开发备忘

    目录 购物清单的数据结构说明 1. <el-radio> 的 label 属性 2. 在 <el-table> 中放入表单组件 3. 表单验证时填写正确的 prop 属性 总结 ...

  5. Windows 10 UWP开发:如何不让界面卡死

    http://edi.wang/post/2016/2/18/windows-10-uwp-async-await-ui-thread 关于UI线程 这里我们需要一点关于 UI 线程模型的概念,简单的 ...

  6. 开发备忘:AngularJS Syntax error, unrecognized expression in template file

    在写基于Angular的项目过程中,运行 grunt test的时候,一直给我蹦出这个错误,导致我的test一直跑不过,怎么试都是失败,经过重复排查,发现是因为template file中的html元 ...

  7. 【WPF开发备忘】使用MVVM模式开发中列表控件内的按钮事件无法触发解决方法

    实际使用MVVM进行WPF开发的时候,可能会用到列表控件中每行一个编辑或删除按钮,这时直接去绑定,发现无法响应: <DataGridTemplateColumn Header="操作& ...

  8. Git开发备忘

    1.在Git中,上传了中文命名的文件,但是后面想删除的时候,发现中文命名被转义了. 利用Git add是无法添加这类文件的,所以这里我们需要用到 git add -u命令,即可实现成功添加. 2.在G ...

  9. IMX515开发备忘

    1.多个PAD可以选择为同样的功能引脚 IMX515处理器一个PAD可以作为多种功能引脚,比如EIM_D25可以作为UART3_RXD,定义如下: 图1 而处理还有一个另一个UART3_RXD的PAD ...

随机推荐

  1. 咖啡师之路:第一日一杯Espresso

    代码敲累了.产品要发布了.熬夜啊加班啊. 精神完全不在状态. 咋办--- 咋办--- 咋办---! 来一杯Espresso浓缩咖啡.各位码农,码神们的必备良品! 咖啡每天要2-3杯,不管是速溶还是现磨 ...

  2. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  3. JavaWeb之JSTL标签

    JSP中有了EL可以很方便的引用对象和属性,但是也有一些不足的地方,比如不能遍历对象列表这些,再加上JSTL(Java Standard Tag Library)的话那就完美了.JSTL主要包括cor ...

  4. WebX框架学习笔记之一

    Webx是什么? Webx是一套基于Java Servlet API的通用Web框架.它在Alibaba集团内部被广泛使用.从2010年底,向社会开放源码. Webx的发展历史 2001年,阿里巴巴内 ...

  5. Struts2之OGNL表达式

    OGNL(Object-Graph Navigation Language的简称),对象图导航语言,它是一门表达式语言,除了用来设置和获取Java对象的属性之外,另外提供诸如集合的投影和过滤以及lam ...

  6. 兼容主流浏览器的js原生函数封装

    1.获取和修改垂直滚动条的滚动距离 //获取滚动条滚动距离function getScrollTop(){ var fromTop=document.documentElement.scrollTop ...

  7. 面向对象编程思想(前传)--你必须知道的javascript

    在写面向对象编程思想-设计模式中的js部分的时候发现很多基础知识不了解的话,是很难真正理解和读懂js面向对象的代码.为此,在这里先快速补上.然后继续我们的面向对象编程思想-设计模式. 什么是鸭子类型 ...

  8. 腾讯云总监手把手教你,如何成为AI工程师?

    作者:朱建平 腾讯云技术总监,腾讯TEG架构平台部专家工程师 1.关于人工智能的若干个错误认知 人工智能是AI工程师的事情,跟我没有什么关系 大数据和机器学习(AI) 是解决问题的一种途径和手段,具有 ...

  9. PRINCE2的发展情况是什么样

    英国皇家特许培训机构AXELOS近期公布了一份调查结果,调查共有172位项目经理参与,其结果展示了未来全球趋势,对项目经理未来的职业形态和对他们必备技能的影响.  未来的项目管理职业  调查结果同时给 ...

  10. oracle查询锁表解锁语句

    --oracle查询锁表解锁语句--首先要用dba权限的用户登录,建议用system,然后直接看sql吧 --1. 如下语句 查询锁定的表: SELECT l.session_id sid, s.se ...