有人能告诉我为什么绑定在这里不起作用吗?我试图将这个字符串CountDisplay绑定到标签文本,但它说没有数据上下文,我发现这是xaml代码:
<?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="PapucoMetar.Views.Skener"
BackgroundColor="White">
<Grid RowDefinitions="*,Auto,Auto,*">
<Label
Grid.Row="1"
FontSize="Large"
HorizontalOptions="Center"
Text="{Binding CountDisplay}"/>
<Button
x:Name="ButtonCount"
Grid.Row="2"
Text="Click me"
Clicked="ButtonClicked_Clicked"
/>
</Grid>
</ContentPage>
下面是代码:
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PapucoMetar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Skener : ContentPage
{
public Skener()
{
InitializeComponent();
BindingContext = this;
}
int count = 0;
string countDisplay = "Click me";
public string CountDisplay
{
get => countDisplay;
set
{
if (value == countDisplay)
return;
countDisplay = value;
OnPropertyChanged();
}
}
private void ButtonClicked_Clicked(object sender, EventArgs e)
{
count++;
CountDisplay = $"You clicked button {count} time(s)";
}
}
}`
我试图绑定按钮的点击次数,但由于某种原因,它不能识别字符串计数显示。
1条答案
按热度按时间icnyk63a1#
您需要向XAML添加一个
DataType
,以便它知道验证绑定属性所针对的类型由于您使用的是代码隐藏而不是虚拟机,因此请将页面类指定为
DataType