1
4

More than 3 years have passed since last update.

眺めて覚える C# Xamarin Hello World(1)

Last updated at Posted at 2020-04-09

iPhoneの電池が持たなくなった。携帯電話の寿命は、大体3年ぐらいだ。

はたして5万円以上の値打ちがあるのかが疑問である。

そこでAndroidにすることにした。

image.png

さくさく動くし、電池も3日もつ!

値段も5万円から2万円適正価格だ!

前から気になっていたXamarinを使ってC#でプログラムしてみた。

1.Visual studio 2019のインストールについては、色々な記事があるので参考にしてほしい。
私の買ったディバイスは、UMIDIGI Xだ。まず、メーカサイトからディバイスドライバーをダウンロードしてWindows 10にinstall しよう。
image.png

ドライブが見えるようになる。
image.png

開発者モードにするには、「設定」アプリ内で「ビルド番号」を7回連続してタップ

image.png

image.png

Visual Studio 2019でプロジェクトを作成する。

image.png
Xamarin Formsを選択する。
Windows APPを作成するような感覚で開発できる。
image.png
空白のプロジェクトを作成する。
image.png

USBにより接続されたAndroidが表示されているか、確認する。
image.png
とりあえず下記のように表示されればOK
image.png
プログラムの内容を見てみよう
image.png
MainPage.xamlをダブルクリックするとソースが現れる。
XAMLはXMLをベースとしたマークアップ言語です。UIを定義するのに用いられます。

MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="HelloWorld.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
    </StackLayout>

</ContentPage>

さてプログラムでHello Worldを表示してみよう。
image.png

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace HelloWorld
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            var layout = new StackLayout();
            var lb = new Label() { Text = "Hello World",FontSize=40 };
            layout.Children.Add(lb);
            this.Content = layout;

        }
    }
}

Xamlでうにゃうにゃ書いていたレアウトをプログラムで書くと上記のようになる。

多分、javaで書くよりすっきりしていて良い。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4