第一个Windows 10 IoT核心版Windows应用程序的改进(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/pi/improvement-on-first-windows-iot-core-windows-appl-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2599 个词 阅读量 0第一个Windows 10 IoT核心版Windows应用程序的改进(译文)
原文地址:https://www.codeproject.com/Articles/1095035/Improvement-on-First-Windows-IoT-Core-Windows-Appl
原文作者:ChristianLavigne
译文由本站 robot-v1.0 翻译
前言
Improvement on First Windows 10 IoT Core Windows Application
第一个Windows 10 IoT核心版Windows应用程序的改进 既然我的第一个应用程序正在运行,我想看看我可以在Raspberry Pi上采用该应用程序编写模型.(Now that I have a working first application working, I wanted to see how far I can take the application writing model on the Raspberry Pi.) 我们的第一个Windows应用程序是WPF应用程序.让我们看看是否可以利用WPF的全部功能,并将其提升到一个新的水平.(Our first Windows application is a WPF application. Let’s see if we can leverage the full power of WPF and take it to the next level.)
为什么(Why)
WPF应用程序将与页面上的硬编码事件处理程序一起使用.但是,有一种更好的,更全面的WPF方法.称为MVVM.(WPF application will work with hardcoded event handlers on the page. But there is a better more comprehensive way to do WPF. It’s called MVVM.) 作为一个纯粹主义者,我宁愿做正确的事.因此,我想确保Windows 10 IoT核心版应用程序的WPF支持MVVM.(Being a purist, I would much rather do things correctly. So I want to make sure WPF for Windows 10 IoT Core applications supports MVVM.) 对于这个小型应用程序,MVVM有点过大.但是一旦我们的应用程序变得越来越复杂,MVVM将使事情变得更容易管理.(MVVM is a bit of an overkill for this small application. But once our applications start becoming more and more complicated, MVVM will make things a lot easier to manage.)
WPF,XAML和MVVM(WPF, XAML and MVVM)
在开始之前,请注意,我将不在这里详细介绍. MVVM比这个小博文可以回答的领域要广泛得多且复杂得多.我将为您提供快速概述,并提供示例.如果您需要更多信息,请随时使用Google.(Before we start, note that I will not go into details here. MVVM is a much wider and complicated field than what this little blog post can answer. I’m just going to give you a quick overview and provide you with an example. If you need more information, feel free to Google it.) MVVM(模型,视图,视图模型)是一种设计模式. MVVM最强大的功能之一是它可以将UI与功能分开. MVVM由3个不同的组件组成:(MVVM (Model, View, ViewModel) is a design pattern. One of the most powerful features of MVVM is that it can separate the UI from the functionality. MVVM is composed of 3 different components:)
-
模型(Model):包含数据(: Contains the data)
-
视图(View):提供一个屏幕(: Provides a screen)
-
视图模型(ViewModel):提供功能(: Provides the functionality) WPF特别适合MVVM模式.(WPF is particularly well suited for the MVVM pattern.) XAML是WPF应用程序的View语言. XAML是一种声明性语言,可以与实际功能代码完全分开地进行编辑.这意味着开发人员可以编写所有功能,然后让设计人员构建屏幕.(XAML is the View language for WPF application. XAML is a declarative language that can be edited completely separate from the actual functionality code. This means that the developers can write all the functionality, and then let the designers build the screen.) 在WPF下对MVVM的需求是:(What we need for MVVM under WPF is:)
-
数据绑定(Data Binding)
-
指挥支持(Commanding support)
我对MVVM的看法(My Take on MVVM)
您会注意到我们没有实现模型.那是因为没有我们的应用程序管理的真实数据.(You will notice that we do not implement a Model. That is because there is no real data our application manages.) 让我们开始吧:(Let’s get started:)
实用程序类(Utility Classes)
首先,我需要几个实用程序类来支持数据绑定和命令.(First, I need a couple of utility classes to support Data Binding and Commands.)
BaseVM
:实施(: Implements the) INotifyPropertyChanged
界面,以便功能((interface so that the functionality () ViewModel
)可以通知UI某些更改.这是代码:() can notify the UI that something changed. Here is the code:)
public class BaseVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
Debug.WriteLine("OnPropertyChanged: {0}", propertyName);
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
VMCommand
:实施(: Implements the) ICommand
界面,以便UI可以向该功能发送命令((interface so that the UI can send commands to the functionality () ViewModel
).这是代码:(). Here is the code:)
public delegate bool CanExecuteDelegate(object o);
public delegate void ExecuteDelegate(object o);
public class VMCommand : ICommand
{
public VMCommand(ExecuteDelegate execute)
{
ExecuteFunction = execute;
CanExecuteFunction = null;
}
public VMCommand(ExecuteDelegate execute, CanExecuteDelegate canexec)
{
ExecuteFunction = execute;
CanExecuteFunction = canexec;
}
private ExecuteDelegate ExecuteFunction { get; set; }
private CanExecuteDelegate CanExecuteFunction { get; set; }
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
bool result = true;
if (CanExecuteFunction != null)
{
result = CanExecuteFunction(parameter);
}
return result;
}
public void Execute(object parameter)
{
if (ExecuteFunction != null)
{
ExecuteFunction(parameter);
}
}
public void OnCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
}
}
视图模型(ViewModel)
然后,我在(Then I implemented the functionality in the) ViewModel
.注意:(. Notice that:)
- 的(The)
ViewModel
真正代表业务.它的(truly represents the business. Its)public
成员是:(members are:)GreetingMessage
,(,)PersonName
和(and)GreetCommand
.(.) - 的代码实现(The code implementation for the)
GreetExecute
函数非常简单,它读取/写入属性(function is very simple, it reads/writes properties of the)ViewModel
并且不知道对(and is not aware of controls on the)View
.(.) 这是代码:(Here is the code:)
public class MainPageVM : BaseVM
{
public MainPageVM()
{
InitializeCommands();
}
private string m_GreetingMessage = "Hello World!";
public string GreetingMessage
{
get
{
return m_GreetingMessage;
}
set
{
if(value != m_GreetingMessage)
{
m_GreetingMessage = value;
OnPropertyChanged();
}
}
}
private string m_PersonName = string.Empty;
public string PersonName
{
get
{
return m_PersonName;
}
set
{
if (value != m_PersonName)
{
m_PersonName = value;
OnPropertyChanged();
}
}
}
public VMCommand GreetCommand { get; set; }
private void InitializeCommands()
{
GreetCommand = new VMCommand(GreetExecute);
}
private void GreetExecute(object o)
{
string greet = "Hello World!";
if (!string.IsNullOrEmpty(PersonName))
{
greet = string.Format("Hello {0}!", PersonName);
}
GreetingMessage = greet;
}
}
视图(View)
然后,我编写了XAML视图.注意:(Then I wrote the XAML View. Notice that:)
- 我们声明一个(We declare a)
ViewModel
作为我们实例化的资源(as a resource that we instantiate as the)DataContext
顶部(of the top)Grid
.这就是我们访问(. This is how we access the)ViewModel
.(.) - 这些控件不需要命名,因为在代码的任何地方都没有引用它们.(The controls do not need to be named, because they are not referenced anywhere in the code.)
- 的(The)
Text
控件的属性绑定到(properties of controls are bound to the)ViewModel
成员.因为(members. Because the)ViewModel
源自于(derives from)BaseVM
,内容(, the content of)Text
只要(will change whenever the content of the)ViewModel
属性更改.也当(property changes. Also when the)TextBox
失去焦点,它将自动更新视图模型.(loses focus, it will automatically update the view model.) - 另外,(Also, there is no event handler on the)
Button
而是绑定到(instead there is a binding to the)Command
在(on the)View
模型.(Model.) 这是代码:(Here is the code:)
<Page
x:Class="HelloWorld.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HelloWorld"
xmlns:vm="using:HelloWorld.VM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<vm:MainPageVM x:Key="PageViewModel" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{StaticResource PageViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="51"/>
<RowDefinition Height="51"/>
<RowDefinition Height="51"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Text="{Binding GreetingMessage}"
HorizontalAlignment="Center"/>
<TextBox Grid.Row="2" Text="{Binding PersonName, Mode=TwoWay}"
HorizontalAlignment="Center" Margin="5" Width="150"/>
<Button Grid.Row="3" Content="Greet"
Command="{Binding GreetCommand}" HorizontalAlignment="Center" />
</Grid>
</Page>
测试中(Testing)
现在,我们的应用程序已编写完毕,我们将对其进行构建和测试.首先,我在本地计算机上对其进行了测试.然后,我切换到远程调试以在Raspberry Pi上对其进行测试.(Now that our application is written, we will build and test it. First, I tested it on my local machine. Then, I switched to Remote Debugging to test it on the Raspberry Pi.)
结论(Conclusion)
MVVM是用于编写WPF应用程序的非常强大的模型.我很高兴看到Windows 10 IoT核心版应用程序可以支持以MVVM编写的完整WPF应用程序.(MVVM is a very powerful model for writing WPF application. I am glad to see that the Windows 10 IoT Core application can support full WPF application written in MVVM.) 这意味着我们可以更轻松地创建更多进化和复杂的应用程序.(This means we can create much more evolved and complicated applications more easily.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
All-Topics 新闻 翻译