Subview Button does not react after touching it

LarkaLarka DEMember ✭✭

Hello
This is my first iOS and Xamarin project.
I have a View with a button on the top. After touching this refresh button the BLE should scan for new devices. All found devices should add to a subview. Each found device has a button. After touching the button the app should connect to the device. At the moment it is enough to show a alert after catching the touch event.
Nearly everything is working. I found the new device. I can display it. But when I touch the device no reaction. When I touch the old button for scanning I get a reaction.

My View:
` partial void Refresh_TouchUpInside(UIButton sender)
{
//containerOne.RemoveFromSuperview();
var firstChild = new LinearChildController();
AddChildViewController(firstChild);
containerOne.AddSubview (firstChild.View);
//if (firstChild != null)
{
containerOne.Frame = new RectangleF((float)containerOne.Frame.X, (float)containerOne.Frame.Y, (float)containerOne.Frame.Width,
(float)firstChild.View.Frame.Height);

        }
        firstChild.View.Frame = containerOne.Bounds;
        firstChild.DidMoveToParentViewController (this);


    }

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

        containerOne = new UIView(new RectangleF(50, 125, ((float)UIScreen.MainScreen.Bounds.Height)- 50, 
        (float)UIScreen.MainScreen.Bounds.Height));
        containerOne.TranslatesAutoresizingMaskIntoConstraints = false;
        containerOne.AutosizesSubviews = true;
        containerOne.BackgroundColor = UIColor.Red;
        View.AddSubview (containerOne);

    }

`

My Subview:
`public override async void LoadView()
{
base.LoadView();

        //this.device = new ObservableCollection<ScanResultViewModel>();

        //View.BackgroundColor = UIColor;

        var ble = CrossBluetoothLE.Current;
        var adapter = CrossBluetoothLE.Current.Adapter;
        adapter.ScanTimeout = 1000;

        var bledevice = new Devices();
        star.Clear();
        ShowDetails();
        adapter.DeviceDiscovered += (s, a) =>
        {
            try
            {
                star.Add(a.Device.Name, new KeyValuePair<int, Guid>(a.Device.Rssi, a.Device.Id));
                            ShowDetails();
            }
            catch (ArgumentException)
            {
                Console.WriteLine("An element with the same key already exists.");
            }
        };
        await adapter.StartScanningForDevicesAsync();
                ShowDetails();
    }


    public void ShowDetails()
    {
        LinearLayout ll = new LinearLayout(View);
        var rect = new RectangleF(0, 0, 250, 20);
        _btn = new UIButton(UIButtonType.System);
        _btn.Frame = new RectangleF(0, 0, 250, 20);

    InvokeOnMainThread(() =>
        {
            foreach (var kvp in star)
            {
                UIImageView smallstar = new UIImageView();
                UIImage smallStarImage = UIImage.FromFile("Smallstar.png");
                smallstar.Image = smallStarImage;
                smallstar.Frame = new RectangleF(0, 0, 20, 20);

                _btn = new UIButton();
                _btn.Frame = View.Bounds;
                _btn.SetTitle ("", UIControlState.Normal);
                _btn.Frame = new RectangleF(0, 0, 20, 20);

                string i = CheckRSSI(kvp.Value.Key);
                UIImageView imageView = new UIImageView();
                UIImage image = UIImage.FromFile(i);
                imageView.Image = image;
                imageView.Frame = new RectangleF(0, 0, 20, 20);


                ll.Add(new LinearLayout.LinearLayoutItem(smallstar));
                ll.Add(new LinearLayout.LinearLayoutItem(new UILabel(rect) { Text = kvp.Key, TextColor = UIColor.White }));
                ll.Add(new LinearLayout.LinearLayoutItem(imageView));
                ll.Add(new LinearLayout.LinearLayoutItem(_btn));

            }
        ll.Build();
         });
    }

    public string CheckRSSI(int Rssi)
    {
        if (Rssi >= -69)
            return "FullRangeWhite.png";
        if (Rssi <= -70 && Rssi > -80)
            return "23RangeWhite.png";
        else
            return "13RangeWhite.png";
    }


    public override void ViewDidLoad()
    {
        _btn.AllTouchEvents += (object sender, EventArgs e) => {
            var alert = new UIAlertView("Clicked", "me", null, "OK");
            alert.Show();
        };

        _btn.TouchUpInside += (object sender, EventArgs e) => {
            var alert = new UIAlertView("Clicked", "me", null, "OK");
            alert.Show();
        };

    }

`

I know that my alert is deprecated. It is just for testing.
But when I touch my device nothing happens. I really don´t know why

Tagged:
Sign In or Register to comment.