【问题标题】:Dynamically created Xamarin iOS UI buttons not clickable动态创建的 Xamarin iOS UI 按钮不可点击
【发布时间】:2017-02-07 22:00:26
【问题描述】:

我有一个从 nib 文件扩展的 Tag 类:

using Foundation;
using System;
using UIKit;
using ObjCRuntime;

namespace TagTest
{
public partial class Tag : UIView
{

    public Tag (IntPtr handle) : base (handle)
    {
    }

    public static Tag Create()
    {

        var arr = NSBundle.MainBundle.LoadNib("Tag", null, null);
        var v = Runtime.GetNSObject<Tag>(arr.ValueAt(0));

        return v;
    }

    public override void AwakeFromNib()
    {
    }

    public UIButton Hashtag
    {
        get
        {
            return HashtagBtn;
        }
    }

    public UILabel HashtagCount
    {
        get
        {
            return HashtagCountLbl;
        }
    }
}

}

被以下视图模型使用

using System.Collections.Generic;
using MvvmCross.Binding.BindingContext;
using MvvmCross.iOS.Views;
using TagTest.Core.ViewModels;
using UIKit;
using Cirrious.FluentLayouts.Touch;
using System;
using System.Drawing;

namespace TagTest
{
public partial class SearchView : MvxViewController
{

    List<Tag> _tags;

    public SearchView () : base ("SearchView", null)
    {
    }

    new SearchViewModel ViewModel
    {
        get
        {
            return (SearchViewModel)base.ViewModel;
        }
        set
        {
            base.ViewModel = value;
        }
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        DisplayTags();

        View.UserInteractionEnabled = false;

        View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
        View.TranslatesAutoresizingMaskIntoConstraints = false;
        EdgesForExtendedLayout = UIRectEdge.None;
        ExtendedLayoutIncludesOpaqueBars = false;
        AutomaticallyAdjustsScrollViewInsets = false;

        View.ClipsToBounds = true;
        View.BackgroundColor = UIColor.Red;

        CreateBindings ();
    }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);

        ViewModel.Init();
    }

    void CreateBindings()
    {
        var set = this.CreateBindingSet<SearchView, SearchViewModel> ();
        set.Bind (this).For(x => x.Title).To (vm => vm.Title);
        set.Apply ();
    }

    public override void ViewDidLayoutSubviews()
    {
        base.ViewDidLayoutSubviews();

        View.LayoutIfNeeded();
    }

    void DisplayTags()
    {
        _tags = new List<Tag>();

        if (ViewModel.Tags != null)
        {
            foreach (var item in ViewModel.Tags)
            {
                _tags.Add(Tag.Create());
            }

            UIView lastTemplateAdded = View;

            for (int i = 0; i < _tags.Count; i++)
            {
                var tag = _tags[i];

                tag.TranslatesAutoresizingMaskIntoConstraints = false;
                tag.Hashtag.SetTitle(ViewModel.Tags[i].Tagname, UIControlState.Normal);
                tag.HashtagCount.Text = ViewModel.Tags[i].Count.ToString();
                tag.Frame = new RectangleF(100f, 300f, 100f, 50f);
                tag.Hashtag.Enabled = true;
                tag.Hashtag.UserInteractionEnabled = true;
                tag.UserInteractionEnabled = true;

                tag.Hashtag.TouchUpInside += (sender, e) =>
                {
                    ViewModel.TagSelectedCommand.Execute(tag);
                };

                View.AddSubview(tag);

                if (i == 0)
                {
                    View.AddConstraints(
                    tag.AtTopOf(View),
                    tag.AtLeftOf(View),
                    tag.Height().EqualTo(20)
                    );
                }
                else
                {
                    View.AddConstraints(
                    tag.Below(lastTemplateAdded, 20),
                    tag.AtLeftOf(View),
                        tag.Height().EqualTo(20)
                    );
                }

                lastTemplateAdded = tag;
            }
        }
    }

    public override void DidReceiveMemoryWarning ()
    {
        base.DidReceiveMemoryWarning ();
        // Release any cached data, images, etc that aren't in use.
    }
}

}

但是 Hashtag 按钮不可点击,TouchupInside 似乎没有被触发。如果我在视图中添加一个按钮,它是可点击的。可能出了什么问题?

【问题讨论】:

    标签: ios xamarin mvvmcross


    【解决方案1】:

    发现问题。这是设置 View.UserInteractionEnabled = false; 的组合。并使用流畅的约束。我现在将 UserInteractionEnabled 设置为 true 并删除了流畅的约束。现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2019-07-29
      • 1970-01-01
      • 2018-01-05
      相关资源
      最近更新 更多