[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部分!!

相关文章
相关标签/搜索