如何从另一个 ViewModel 中访问 ObservableCollection?

huangapple go评论107阅读模式
英文:

How to access ObservableCollection from another ViewModel?

问题

I have a BaseViewModel where I define my ObservableCollection and then I populate it. In another ViewModel I have a Grid and I bind it to my collection, everything works.

  1. public partial class BaseViewModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private ObservableCollection<Patient> patientInfo;
  5. public BaseViewModel()
  6. {
  7. PopulateObservableCollection();
  8. }
  9. public void PopulateObservableCollection()
  10. {
  11. PatientInfo = new ObservableCollection<Patient>();
  12. try
  13. {
  14. string query = "select * from " + App.userDetails.DbTable;
  15. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, conn);
  16. using (sqlDataAdapter)
  17. {
  18. System.Data.DataTable dt = new System.Data.DataTable();
  19. sqlDataAdapter.Fill(dt);
  20. foreach (System.Data.DataRow row in dt.Rows)
  21. PatientInfo.Add(new Patient(Convert.ToInt32(row[0]), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString(), row[6].ToString(), row[7].ToString(), row[8].ToString()));
  22. }
  23. }
  24. catch (Exception)
  25. { }
  26. }
  27. }

Now I want to change my ObservableCollection from the other ViewModels and be able to access the memory from the BaseViewModel.

Next a ViewModel where I try to change the ObservableCollection, but I'm not accessing the objected created in the BaseViewModel:

  1. [QueryProperty(nameof(Patient), nameof(Patient))]
  2. public partial class EditingViewModel : BaseViewModel
  3. {
  4. [ObservableProperty] private Patient patient;
  5. [RelayCommand]
  6. private async Task Delete()
  7. {
  8. try
  9. {
  10. string query = "DELETE from " + App.userDetails.DbTable + " WHERE id = @patientId";
  11. sqlConnection = new SqlConnection(conn);
  12. SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
  13. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  14. using (sqlDataAdapter)
  15. {
  16. sqlConnection.Open();
  17. sqlCommand.Parameters.AddWithValue("@patientId", Patient.Id);
  18. sqlCommand.ExecuteScalar();
  19. }
  20. //Removing object from observable collection
  21. PatientInfo.Remove(PatientInfo.SingleOrDefault(x => x.Id == Patient.Id));
  22. }
  23. catch (Exception ex)
  24. {
  25. await Shell.Current.DisplayAlert("Aplicação", ex.ToString(), "Sair");
  26. }
  27. finally
  28. {
  29. sqlConnection.Close();
  30. }
  31. }
  32. }

My "DashboardView" where the grid is defined:

  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:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
  5. x:Class="PatientAppMultiPlatf.Views.DashboardPage"
  6. xmlns:viewModels="clr-namespace:PatientAppMultiPlatf.ViewModels"
  7. xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
  8. Title="Lista de Pacientes">
  9. <VerticalStackLayout Padding="10">
  10. <HorizontalStackLayout Padding="20" >
  11. <Button
  12. Text="Exportar" Command="{Binding PDFCommand}" CornerRadius="20" WidthRequest="100" HorizontalOptions="End">
  13. </Button>
  14. <Button
  15. Text="Filtrar" Command="{Binding FilterCommand}" WidthRequest="100" CornerRadius="20" HorizontalOptions="Center">
  16. </Button>
  17. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  18. <Entry Text="{Binding Date}" VerticalOptions="Center" Placeholder="Data"/>
  19. </Frame>
  20. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  21. <Entry Text="{Binding Name}" VerticalOptions="Center" Placeholder="Nome"/>
  22. </Frame>
  23. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  24. <Entry Text="{Binding Age}" VerticalOptions="Center" Placeholder="Idade"/>
  25. </Frame>
  26. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  27. <Entry Text="{Binding Gender}" VerticalOptions="Center" Placeholder="Genero"/>
  28. </Frame>
  29. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  30. <Entry Text="{Binding Process}" VerticalOptions="Center" Placeholder="Processo"/>
  31. </Frame>
  32. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  33. <Entry Text="{Binding Diagnostic}" VerticalOptions="Center" Placeholder="Diagnostico"/>
  34. </Frame>
  35. <Frame HeightRequest="45" Padding="5" HasShadow="True" BorderColor="White">
  36. <Entry Text="{Binding Discharge}" VerticalOptions="Center" Placeholder="Alta"/>
  37. </Frame>
  38. </HorizontalStackLayout>
  39. <syncfusion:SfDataGrid x:Name="dataGrid" DefaultColumnWidth="90"
  40. ItemsSource="{Binding PatientInfo}" SortingMode="Single" SelectedRow="{Binding SelectedPatient}" SelectionMode="Single"
  41. HeightRequest="600" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" AutoGenerateColumnsMode="None">
  42. <syncfusion:SfDataGrid.Behaviors>
  43. <toolkit:EventToCommandBehavior
  44. EventName="CellDoubleTapped"
  45. Command="{Binding CellDoubleTappedCommand}">
  46. </toolkit:EventToCommandBehavior>
  47. </syncfusion:SfDataGrid.Behaviors>
  48. <syncfusion:SfDataGrid.Columns VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
  49. <syncfusion:DataGridTextColumn HeaderText="Data" MappingName="Date"/>
  50. <syncfusion:DataGridTextColumn HeaderText="Nome" MappingName="Name" ColumnWidthMode="Fill"/>
  51. <syncfusion:DataGridTextColumn HeaderText="Idade" MappingName="Age"/>
  52. <syncfusion:DataGridTextColumn HeaderText="Genero" MappingName="Gender"/>
  53. <syncfusion:DataGridTextColumn HeaderText="Processo" MappingName="Process"/>
  54. <syncfusion:DataGridTextColumn HeaderText="Diagnostico" MappingName="Diagnostic" ColumnWidthMode="Fill"/>
  55. <syncfusion:DataGridTextColumn HeaderText="Alta" MappingName="Discharge" ColumnWidthMode="LastColumnFill"/>
  56. </syncfusion:SfDataGrid.Columns>
  57. <syncfusion:SfDataGrid.DefaultStyle>
  58. <syncfusion:DataGridStyle HeaderRowBackground="#b2cbc8" HeaderRowTextColor="Black" />
  59. </syncfusion:SfDataGrid.DefaultStyle>
  60. </syncfusion:SfDataGrid>
  61. </VerticalStackLayout>
  62. </ContentPage>

Each view has a xaml.cs where I bind the page to the view model:

  1. namespace PatientAppMultiPlatf.Views;
  2. public partial class DashboardPage : ContentPage
  3. {
  4. public DashboardPage(DashboardViewModel viewModel)
  5. {
  6. InitializeComponent();
  7. BindingContext = viewModel;
  8. }
  9. }

I'm using MVVM architecture. What is the best method to access the ObservableCollection that is being

英文:

I have a BaseViewModel where I define my ObservableCollection and then I populate it. In another ViewModel I have a Grid and I bind it to my collection, everything works.

  1. public partial class BaseViewModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private ObservableCollection&lt;Patient&gt; patientInfo;
  5. public BaseViewModel()
  6. {
  7. PopulateObservableCollection();
  8. }
  9. public void PopulateObservableCollection()
  10. {
  11. PatientInfo = new ObservableCollection&lt;Patient&gt;();
  12. try
  13. {
  14. string query = &quot;select * from &quot; + App.userDetails.DbTable;
  15. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, conn);
  16. using (sqlDataAdapter)
  17. {
  18. System.Data.DataTable dt = new System.Data.DataTable();
  19. sqlDataAdapter.Fill(dt);
  20. foreach (System.Data.DataRow row in dt.Rows)
  21. PatientInfo.Add(new Patient(Convert.ToInt32(row[0]), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString(), row[6].ToString(), row[7].ToString(), row[8].ToString()));
  22. }
  23. }
  24. catch (Exception)
  25. { }
  26. }
  27. }

Now I want to change my ObservableCollection from the other ViewModels and be able to access the memory from the BaseViewModel.

Next a ViewModel where I try to change the ObservableCollection, but I'm not accessing the objected created in the BaseViewModel:

  1. [QueryProperty(nameof(Patient), nameof(Patient))]
  2. public partial class EditingViewModel : BaseViewModel
  3. {
  4. [ObservableProperty] private Patient patient;
  5. [RelayCommand]
  6. private async Task Delete()
  7. {
  8. try
  9. {
  10. string query = &quot;DELETE from &quot; + App.userDetails.DbTable + &quot; WHERE id = @patientId&quot;;
  11. sqlConnection = new SqlConnection(conn);
  12. SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
  13. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  14. using (sqlDataAdapter)
  15. {
  16. sqlConnection.Open();
  17. sqlCommand.Parameters.AddWithValue(&quot;@patientId&quot;, Patient.Id);
  18. sqlCommand.ExecuteScalar();
  19. }
  20. //Removing object from observable collection
  21. PatientInfo.Remove(PatientInfo.SingleOrDefault(x =&gt; x.Id == Patient.Id));
  22. }
  23. catch (Exception ex)
  24. {
  25. await Shell.Current.DisplayAlert(&quot;Aplica&#231;ao&quot;, ex.ToString(), &quot;Sair&quot;);
  26. }
  27. finally
  28. {
  29. sqlConnection.Close();
  30. }
  31. }
  32. }

My "DashboardView" where the grid is defined:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  2. &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
  3. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  4. xmlns:toolkit=&quot;http://schemas.microsoft.com/dotnet/2022/maui/toolkit&quot;
  5. x:Class=&quot;PatientAppMultiPlatf.Views.DashboardPage&quot;
  6. xmlns:viewModels=&quot;clr-namespace:PatientAppMultiPlatf.ViewModels&quot;
  7. xmlns:syncfusion=&quot;clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid&quot;
  8. Title=&quot;Lista de Pacientes&quot;&gt;
  9. &lt;VerticalStackLayout Padding=&quot;10&quot;&gt;
  10. &lt;HorizontalStackLayout Padding=&quot;20&quot; &gt;
  11. &lt;Button
  12. Text=&quot;Exportar&quot; Command=&quot;{Binding PDFCommand}&quot; CornerRadius=&quot;20&quot; WidthRequest=&quot;100&quot; HorizontalOptions=&quot;End&quot;&gt;
  13. &lt;/Button&gt;
  14. &lt;Button
  15. Text=&quot;Filtrar&quot; Command=&quot;{Binding FilterCommand}&quot; WidthRequest=&quot;100&quot; CornerRadius=&quot;20&quot; HorizontalOptions=&quot;Center&quot;&gt;
  16. &lt;/Button&gt;
  17. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  18. &lt;Entry Text=&quot;{Binding Date}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Data&quot;/&gt;
  19. &lt;/Frame&gt;
  20. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  21. &lt;Entry Text=&quot;{Binding Name}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Nome&quot;/&gt;
  22. &lt;/Frame&gt;
  23. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  24. &lt;Entry Text=&quot;{Binding Age}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Idade&quot;/&gt;
  25. &lt;/Frame&gt;
  26. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  27. &lt;Entry Text=&quot;{Binding Gender}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Genero&quot;/&gt;
  28. &lt;/Frame&gt;
  29. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  30. &lt;Entry Text=&quot;{Binding Process}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Processo&quot;/&gt;
  31. &lt;/Frame&gt;
  32. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  33. &lt;Entry Text=&quot;{Binding Diagnostic}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Diagnostico&quot;/&gt;
  34. &lt;/Frame&gt;
  35. &lt;Frame HeightRequest=&quot;45&quot; Padding=&quot;5&quot; HasShadow=&quot;True&quot; BorderColor=&quot;White&quot;&gt;
  36. &lt;Entry Text=&quot;{Binding Discharge}&quot; VerticalOptions=&quot;Center&quot; Placeholder=&quot;Alta&quot;/&gt;
  37. &lt;/Frame&gt;
  38. &lt;/HorizontalStackLayout&gt;
  39. &lt;syncfusion:SfDataGrid x:Name=&quot;dataGrid&quot; DefaultColumnWidth=&quot;90&quot;
  40. ItemsSource=&quot;{Binding PatientInfo}&quot; SortingMode=&quot;Single&quot; SelectedRow=&quot;{Binding SelectedPatient}&quot; SelectionMode=&quot;Single&quot;
  41. HeightRequest=&quot;600&quot; VerticalOptions=&quot;CenterAndExpand&quot; HorizontalOptions=&quot;Center&quot; AutoGenerateColumnsMode=&quot;None&quot;&gt;
  42. &lt;syncfusion:SfDataGrid.Behaviors&gt;
  43. &lt;toolkit:EventToCommandBehavior
  44. EventName=&quot;CellDoubleTapped&quot;
  45. Command=&quot;{Binding CellDoubleTappedCommand}&quot;&gt;
  46. &lt;/toolkit:EventToCommandBehavior&gt;
  47. &lt;/syncfusion:SfDataGrid.Behaviors&gt;
  48. &lt;syncfusion:SfDataGrid.Columns VerticalOptions=&quot;CenterAndExpand&quot; HorizontalOptions=&quot;CenterAndExpand&quot;&gt;
  49. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Data&quot; MappingName=&quot;Date&quot;/&gt;
  50. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Nome&quot; MappingName=&quot;Name&quot; ColumnWidthMode=&quot;Fill&quot;/&gt;
  51. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Idade&quot; MappingName=&quot;Age&quot;/&gt;
  52. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Genero&quot; MappingName=&quot;Gender&quot;/&gt;
  53. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Processo&quot; MappingName=&quot;Process&quot;/&gt;
  54. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Diagnostico&quot; MappingName=&quot;Diagnostic&quot; ColumnWidthMode=&quot;Fill&quot;/&gt;
  55. &lt;syncfusion:DataGridTextColumn HeaderText=&quot;Alta&quot; MappingName=&quot;Discharge&quot; ColumnWidthMode=&quot;LastColumnFill&quot;/&gt;
  56. &lt;/syncfusion:SfDataGrid.Columns&gt;
  57. &lt;syncfusion:SfDataGrid.DefaultStyle&gt;
  58. &lt;syncfusion:DataGridStyle HeaderRowBackground=&quot;#b2cbc8&quot; HeaderRowTextColor=&quot;Black&quot; /&gt;
  59. &lt;/syncfusion:SfDataGrid.DefaultStyle&gt;
  60. &lt;/syncfusion:SfDataGrid&gt;
  61. &lt;/VerticalStackLayout&gt;
  62. &lt;/ContentPage&gt;

Each view has a xaml.cs where I bind the page to the view model:

  1. namespace PatientAppMultiPlatf.Views;
  2. public partial class DashboardPage : ContentPage
  3. {
  4. public DashboardPage(DashboardViewModel viewModel)
  5. {
  6. InitializeComponent();
  7. BindingContext = viewModel;
  8. }
  9. }

I'm using MVVM architecture. What is the best method to access the ObservableCollection that is being binded?

答案1

得分: 3

BaseViewModel中,将以下部分进行更改:

  1. private ObservableCollection&lt;Patient&gt; patientInfo;

更改为:

  1. protected ObservableCollection&lt;Patient&gt; patientInfo;
英文:

In the BaseViewModel, change

  1. [ObservableProperty]
  2. private ObservableCollection&lt;Patient&gt; patientInfo;

to

  1. [ObservableProperty]
  2. protected ObservableCollection&lt;Patient&gt; patientInfo;

答案2

得分: 1

ObservableCollections 不是 ObservableProperties。

对于集合本身的更改足以通知任何更改。

如果我担心将其设置为其他内容,通常会这样做:

  1. public ObservableCollection MyProperty {get;} = new();

此外,我没有看到您的 x:DataType 在任何地方。为什么没有设置它,以及为什么您的 VisualElement 有名称?

编辑:我不确定我是否足够强调“PUBLIC”这个词,所以我想再次指出。

英文:

ObservableCollections are not ObservableProperties.

The change to the collection itself is more than enough to notify any changes.

If I am concerned about setting it to something else, I usually do this:

  1. **public** ObservableCollection MyProperty {get;} = new();

Also, I do not see anywhere your x:DataType. Why are you not setting it, and why is your VisualElement named?

Edit: I am not sure I put enough stress on the word PUBLIC, so I want to point it out again here.

答案3

得分: 0

根据您的代码,我创建了一个演示,它可以从继承自父类(BaseViewModel)属性的PatientInfo中移除项目。您可以参考以下代码:

BaseViewModel.cs

  1. public partial class BaseViewModel : ObservableObject
  2. {
  3. [ObservableProperty]
  4. private ObservableCollection<Patient> patientInfo;
  5. public BaseViewModel()
  6. {
  7. PopulateObservableCollection();
  8. }
  9. public void PopulateObservableCollection()
  10. {
  11. PatientInfo = new ObservableCollection<Patient>();
  12. try
  13. {
  14. PatientInfo.Add(new Patient { Id = 01, Name = "test1" });
  15. PatientInfo.Add(new Patient { Id = 02, Name = "test2" });
  16. PatientInfo.Add(new Patient { Id = 03, Name = "test3" });
  17. }
  18. catch (Exception)
  19. { }
  20. }
  21. }
  22. public class Patient
  23. {
  24. public int Id { get; set; }
  25. public string Name { get; set; }
  26. }

EditingViewModel.cs

  1. public partial class EditingViewModel : BaseViewModel
  2. {
  3. [RelayCommand]
  4. private async Task Delete()
  5. {
  6. try
  7. {
  8. // 从可观察集合中移除对象
  9. // 这里我可以从父类`BaseViewModel`的`PatientInfo`中移除项目
  10. PatientInfo.RemoveAt(0);
  11. }
  12. catch (Exception ex)
  13. {
  14. await Shell.Current.DisplayAlert("应用", ex.ToString(), "退出");
  15. }
  16. finally
  17. {
  18. }
  19. }
  20. }

MainPage.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:MauiMvvmApp="clr-namespace:MauiMvvmApp226"
  5. x:Class="MauiMvvmApp226.MainPage">
  6. <ContentPage.BindingContext>
  7. <MauiMvvmApp:EditingViewModel></MauiMvvmApp:EditingViewModel>
  8. </ContentPage.BindingContext>
  9. <VerticalStackLayout
  10. Spacing="25"
  11. Padding="30,0"
  12. VerticalOptions="Start">
  13. <ListView ItemsSource="{Binding PatientInfo}">
  14. <ListView.ItemTemplate>
  15. <DataTemplate>
  16. <TextCell Text="{Binding Id}"
  17. Detail="{Binding Name}"
  18. TextColor="#f35e20"
  19. DetailColor="#503026" />
  20. </DataTemplate>
  21. </ListView.ItemTemplate>
  22. </ListView>
  23. <Button
  24. Command="{Binding DeleteCommand}"
  25. x:Name="CounterBtn"
  26. Text="点击我"
  27. HorizontalOptions="Center" />
  28. </VerticalStackLayout>
  29. </ContentPage>
英文:

Based on your code,I created a demo and it could remove the item from PatientInfo which inherits from the properties of the parent class( BaseViewModel)

You can refer to the following code:

BaseViewModel.cs

  1. public partial class BaseViewModel: ObservableObject
  2. {
  3. [ObservableProperty]
  4. private ObservableCollection&lt;Patient&gt; patientInfo;
  5. public BaseViewModel()
  6. {
  7. PopulateObservableCollection();
  8. }
  9. public void PopulateObservableCollection()
  10. {
  11. PatientInfo = new ObservableCollection&lt;Patient&gt;();
  12. try
  13. {
  14. PatientInfo.Add(new Patient { Id = 01, Name=&quot;test1&quot;});
  15. PatientInfo.Add(new Patient { Id = 02, Name = &quot;test2&quot; });
  16. PatientInfo.Add(new Patient { Id = 03, Name = &quot;test3&quot; });
  17. }
  18. catch (Exception)
  19. { }
  20. }
  21. }
  22. public class Patient
  23. {
  24. public int Id { get; set; }
  25. public string Name { get; set; }
  26. }

EditingViewModel.cs

   

  1. public partial class EditingViewModel : BaseViewModel
  2. {
  3. //[ObservableProperty] private Patient patient;
  4. [RelayCommand]
  5. private async Task Delete()
  6. {
  7. try
  8. {
  9. //Removing object from observable collection
  10. //here I could remove the item from PatientInfo from parent class `BaseViewModel `
  11. PatientInfo.RemoveAt(0);
  12. }
  13. catch (Exception ex)
  14. {
  15. await Shell.Current.DisplayAlert(&quot;Aplica&#231;ao&quot;, ex.ToString(), &quot;Sair&quot;);
  16. }
  17. finally
  18. {
  19. }
  20. }
  21. }

MainPage.xaml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  2. &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
  3. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  4. xmlns:MauiMvvmApp=&quot;clr-namespace:MauiMvvmApp226&quot;
  5. x:Class=&quot;MauiMvvmApp226.MainPage&quot;&gt;
  6. &lt;ContentPage.BindingContext&gt;
  7. &lt;MauiMvvmApp:EditingViewModel&gt;&lt;/MauiMvvmApp:EditingViewModel&gt;
  8. &lt;/ContentPage.BindingContext&gt;
  9. &lt;VerticalStackLayout
  10. Spacing=&quot;25&quot;
  11. Padding=&quot;30,0&quot;
  12. VerticalOptions=&quot;Start&quot;&gt;
  13. &lt;ListView ItemsSource=&quot;{Binding PatientInfo}&quot;&gt;
  14. &lt;ListView.ItemTemplate&gt;
  15. &lt;DataTemplate&gt;
  16. &lt;TextCell Text=&quot;{Binding Id}&quot;
  17. Detail=&quot;{Binding Name}&quot;
  18. TextColor=&quot;#f35e20&quot;
  19. DetailColor=&quot;#503026&quot; /&gt;
  20. &lt;/DataTemplate&gt;
  21. &lt;/ListView.ItemTemplate&gt;
  22. &lt;/ListView&gt;
  23. &lt;Button
  24. Command=&quot;{ Binding DeleteCommand}&quot;
  25. x:Name=&quot;CounterBtn&quot;
  26. Text=&quot;Click me&quot;
  27. HorizontalOptions=&quot;Center&quot; /&gt;
  28. &lt;/VerticalStackLayout&gt;
  29. &lt;/ContentPage&gt;  

huangapple
  • 本文由 发表于 2023年2月24日 17:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定