xamarin file.xaml.cs和文件.xaml之间的绑定不起作用

dojqjjoe  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在尝试将数据从.xaml.cs绑定到.xaml,但它无法识别应用程序中绑定的变量。我应该如何正确使用DataContext以便建立此绑定连接?我是xamarin的初学者,不知道如何进行此操作...因此任何帮助都很有用

.xaml格式

using System;
    using System.Windows;
    
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using Plugin.BLE;
    using Plugin.BLE.Abstractions.Contracts;
    using Plugin.BLE.Abstractions.Exceptions;
    using Xamarin.Forms;
    
    namespace BT
    {
        public partial class MainPage : ContentPage
        {
            
            #region MAINPAGE CONSTRUCTOR
            public MainPage()
            {
                InitializeComponent();
                initializeBluetooh();
    
            }
            #endregion
        
        
            #region BT constructor
            
            #endregion
            #region BT VARIABLES
            // bluetooth
            IBluetoothLE _handler;
            IAdapter _adapter;
    
    
            ObservableCollection<IDevice> _devicelist;
            ObservableCollection<IDevice> _devicelistAux;
            IDevice _connectedDevice;
    
            String _deviceName;
            String _bt_state;
    
            IReadOnlyList<IService> _services;
            Task<IReadOnlyList<ICharacteristic>> _characteristics;
            ICharacteristic _characteristic;
            IService _service;
    
            String _stringBuff;
    
    
    
    
            private Guid guid;
    
            IDevice deviceConn;
    
            #endregion
            #region BT OBJECTS
            public ObservableCollection<IDevice> devicelist
            {
                get { return _devicelist; }
                set { _devicelist = value; }
            }
    
            public IDevice connectedDevice
            {
                get { return _connectedDevice; }
                set { _connectedDevice = value; }
            }
            public IBluetoothLE handler
            {
                get { return _handler; }
                set { _handler = value; }
            }
            public IAdapter adapter
            {
                get { return _adapter; }
                set { _adapter = value; }
            }
    
            public IReadOnlyList<IService> services
            {
                get { return _services; }
                set { _services = value; }
            }
            public Task<IReadOnlyList<ICharacteristic>> characteristics
            {
                get { return _characteristics; }
                set { _characteristics = value; }
            }
            public IService service
            {
                get { return _service; }
                set { _service = value; }
            }
            public ICharacteristic characteristic
            {
                get { return _characteristic; }
                set { _characteristic = value; }
            }
            public String bt_state
            {
                get { return _bt_state; }
                set { _bt_state = value; }
            }
    
    
    
    
            #endregion
            #region BT FUNCTIONS
            public async void initializeBluetooh()
            {
                handler = CrossBluetoothLE.Current;
                adapter = CrossBluetoothLE.Current.Adapter;
                adapter.ScanMode = ScanMode.LowPower;
    
                adapter.ScanTimeout = 5000;
    
                devicelist = new ObservableCollection<IDevice>();
    
    
                bt_state = "BLUETOOTH INFO:\n";
                Console.WriteLine(bt_state);
                    
    
            }
            public async void InitScanDevices(object sender, EventArgs e)
            {
    
    
                bt_state = "BLUETOOTH INFO:\n" +
                    "\nBluetooth state: Scanning...";
                Console.WriteLine(bt_state);
    
    
                devicelist.Clear();
                adapter.DeviceDiscovered += (s, args) =>
                {
    
                   IDevice dispositivoDescubierto = args.Device;
                   devicelist.Add(dispositivoDescubierto);
                   Console.WriteLine(dispositivoDescubierto.Name);
    
                 };
                if (!handler.Adapter.IsScanning)
                {
                    await adapter.StartScanningForDevicesAsync();
    
                    bt_state = "BLUETOOTH INFO:\n" +
                    "\nBluetooth state: Stopped Scanning";
                    Console.WriteLine(bt_state);
    
                }
            }
            public async void OnCollectionViewSelectionChanged(object sender, SelectedItemChangedEventArgs e)
            {
                if (adapter.IsScanning)
                {
                    await adapter.StopScanningForDevicesAsync();
                }
                var selectedDevice = (IDevice)e.SelectedItem;
    
                Console.WriteLine(selectedDevice.Name);
                try
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        adapter.ConnectToDeviceAsync(selectedDevice);
                        deviceConn = selectedDevice;
    
                    });
    
                }
                catch (DeviceConnectionException)
                {
                    //DisplayAlert("Error", "Could not connect to device", "Dismiss");
                }
    
    
            }
    
            #endregion
            #region BT COMMANDS
            
            #endregion
        }
    }

. xaml.cs文件系统

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BT.MainPage">

    <StackLayout>
        <Label
            Margin="0,50,0,0"/>
        <Frame OutlineColor="White">
             <Label
            Text="BLUETOOTH CONNECTION"
            TextColor="Coral"
            />

        </Frame>
        <Frame OutlineColor="DarkBlue">
            <Label Text="{Binding bt_state}"/>
        </Frame>
       <Button
            Clicked="InitScanDevices"
            Text="Scan for devices"/>
        <ListView
            SelectionMode="Single"
            ItemSelected="OnCollectionViewSelectionChanged"
            ItemsSource="{Binding devicelist}"
            BackgroundColor="Aqua">
        <ListView.ItemTemplate>
            <DataTemplate>
                   
                       <TextCell Text="{Binding Name}" TextColor="Black"/>
                       
            </DataTemplate>
        </ListView.ItemTemplate>
           </ListView>

    </StackLayout>

</ContentPage>

提前谢谢你劳尔。

ve7v8dk2

ve7v8dk21#

您必须分配一个BindingContext,以便页面知道从何处获取数据

public MainPage()
 {
     InitializeComponent();
     initializeBluetooh();

     BindingContext = this;
 }

相关问题