Dotnet-core-create-uwp-app

提供:Dev Guides
移動先:案内検索

.NET Core-UWPアプリの作成

この章では、.NET Coreを使用してUWPアプリケーションを作成する方法について説明します。 UWPは、Windows 10 UWPアプリケーションとも呼ばれます。 このアプリケーションは、以前のバージョンのWindowsでは実行されませんが、将来のバージョンのWindowsでのみ実行されます。

以下は、UWPがスムーズに実行されるいくつかの例外です。

  • ローカルで実行する場合は、Windows 10が必要です。Windows8でも開発できます。その後、エミュレータで実行する必要がありますが、Windows 10を使用することをお勧めします。
  • UWPアプリケーションには、Windows 10 SDKも必要です。 Visual Studio 2015セットアップを開き、Visual Studioを変更します。
  • [機能の選択]ページで下にスクロールすると、ユニバーサルWindowsアプリ開発ツールが表示されます。以下に示すように、そのオプションをチェックします。

ここでは、SDKのさまざまなバージョンとツールの最新の更新も確認できます。[次へ]をクリックします。

Professional 2015

次に、[インストール]ボタンをクリックします。

インストールボタン

インストールが完了したら、システムを再起動する必要があります。

セットアップ完了

次の手順に従ってUWPを実装しましょう。

  • まず、Visual Studio 2015を起動します。
  • [ファイル]メニューをクリックし、[新規]→[プロジェクト]を選択します。 [新しいプロジェクト]ダイアログが表示されます。 ダイアログボックスの左ペインにさまざまなタイプのテンプレートが表示されます。

ファイルメニュー

  • 左側のペインにツリービューが表示されます。テンプレート→Visual C#→Windowsからユニバーサルテンプレートを選択してください。
  • 中央のペインから、Blank App(Universal Windows)テンプレートを選択します。
  • [名前]フィールドに UWPFirstApp と入力してプロジェクトに名前を付け、[OK]をクリックします。

UWPFirstApp

  • ターゲットバージョン/最小バージョンダイアログが表示されます。 このチュートリアルではデフォルト設定で問題ないため、[OK]を選択してプロジェクトを作成します。

デフォルト設定

  • ここでは、すべてのWindows 10デバイスをターゲットにできる単一のプロジェクトがあり、.NET CoreとUWPの両方がマルチターゲットの簡素化であることがわかります。
  • 新しいプロジェクトが開くと、そのファイルがソリューションエクスプローラーペインの右側に表示されます。 ファイルを表示するには、[プロパティ]タブではなく[ソリューションエクスプローラー]タブを選択する必要がある場合があります。
  • Blank App(Universal Window)は最小限のテンプレートですが、まだ多くのファイルが含まれています。 これらのファイルは、C#を使用するすべてのUWPアプリに不可欠です。 Visual Studioで作成するすべてのプロジェクトにはファイルが含まれています。
  • 実行例を確認するには、MainPage.XAMLを開いて次のコードを追加します。
<Page
   x:Class = "UWPFirstApp.MainPage"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local = "using:UWPFirstApp"
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable = "d">

   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <StackPanel HorizontalAlignment = "Center">
         <TextBlock Text = "Hello, world!"
            Margin = "20"
            Width = "200"
            HorizontalAlignment = "Left"/>
         <TextBlock Text = "Write your name."
            Margin = "20"
            Width = "200"
            HorizontalAlignment = "Left"/>
         <TextBox x:Name = "txtbox"
            Width = "280"
            Margin = "20"
            HorizontalAlignment = "Left"/>
         <Button x:Name = "button" Content = "Click Me"
            Margin = "20"
            Click = "button_Click"/>
         <TextBlock x:Name = "txtblock"
            HorizontalAlignment = "Left"
            Margin = "20"/>
      </StackPanel>
   </Grid>

</Page>

以下は、C#のボタンのクリックイベントです。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;
using Windows.Foundation.Collections;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//The Blank Page item template is documented at
//http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace UWPHellowWorld {
  ///<summary>
  ///An empty page that can be used on its own or navigated to within a Frame.
  ///</summary>
   public sealed partial class MainPage : Page {
      public MainPage() {
         this.InitializeComponent();
      }
      private void button_Click(object sender, RoutedEventArgs e) {
         if (txtbox.Text != "")
            txtblock.Text = "Hello: " + txtbox.Text;
         else
            txtblock.Text = "You have not write your name";
      }
   }
}

ローカルマシンで上記のコードを実行すると、次のウィンドウが表示されます。 テキストボックスに任意の名前を入力し、[クリック]ボタンを押します。

Click Me