我有一个标签和两个条目。我想让标签显示所选条目。现在选择第二个条目不会更改标签。罪魁祸首是什么?
这是我的代码。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MyProject"
x:Class="MyProject.TestPage"
x:DataType="vm:TestViewModel"
Title="TestPage">
<VerticalStackLayout>
<Entry x:Name="Entry1" Text="Entry 1">
<Entry.GestureRecognizers>
<TapGestureRecognizer Command="{Binding SetSelectedEntryCommand}"
CommandParameter="{Reference Entry1}" />
</Entry.GestureRecognizers>
</Entry>
<Entry x:Name="Entry2"
Text="Entry 2">
<Entry.GestureRecognizers>
<TapGestureRecognizer Command="{Binding SetSelectedEntryCommand}"
CommandParameter="{Reference Entry2}" />
</Entry.GestureRecognizers>
</Entry>
<Label Text="{Binding SelectedEntry.Text}" />
</VerticalStackLayout>
</ContentPage>
public partial class TestPage : ContentPage
{
public TestPage(TestViewModel model)
{
InitializeComponent();
BindingContext = model;
model.InitializeCommand.Execute(Entry1);
}
}
这很烦人:看起来你的帖子大部分都是代码;请添加更多详细信息。
public partial class TestViewModel : ObservableObject
{
[ObservableProperty]
Entry _selectedEntry;
[RelayCommand]
void Initialize(Entry selectedEntry)
{
SelectedEntry = selectedEntry;
SelectedEntry.BackgroundColor = Colors.Pink;
}
[RelayCommand]
void SetSelectedEntry(Entry selectedEntry)
{
SelectedEntry.BackgroundColor = Colors.White;
SelectedEntry = selectedEntry;
SelectedEntry.BackgroundColor = Colors.Pink;
}
}
编辑
显然它在 Android 上可以运行,但在 Windows 上却不行。因为我需要在 Windows 上使用触摸屏?