【问题标题】:How to perform SetBinding and BindingContext in a XAML page?如何在 XAML 页面中执行 SetBinding 和 BindingContext?
【发布时间】:2018-09-21 12:06:51
【问题描述】:

我目前正在尝试开发基于 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;
    }

这个 C# 代码在 XAML 中的等价物是什么?

我是这样开始的:

<?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>

【问题讨论】:

    标签: c# xaml xamarin binding tabbedpage


    【解决方案1】:

    你是正确的,只需将 c# 绑定属性更改为 Xaml 属性。

    就像c#中的ItemSourceProperty一样,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】:

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

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

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

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

      您还需要在 BindingContext 实例中定义您的命令:

      public ICommand ButtonCommand { get; set; }
      

      【讨论】:

      猜你喜欢
      • 2018-10-26
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多