XAML 从ViewModel绑定标签文本不起作用

wfauudbj  于 2023-09-28  发布在  其他
关注(0)|答案(2)|浏览(172)

我现在正在学习.NET MAUI,我遇到了一个数据绑定的问题。我已经阅读了Microsoft文档中关于数据绑定的部分,但我仍然不知道为什么它不起作用。
我可以显示按钮和第二个标签。但是,一旦我将它绑定到视图模型上的属性,它就不会显示。如果我使用绑定。它显示MauiTestApp. ViewModel. MainViewModel。我可以从中收集到的是Data上下文是正确的,我应该能够访问属性的上下文。
以下是我的View Model:

  1. using MauiTestApp.Model;
  2. using System.Collections.ObjectModel;
  3. namespace MauiTestApp.ViewModel
  4. {
  5. public partial class MainViewModel : ObservableObject
  6. {
  7. public ObservableCollection<Monkey> Monkeys = new ObservableCollection<Monkey>();
  8. public String s = "Hello world";
  9. public MainViewModel()
  10. {
  11. Monkeys.Add(new Monkey("Bob", 20));
  12. Monkeys.Add(new Monkey("Steve", 10));
  13. Monkeys.Add(new Monkey("Joey", 14));
  14. }
  15. public void AddMonkey()
  16. {
  17. Monkeys.Add(new Monkey("Harry", 23));
  18. }
  19. }
  20. }

下面是XAML

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:MauiTestApp"
  5. xmlns:viewModel="clr-namespace:MauiTestApp.ViewModel"
  6. xmlns:model="clr-namespace:MauiTestApp.Model"
  7. x:Class="MauiTestApp.MainPage">
  8. <ContentPage.BindingContext>
  9. <viewModel:MainViewModel/>
  10. </ContentPage.BindingContext>
  11. <ScrollView>
  12. <Grid>
  13. <Grid.RowDefinitions>
  14. <RowDefinition Height="50"></RowDefinition>
  15. <RowDefinition Height="30"></RowDefinition>
  16. <RowDefinition Height="30"></RowDefinition>
  17. </Grid.RowDefinitions>
  18. <Button Grid.Row="0" Text="Press me" Pressed="Button_Pressed"></Button>
  19. <Label Grid.Row="1" Text="{Binding s}" TextColor="Purple" ></Label>
  20. <Label Grid.Row="2" Text="Hello world" TextColor="Purple"></Label>
  21. </ScrollView>
  22. </ContentPage>

下面的XAML代码:

  1. namespace MauiTestApp
  2. {
  3. public partial class MainPage : ContentPage
  4. {
  5. public MainViewModel viewModel;
  6. public MainPage(MainViewModel vm)
  7. {
  8. InitializeComponent();
  9. viewModel = vm;
  10. }
  11. private void Button_Pressed(object sender, EventArgs e)
  12. {
  13. viewModel.AddMonkey();
  14. }
  15. }
  16. }

如果这是一个基本的问题,我很抱歉,但我完全被困在这一点上。
谢谢你的帮助!!!

mlnl4t2r

mlnl4t2r1#

在标签中看不到任何文本的原因是因为它不是属性。您需要使用MVVM工具包来使您的生活更轻松。在这里阅读更多
所以ViewModel中的绑定应该如下所示

  1. [ObservableProperty]
  2. private string s = "Hello world";

不是说它是一个带有小s的私有字段。通过ObservableProperty,它被生成为一个公共的并使用大写字母S分配的适当Property。重要的是总是这样做。永远不要使用私有字段。总是财产。
现在有很多问题与您的代码一般,所以如果你不介意,我会给予你一个可能的解决方案,我可以从你的代码翻译。我希望能帮上忙。
首先,完整的视图模型看起来像这样。注意带有[RelayCommand]的方法。它将接受您的方法并将其转换为可绑定的命令。它还遵循命名约定。因此,它永远不会以命令结束。要绑定到它,我们在视图中添加Command。通过这样做,我们将视图模型从所有引用解耦到MUAI引用。

  1. public partial class MonkeyViewModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private ObservableCollection<Monkey> _monkeys;
  5. [ObservableProperty]
  6. private string s = "Hello world";
  7. public MonkeyViewModel()
  8. {
  9. Monkeys = new()
  10. {
  11. new Monkey
  12. {
  13. Name = "Bob",
  14. Location = 20
  15. },
  16. new Monkey
  17. {
  18. Name = "Steve",
  19. Location = 10
  20. },
  21. new Monkey
  22. {
  23. Name = "Joey",
  24. Location = 14
  25. },
  26. };
  27. }
  28. [RelayCommand]
  29. public void AddMonkey()
  30. {
  31. var harryMonkey = new Monkey
  32. {
  33. Name = "Harry",
  34. Location = 23
  35. };
  36. Monkeys.Add(harryMonkey);
  37. }
  38. }

如果我们看看你的观点。它可以看起来像这样,显示一个列表的钱和你的按钮。

  1. <VerticalStackLayout>
  2. <CollectionView EmptyView = "No data" ItemsSource="{Binding Monkeys}">
  3. <CollectionView.ItemTemplate>
  4. <DataTemplate x:DataType="models:Monkey">
  5. <Grid Padding = "10">
  6. <Grid.RowDefinitions>
  7. <RowDefinition Height = "*" />
  8. <RowDefinition Height = "*" />
  9. <RowDefinition Height = "Auto" />
  10. </Grid.RowDefinitions>
  11. <Label Grid.Row="0" Text="{Binding Name}" />
  12. <Label Grid.Row="1" Text= "{Binding Location}" />
  13. <BoxView
  14. Grid.Row= "2"
  15. BackgroundColor= "Gray"
  16. HeightRequest= "1" />
  17. </Grid>
  18. </DataTemplate>
  19. </CollectionView.ItemTemplate>
  20. </CollectionView>
  21. <Button
  22. Command= "{Binding AddMonkeyCommand}"
  23. Text= "Press me" />
  24. <Label
  25. Text= "{Binding S}"
  26. TextColor= "Purple" />
  27. </VerticalStackLayout>

代码落后。永远不要忘记绑定到BindingContext,无论是在这里还是像你一样,在xaml中。如果不是这样,你的观点将不会绑定到任何东西。

  1. public partial class MainPage : ContentPage
  2. {
  3. private readonly MonkeyViewModel _monkeyViewModel;
  4. public MainPage(MonkeyViewModel monkeyViewModel)
  5. {
  6. _monkeyViewModel = monkeyViewModel;
  7. InitializeComponent();
  8. BindingContext = _monkeyViewModel;
  9. }
  10. }
展开查看全部
mfuanj7w

mfuanj7w2#

只能绑定公共属性
这不是一个属性

  1. public String s = "Hello world";

这是

  1. public String s { get; } = "Hello world";

相关问题