Problem with Button Clicked. BLE.Plugin

Hi! I have this following code. The problem is that when I put the code outside the ButtonScan method, it works. If I put it inside the method, it doesn't work because it doesn'tt let me click on the button. I'm working with BLE.Plugin.

MainPage.xaml

<StackLayout Padding="10,10,10,10" BackgroundColor="White">
  <Image TranslationY="20" Source="Image.png"/>
  <Button x:Name="btn_scan_device" BorderColor="Black" Text="Scan" TextColor="Green" TranslationY="150" WidthRequest="60"   HeightRequest="50" Clicked="ButtonScan"/>
  <StackLayout x:Name="sl_deviceList">
    <ListView x:Name="lv_deviceList">
      <Label x:Name="lbl_deviceList" />
    </ListView>
  </StackLayout>
</StackLayout>

MainPage.xaml.cs (This Works)

public partial class MainPage : ContentPage
{
    public IDevice device;

    public MainPage()
    {
        InitializeComponent();

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

        ble.StateChanged += (s, c) =>
        {
            Debug.WriteLine($"The bluetooth state changed to {c.NewState}");
        };

        adapter.DeviceDiscovered += (s, a) =>
        {
            Debug.WriteLine($"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NEW DEVICE FONDUURUUUUUUUU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! {a.Device.Name}");
        };

        adapter.StartScanningForDevicesAsync();

    }

    public void ButtonScan(object sender, EventArgs e)
    {

    }
}

(This NOT Works)

public partial class MainPage : ContentPage
{
    public IDevice device;

    public MainPage()
    {
        InitializeComponent();

    }

    public void ButtonScan(object sender, EventArgs e)
    {
        var ble = CrossBluetoothLE.Current;
        var adapter = CrossBluetoothLE.Current.Adapter;

        ble.StateChanged += (s, c) =>
        {
            Debug.WriteLine($"The bluetooth state changed to {c.NewState}");
        };

        adapter.DeviceDiscovered += (s, a) =>
        {
            Debug.WriteLine($"!!!!!!!!!!!!!!!!!!!!!!!!!!NEW DEVICE FONDUURUUUUUUUU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! {a.Device.Name}");
        };

        adapter.StartScanningForDevicesAsync();
    }
}

Best Answer

Answers

  • Hi @JohnMiller
    I did what you told me and now it works. What I was trying with the TranslationY was to center the button in the middle of the screen. The same with the image, separate it from the top. How can this affect the operation of a button?
    Thank you very much!

Sign In or Register to comment.