You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI中CollectionView内Image的IsVisible属性初始加载不生效

解决方案

1. 确保GridSession实现INotifyPropertyChanged

目前GridSession未实现属性变更通知接口,MAUI绑定系统可能在初始UI渲染时无法正确捕获Locked的初始值。修改该类:

public class GridSession : INotifyPropertyChanged
{
    private int _id;
    private string _name;
    private bool _locked;

    public int ID
    {
        get => _id;
        set
        {
            _id = value;
            OnPropertyChanged();
        }
    }

    public string Name
    {
        get => _name;
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public bool Locked
    {
        get => _locked;
        set
        {
            _locked = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 在UI线程加载集合并触发属性通知

构造函数中批量加载集合的时机可能早于UI绑定完成,切换到UI线程执行加载,并手动触发集合属性变更:

public class MainPageViewModel : INotifyPropertyChanged
{
    private ObservableCollection<GridSession> _bridgeSessionsCollection;
    public ObservableCollection<GridSession> BridgeSessionsCollection
    {
        get => _bridgeSessionsCollection;
        set
        {
            _bridgeSessionsCollection = value;
            OnPropertyChanged();
        }
    }

    // 其他属性定义...

    public MainPageViewModel()
    {
        BridgeSessionsCollection = new ObservableCollection<GridSession>();
        
        MainThread.BeginInvokeOnMainThread(() =>
        {
            BridgeApplicationData = BridgeDataModel.Get();
            foreach (int id in BridgeApplicationData.BridgeSessionIDs.Values)
            {
                GridSession gs = new GridSession();
                gs.ID = id;
                BridgeSessionModel bs = BridgeSessionModel.Get(id);
                gs.Name = bs.Name;
                gs.Locked = bs.Locked;

                BridgeSessionsCollection.Add(gs);

                IDCount = Math.Max(IDCount, gs.ID);
            }
            OnPropertyChanged(nameof(BridgeSessionsCollection));
        });
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

3. 给Image添加固定WidthRequest

避免Grid百分比列在初始渲染时宽度计算异常,给Image设置固定宽度:

<Image IsVisible="{Binding Locked}" 
       HeightRequest="24" 
       WidthRequest="24"
       Source="locked.png" 
       Grid.Column="3"  
       VerticalOptions="Start" />

4. 强制CollectionView刷新布局

在MainPage的OnAppearing方法中触发布局刷新,确保元素正确渲染:

protected override void OnAppearing()
{
    base.OnAppearing();
    // 给CollectionView添加x:Name="SessionCollection"
    SessionCollection?.InvalidateLayout();
}

问题根源

这是Windows平台MAUI初始渲染的时序问题:UI渲染完成前集合已加载,但绑定系统未同步IsVisible的初始值。调整窗口或返回页面会触发UI重绘,此时绑定值被正确应用。上述方案从属性通知、UI线程加载、布局刷新三个维度解决初始渲染的同步问题。

内容的提问来源于stack exchange,提问作者Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 20:15:25