c# - .NET MAUI - 为什么绑定(bind)不能与 ObservableCollection 一起使用?

标签 c# .net xaml maui

我有一个类“CellularInfo”,我试图在可观察集合中显示项目,但绑定(bind)了RssiRsrpRsrq 未显示。

我知道 LTEInfo 正在 CellularInfo.xaml.cs 中填充,但它没有绑定(bind)。

Cellularinfo.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CellularSignal1"
             x:Class="CellularSignal1.CellularInfo"
             Title="Signal">
    <VerticalStackLayout>
        <ListView BackgroundColor="Red" ItemsSource="{x:Reference lteStrengths}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        <Label Grid.Row="0" Text="{Binding Rssi}" HeightRequest="50"/>
                        <Label Grid.Row="1" Text="{Binding Rsrp}" HeightRequest="50"/>
                        <Label Grid.Row="2" Text="{Binding Rsrq}" HeightRequest="50"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </VerticalStackLayout>
</ContentPage>

CellularInfo.xaml.cs:

namespace CellularSignal1;

using Android.Content;
using Android.OS;
using Android.Telephony;
using Java.Util.Logging;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Internals;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

public partial class CellularInfo : ContentPage
{
    public ObservableCollection<LTEInfo> lteStrengths { get; set; } = new ObservableCollection<LTEInfo>();

    public CellularInfo()
    {
        InitializeComponent();
    }

    protected override async void OnAppearing()
    {
       addSignalStrengths();
    }

    public void addSignalStrengths()
    {
       LTEInfo lte = new LTEInfo
      {
          Rssi = 3,
          Rsrp = 2,
          Rsrq = 7,
          Level = 8
      };
      lteStrengths.Add(lte);
    }
  }

LTEInfo.cs:

using CommunityToolkit.Mvvm.ComponentModel;

namespace CellularSignal1
{
    public class LTEInfo : ObservableObject
    {

        public int Rssi { get; set; }
        public int Rsrp { get; set; }
        public int Rsrq { get; set; }
        public int Level { get; set; }
    }
}

最佳答案

将您的ItemsSource更改为

<ListView BackgroundColor="Red" ItemsSource="{Binding lteStrengths}">
  

并在构造函数中设置您的BindingContext

public CellularInfo()
{
    InitializeComponent();

    BindingContext = this;
}

关于c# - .NET MAUI - 为什么绑定(bind)不能与 ObservableCollection 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75636558/

相关文章:

c# - 如何正确使用 VisualStateManager?

.net - 是否有 Ruby .NET 编译器?

c# - WPF 资源未加载?

c# - WPF 中带有文本 block 的动态工具提示

.net - WPF 控件未显示在 WinForms 应用程序的 ElementHost 中

c# - 如何在新的子窗口中实现文件上传?

c# - 为什么要有 Enumerator 结构和 EnumeratorImpl 类?

c# - 如何在与主窗口相同的监视器上打开对话框( View )

c# - 流的通用 IDisposable 包装器 - 捕获所有异常的替代方法

.net - 从 Monitor 所有者以外的另一个线程释放锁