代码之家  ›  专栏  ›  技术社区  ›  I. Mendoza

如何在XAML页面中执行SetBinding和BindingContext?

  •  0
  • I. Mendoza  · 技术社区  · 6 年前

    我目前正在尝试开发一个基于Xamarin表单选项卡页面的应用程序。起初,应用程序只有一个用C#编写的内容页。我现在要做的是将这个现有内容页实现到选项卡式页面的一个选项卡中。

    我正在用XAML编写选项卡式页面,我不知道如何用这种语言执行“SetBinding”或“BindingContext”。

    以下是我想从C#转换到XAML的内容:

        public MyPage()
        {
    
            this.BindingContext = new MyPageViewModel();
    
            Picker pickerBluetoothDevices = new Picker() { Title = "Select a bth device" };
            pickerBluetoothDevices.SetBinding(Picker.ItemsSourceProperty, "ListOfDevices");
            pickerBluetoothDevices.SetBinding(Picker.SelectedItemProperty, "SelectedBthDevice");
            pickerBluetoothDevices.SetBinding(VisualElement.IsEnabledProperty, "IsPickerEnabled");
    
            Entry entrySleepTime = new Entry() {Keyboard = Keyboard.Numeric, Placeholder = "Sleep time" };
            entrySleepTime.SetBinding(Entry.TextProperty, "SleepTime");
    
            Button buttonConnect = new Button() { Text = "Connect" };
            buttonConnect.SetBinding(Button.CommandProperty, "ConnectCommand");
            buttonConnect.SetBinding(VisualElement.IsEnabledProperty, "IsConnectEnabled");
    
            Button buttonDisconnect = new Button() { Text = "Disconnect" };
            buttonDisconnect.SetBinding(Button.CommandProperty, "DisconnectCommand");
            buttonDisconnect.SetBinding(VisualElement.IsEnabledProperty, "IsDisconnectEnabled");
    
            Button buttonSend = new Button() { Text = "Send" };
            buttonSend.SetBinding(Button.CommandProperty, "SendCommand");
            buttonSend.SetBinding(VisualElement.IsEnabledProperty, "IsSendEnabled");
    
            StackLayout slButtons = new StackLayout() {Orientation = StackOrientation.Horizontal, Children = {buttonDisconnect, buttonConnect, buttonSend } };
    
            ListView lv = new ListView();
            lv.SetBinding(ListView.ItemsSourceProperty, "ListOfBarcodes");
            lv.ItemTemplate = new DataTemplate(typeof(TextCell));
            lv.ItemTemplate.SetBinding(TextCell.TextProperty, ".");
    
            int topPadding = 0;
            if (Device.RuntimePlatform == Device.iOS)
                topPadding = 20;
    
            StackLayout sl = new StackLayout { Children = { pickerBluetoothDevices, entrySleepTime, slButtons, lv }, Padding = new Thickness(0,topPadding,0,0) };
            Content = sl;
        }
    

    在XAML中,这段C代码的等效代码是什么?

    我是这样开始的:

    <?xml version="1.0" encoding="utf-8" ?>
    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="TestBth.MyTabbedPage">
      <!--Pages can be added as references or inline-->
      <ContentPage Title="Connect">
    
    
    
            <ContentPage.Content>
                <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
    
                    <Picker x:Name="pickerBluetoothDevices" Title="Select a bth device"/>
    
                    <Button x:Name="buttonConnect" Text="Connect"/>
                    <Button x:Name="buttonDisconnect" Text="Disconnect"/>
                    <Button x:Name="buttonSend" Text="Send"/>
    
                    <ListView x:Name="lv"/>
    
                </StackLayout>
    
            </ContentPage.Content>
    
      </ContentPage>
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Bikash    6 年前

    只要将c#binding属性更改为Xaml属性,就可以纠正错误。

    喜欢 ItemSourceProperty 在c中,xaml绑定是 ItemSource

    <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
    
                <Picker x:Name="pickerBluetoothDevices" ItemsSource="{Binding ListOfDevices}" Title="Select a bth device" SelectedItem="{Binding SelectedBthDevice}" IsEnable="{Binding IsPickerEnabled}"/>
    
                <Button x:Name="buttonConnect" Text="Connect" Command="{Binding ConnectCommand}"/>
                <Button x:Name="buttonDisconnect" Text="Disconnect" Command="{Binding DisconnectCommand}"/>
                <Button x:Name="buttonSend" Text="Send" Command="{Binding SendCommand}"/>
    
                <ListView x:Name="lv" ItemSource="{Binding ListOfBarcodes}"/>
    
            </StackLayout>
    
        2
  •  0
  •   Michał Å»ołnieruk    6 年前

    您为什么要在代码中创建绑定?您还可以在代码中进行绑定,例如,要将命令绑定到按钮,您可以使用:

    <Button Command="{Binding ButtonCommand}"/>
    

    在视图的构造函数中,需要设置上下文:

     public View()
      {
            InitializeComponent();
            this.BindingContext = this; // or instance of your ViewModel
      }
    

    此外,还需要在BindingContext实例中定义命令:

    public ICommand ButtonCommand { get; set; }