mk-toolブログ

エンジニアと家のことをごちゃごちゃと書いてます

【Xamarin】邪道だけど試してみる4

ボタンをクリックして他クラスの関数を起動させる方法。

これは、もう本当C#の話でここまでくれば後はどんどん作るのみかと!

 

TestClass

>|cs|

using System;

namespace MySecondTry
{
    public class TestClass
    {
        static int a = 0;

        public int Abc(){
            a = a * 2;
            return a;
        }
    }
}

||<

 

MySecondTry.cs

>|cs|

            //イベントを指定するためのクラスを初期化
            TestClass tc = new TestClass();

            var button = new Button {
                Text = "Click Me!",
                FontSize = 30,
                HorizontalOptions = LayoutOptions.Center,//中央に配置する(横方向)
                VerticalOptions = LayoutOptions.CenterAndExpand // 中央に配置する(縦方向)
            };
            // クリック時のイベント
            button.Clicked += (s, e) => {
                // ボタンの文字を変更する
                button.Text = tc.Abc().ToString();
            };
                
            // The root page of your application
            MainPage = new ContentPage {
                Content = new StackLayout {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            XAlign = TextAlignment.Center,
                            Text = "Welcome to Xamarin Forms!"
                        },
                        button
                    }
                }
            };

||<

 

よっしゃここまできたら!いける!