2017-02-07 73 views
0

我具有從筆尖文件膨脹的標籤類:動態創建Xamarin iOS的UI按鈕無法點擊

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似乎沒有被觸發。如果我向視圖添加一個按鈕,它是可點擊的。可能會出現什麼問題?

回答

0

發現問題。這是設置View.UserInteractionEnabled = false;並使用流暢的約束。我現在將UserInteractioEnabled設置爲true並刪除了流暢的約束。現在都在工作。