如何在HoloLens中创建一个2D的Hello World程序

注:本文提及到的代码示例下载地址 > How to build an "Hello World" 2D app in HololLens.

HoloLens 是微软的一款MR Glass(混合现实全息眼镜), 国内某非著名SQ主播出了个片子介绍它,有兴趣的点这里:http://v.youku.com/v_show/id_XMTcxNjIzNzQ2MA==.html?from=y1.7-1.2

HoloLens中可用的App大致分为两种类型:

开发的前期准备:

  • 你的VS版本必须是VS 2015或以上(推荐 vs 2015 update 3及以上),不然没有Win10 UWP开发套件的。下载地址点这里:https://www.visualstudio.com/vs-2015-product-editions
  • 必须安装Win 10 UWP 开发套件。在安装VS的时候把安装 UWP这块的勾选上就可以了。
  • 你还需要一个酷炫到没朋友的HoloLens模拟器→_→。

一切准备妥当,让我们来开始创建一个HoloLens的2D Hello World程序:

  1.首先我们来先造一个筏子→_→,打开VS 并创建一个UWP app。

    

  2.打开MainPage.xaml,贴入如下代码:

 1 <Page
 2     x:Class="HololensApp.MainPage"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:local="using:HololensApp"
 6     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     mc:Ignorable="d">
 9 
10     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
11         <StackPanel>
12             <StackPanel Orientation="Horizontal">
13                 <TextBlock Text="Click count:" />
14                 <TextBlock Text="{Binding ClickCount}" />
15             </StackPanel>
16             <Button Click="Button_Click">Click Me</Button>
17         </StackPanel>
18     </Grid>
19 </Page>

  在CS代码中贴入如下代码:

public sealed partial class MainPage : Page
{
    public MainPageViewModel ViewModel = new MainPageViewModel();

    public MainPage()
    {
        this.InitializeComponent();

        this.DataContext = ViewModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ViewModel.ClickCount++;
    }
}

public class MainPageViewModel : BindableBase
{
    private int _clickCount;
    public int ClickCount
    {
        get { return _clickCount; }
        set { SetProperty(ref _clickCount, value); }
    }
}


[Windows.Foundation.Metadata.WebHostHidden]
public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

  修复掉引用错误:在划红线的位置点击,出现小灯泡后选择小灯泡里的选项即可。

  

  3.然后选择调试环境(这里你需要安装HoloLens 模拟器)

    

  3.然后按F5键,开始跑程序。

    这时模拟器就会启动并且部署你的app到模拟器,模拟器比较慢,耐心等待一段时间,

     

   当程序运行起来后,你可以看到如下界面:

    

    在app窗体上右键向上拖动以固定改App,此时app才算是真正运行起来:

     使用W,A,S,D和上下左右键移动中间的圆点到按钮上并按空格键,可以发现上面的数字显示你点击的次数:

    

到此,该演示就结束了,是不是有种受骗上当的感觉?TMD这不就是UWP吗? 

是的,其实他就是UWP,只是阉割版而已,重说三:阉割版,阉割版,阉割版,也就是有些UWP的特性在HoloLens现阶段中是不支持的,这一点才是真正需要你注意的地方。详细的列表请查阅微软官方文档:Current limitations for apps using APIs from the shell

HoloLens 3D入门教程请戳http://www.cnblogs.com/onecodeonescript/p/5925906.html

原文地址:https://www.cnblogs.com/onecodeonescript/p/5885702.html