入门:选择编程语言Getting started: Choosing a programming language

选择编程语言Choosing a programming language

在执行下一步操作之前,你应知道在开发通用 Windows 平台 (UWP) 应用时可从中选择的编程语言。Before we go any further, you should know about the programming languages that you can choose from when you develop Universal Windows Platform (UWP) apps. 尽管本文中的演练使用的是 C#,但你可以使用一种或多种编程语言开发 UWP 应用(请参阅语言、工具和框架)。Although the walkthroughs in this article use C#, you can develop UWP apps using one or more programming languages (see Languages, tools and frameworks).

可使用 C++、C#、Microsoft Visual Basic 和 JavaScript 进行开发。You can develop using C++, C#, Microsoft Visual Basic, and JavaScript. JavaScript 使用用于 UI 布局的 HTML5 标记,其他语言使用一种称为Extensible Application Markup Language (XAML) 的标记语言来描述其 UI。JavaScript uses HTML5 markup for UI layout, and the other languages use a markup language called Extensible Application Markup Language (XAML) to describe their UI.

尽管我们在本文中以 C# 为重点,但其他语言也提供了你可能希望尝试的独特优势。Although we're focusing on C# in this article, the other languages offer unique benefits, which you may want to explore. 例如,如果应用的性能是首要关注事项,特别是对于密集图形,则 C++ 可能是正确选择。For example, if your app's performance is a primary concern, especially for intensive graphics, then C++ might be the right choice. Microsoft .NET 版本的 Visual Basic 非常适合 Visual Basic 应用的开发人员。The Microsoft .NET version of Visual Basic is great for Visual Basic app developers. 使用 HTML5 的 JavaScript 非常适合具有 Web 开发背景的开发人员。JavaScript with HTML5 is great for those coming from a web development background. 有关更多信息,请参见下列内容之一:For more info, see one of the following:

注意   对于使用三维图形的应用程序,OpenGL 和 OpenGL ES 标准并不本机可用于 UWP 应用。Note  For apps that use 3D graphics, the OpenGL and OpenGL ES standards are not natively available for UWP apps. 如果你不愿意将 OpenGL ES 代码重新写入到 Microsoft DirectX,你可能会有兴趣了解 “角度”If you would rather not rewrite your OpenGL ES code into Microsoft DirectX, you may be interested to know about Angle. 角度是一个持续项目,旨在将 OpenGL API 调用转换为 DirectX API 调用,以将 OpenGL 转换为 DirectX。Angle is an on-going project designed to convert OpenGL to DirectX by translating OpenGL API calls into DirectX API calls. 若要了解更多信息,请参阅下列文章:To learn more, see the following:

尝试 C#Giving C# a go

作为 iOS 开发人员,你已习惯使用 Objective-C 和 Swift。As an iOS developer, you're accustomed to Objective-C and Swift. C# 是与 Objective-C 和 Swift 最相似的 Microsoft 编程语言。The closest Microsoft programming language to both is C#. 对于大多数开发人员和大多数应用而言,我们认为 C# 是可供学习和使用的最容易、最快速的语言,因此本文的信息和演练将以该语言为主。For most developers and most apps, we think C# is the easiest and fastest language to learn and use, so this article's info and walkthroughs focus on that language. 若要了解有关 C# 的详细信息,请参阅以下内容:To learn more about C#, see the following:

下面是一个使用 Objective-C 和 C# 编写的类。Following is a class written in Objective-C and C#. 首先显示 Objective-C 版本,然后是 C# 版本。The Objective-C version is shown first, followed by the C# version.

// Objective-C header: SampleClass.h.

#import <Foundation/Foundation.h>

@interface SampleClass : NSObject {
    BOOL localVariable;
}

@property (nonatomic) BOOL localVariable;

-(int) addThis: (int) firstNumber andThis: (int) secondNumber;

@end
// Objective-C implementation.

#import "SampleClass.h"

@implementation SampleClass

@synthesize localVariable = _localVariable;

- (id)init {
    self = [super init];
    if (self) {
        localVariable = true;
    }
    return self;
}

-(int) addThis: (int) firstNumber andThis: (int) secondNumber {
    return firstNumber + secondNumber;
}

@end
// Objective-C usage.

SampleClass *mySampleClass = [[SampleClass alloc] init];
mySampleClass.localVariable = false;
int result = [mySampleClass addThis:1 andThis:2];

现在来看一下 C# 版本。Now, for the C# version. 就像 Swift,你将看到标头和实现不在单独的文件中。You'll see that like Swift, the header and the implementation are not in separate files.

// C# header and implementation.

using System;

namespace MyApp  // Defines this code' s scope.
{
    class SampleClass
    {
        private bool localVariable;

        public SampleClass() // Constructor.
        {
            localVariable = true;
        }

        public bool myLocalVariable // Property.
        {
            get
            {
                return localVariable;
            }
            set
            {
                localVariable = value; 
            }
        }

        public int AddTwoNumbers(int numberOne, int numberTwo)
        {
            return numberOne + numberTwo;
        }        
    }
}
// C# usage.

SampleClass mySampleClass = new SampleClass();
mySampleClass.myLocalVariable = false;
int result = mySampleClass.AddTwoNumbers(1, 2);

C# 是一种简单易学的语言,并附带构成 .NET 的许多支持类和框架。C# is an easy language to pick up, and comes with the many support classes and frameworks that make up .NET. 在任何时间,你都将愉快地编写代码,并且看不到方括号。In no time, you'll be happily writing your code without a square bracket in sight!

后续步骤Next step

入门:熟悉 Visual Studio 环境Getting started: Getting around in Visual Studio