简体   繁体   English

UWP应用架构困境

[英]UWP app architecture dilemma

I am newbie at UWP developing (design targeting phones), and now i am developing one app using it (have experience in Android developing). 我是UWP开发(针对手机的设计)的新手,现在我正在使用它开发一个应用程序(具有Android开发经验)。 App is designed as single-page app which has a map and tons of another menus, buttons and field: they are appear and disappear based on some logic. 该应用程序被设计为单页应用程序,具有地图以及大量其他菜单,按钮和字段:它们根据某种逻辑出现和消失。 Many of this controls rendering or filling in runtime (without/partially use xaml, just in c#, if possible viewmodel getting used), because it's loading from API. 其中许多控件用于渲染或填充运行时(如果可能的话,不使用xaml,仅在c#中使用xaml,如果可能的话,使用viewmodel),因为它是从API加载的。 The dilemma is page class (also as xaml one) going to be larger and larger, and i am not sure how to separate this correctly (i am not mean partial classes), for not all this controls getting loaded on app launch (now they are, and i collapse/visible needed). 难题是页面类(也就是xaml类)会越来越大,我不确定如何正确地将其分开(我不是指部分类),因为并不是所有这些控件都在应用程序启动时加载(现在它们是,我崩溃/可见需要)。 For example, in Android terminology there is fragments can replace/overlay each other with animations etc. Now i have one extension class where i interact with API and only send callbacks to page class, but it's not enough =) 例如,在Android术语中,有片段可以用动画等替换/叠加。现在,我有一个扩展类,可以与API交互,仅向页面类发送回调,但这还不够=)

One way to reduce complexity of a page is breaking it into separate UserControls . 降低页面复杂度的一种方法是将其分为单独的UserControls Combining that with MVVM pattern , you can also separate the view-logic in multuple view-models classes. 将其与MVVM模式相结合,还可以在多个视图模型类中分离视图逻辑。

As an example, consider you have a master detail layout in your page, let's say this view shows users information. 举例来说,假设您的页面中有一个主要的细节布局,假设此视图显示了用户信息。 You will have three separate views: 您将拥有三个单独的视图:

  • UserView (inherits Page) - Activity in android UserView(继承页面)-Android中的活动
  • UserListView (inherits UserControl) - Fragment in android UserListView(继承自UserControl)-Android中的片段
  • UserDetailsView (inherits UserControl) - Fragment in android UserDetailsView(继承自UserControl)-Android中的片段

and also three separate view models respectively. 以及三个单独的视图模型。 There are some challenges with this pattern: 这种模式存在一些挑战:

Communication 通讯

Your views need to communicate to each other. 您的视图需要相互交流。 For example, when you select a user in the UserListView, the UserDetailsView need to update and shows details of the selected user. 例如,当您在UserListView中选择一个用户时,UserDetailsView需要更新并显示所选用户的详细信息。

There are at least three ways to communicate between these views: 这些视图之间至少存在三种通信方式:

  • Views directly call each other through methods and dependency properties 视图通过方法和依赖项属性直接相互调用
  • Views communicate indirectly through the model 视图通过模型间接通信
  • Views communicate indirectly through their view-model objects 视图通过其视图模型对象进行间接通信

I usually choose first and second methods and not the third one. 我通常选择第一种和第二种方法,而不是第三种。

Shared UI Resources 共享的UI资源

Views in a page may want to use the shared resource in the page, the command bar for example. 页面中的视图可能要使用页面中的共享资源,例如命令栏。 For example, the UserListView may need to have a Add (+) button on the command bar, also the UserDetailsView may need a few buttons as well. 例如,UserListView可能需要在命令栏上有一个Add(+)按钮,UserDetailsView也可能需要一些按钮。 This will be challenge for you to handle this kind of situations. 这将是您应对这种情况的挑战。

You can separate your logic using MVVM Pattern. 您可以使用MVVM模式分离逻辑。

https://blogs.msdn.microsoft.com/johnshews_blog/2015/09/09/a-minimal-mvvm-uwp-app/ https://blogs.msdn.microsoft.com/johnshews_blog/2015/09/09/a-minimal-mvvm-uwp-app/

You can use the power of XAML to hide or show your controls depending of the business rules, using Binding or x:Bind 您可以使用Binding或x:Bind使用XAML的功能根据业务规则隐藏或显示控件。

Using MVVM Pattern is the best practice to create XAML based applications like UWP, WPF. 使用MVVM模式是创建基于XAML的应用程序(如UWP,WPF)的最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM