C# 如何在XAML页面中执行SetBinding和BindingContext?
我目前正在尝试开发一个基于Xamarin表单选项卡页面的应用程序。起初,应用程序只有一个用C#编写的内容页。我现在要做的是将这个现有的内容页面实现到选项卡式页面的一个选项卡中 我正在用XAML编写选项卡式页面,不知道如何用这种语言执行“SetBinding”或“BindingContext” 以下是我想将C#转换为XAML的内容: 在XAML中,这个C#代码的等价物是什么 我是这样开始的:C# 如何在XAML页面中执行SetBinding和BindingContext?,c#,xaml,xamarin,binding,tabbedpage,C#,Xaml,Xamarin,Binding,Tabbedpage,我目前正在尝试开发一个基于Xamarin表单选项卡页面的应用程序。起初,应用程序只有一个用C#编写的内容页。我现在要做的是将这个现有的内容页面实现到选项卡式页面的一个选项卡中 我正在用XAML编写选项卡式页面,不知道如何用这种语言执行“SetBinding”或“BindingContext” 以下是我想将C#转换为XAML的内容: 在XAML中,这个C#代码的等价物是什么 我是这样开始的: <?xml version="1.0" encoding="utf-8" ?> <Tab
<?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>
为什么要在代码中创建绑定?您还可以在代码中进行绑定,例如,要将命令绑定到按钮,请使用:
<Button Command="{Binding ButtonCommand}"/>
您还需要在BindingContext实例中定义命令:
public ICommand ButtonCommand { get; set; }
您为什么要在代码中创建绑定?您还可以在代码中进行绑定,例如,要将命令绑定到按钮,请使用:
<Button Command="{Binding ButtonCommand}"/>
您还需要在BindingContext实例中定义命令:
public ICommand ButtonCommand { get; set; }
只要将c#binding属性更改为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>
只要将c#binding属性更改为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>
我不知道需要在那里设置上下文。那真的很有用。非常感谢您的支持!当然如果您愿意,也可以在xaml内部进行:我不知道需要在那里设置上下文。那真的很有用。非常感谢您的支持!当然如果您愿意,也可以在xaml中执行此操作: