[Xamarin] button及事件處理

在visual studio + Genymobile 的調試環境下,遇到連不上虛機的狀況,在Xamarin官方論壇上找到解決方案,以下:android

主要就是去android的project屬性調整幾個值c#

Couldn't connect to logcat, GetProcessId returned: 0app

Open your application properties Android Manifest -->Required Permission -->Enable ACCESS_COARSE_LOCATION and INTERNET(tick) Android Options --> Supported Architecture -->Enable armeabi and armeabi-v7aide

Button,昨天的都是一些顯示相關的基礎控件,button算是第一個交互控件,示例代碼以下,綁定事件便可用,C#碼農在VS下tab兩下自動就生成對應事件函數啦~函數

Button的properties並非每一個都支持全部平臺(iOS Android WP),須要參考下API doc性能

示例的結構:首先仍是新建一個繼承contentPage的類,最外層用的是StackLayout,直接賦值到content屬性上。StackLayout按順序放一個button和ScrolView,ScrolView再包含一個預先定義成field的StackLayout用來存放以後動態生成的Label,button的click事件裏實現動態添加一個Label到LoggerLayout裏ui

    class ButtonLoggerPage: ContentPage
    {
        StackLayout loggerLayout = new StackLayout();

        public ButtonLoggerPage()
        {
            //Create the Button and attach Clicked handler
            Button button = new Button
            {
                Text = "Log the Click Time"
            };
            button.Clicked += button_Clicked;

            this.Padding = new Thickness(5,Device.OnPlatform(20,0,0),5,0);

            //Assemble the page
            this.Content = new StackLayout
            {
                Children =
                {
                    button,
                    new ScrollView
                    {
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        Content = loggerLayout
                    }
                }
            };
        }

        void button_Clicked(object sender, EventArgs e)
        {
            //Add Laabel to scrollable StackLayout
            loggerLayout.Children.Add(new Label { 
                Text = "Button clicked at" + DateTime.Now.ToString("T")
            });
        }
    }

效果以下,其中button在點擊的時候,背景色會改變:this

walking the treespa

相似HTML 或 XAML,控件之間能夠經過visual tree查詢,上面例子的事件處理函數中,直接使用了loggerLayout變量,也能夠經過sender參數找過去,示例以下調試

            //another way to find loggerLatout
            Button button = (Button)sender;
            StackLayout outerLayout = (StackLayout)button.ParentView;
            //second one is scrollView
            ScrollView scrollView = (ScrollView)outerLayout.Children[1];
            StackLayout loggerLayout_2 = (StackLayout)scrollView.Content;

例子主要用來講明visual tree的使用,就示例自己來講,來回的強制轉換確定影響性能,同時若是改變了layout結構,相應的代碼也要調整,不過尚未去查API有沒有支持find by id之類的方式

Sharing button clicks

這個和C#裏大多數事件處理機制相同,把多個控件的click事件綁定(+=)到同一個處理函數便可

Anonymous event handlers

使用C#的Lambda特性實現匿名事件函數,示例:

button.Clicked += (sender,args) =>
    {
        //statement
    }

Distinguishing views with IDs

Element 基類定義了一個string類型的StyleId,用來設置用戶自定義的id,有點value/tag的意思吧

因此普通的button控件均可以設置這個值

    public abstract class Element : BindableObject, IElement, INameScope
    {
        //
        // Summary:
        //     Gets or sets a user defined value to uniquely identify the element.
        //
        // Remarks:
        //     Use the StyleId property to identify individual elements in your application
        //     for identification in ui testing and in theme engines.
        public string StyleId { get; set; }


明天進入 Infrastructure部分!!

相關文章
相關標籤/搜索