ListView在我滑动屏幕之前不会显示

我试图在我的“StateContainer”(帕特里克McCurley ) MAUI应用程序中实现.NET。当ListView第一次显示时,它正确工作。但是,在我滑动屏幕之前,当状态再次发生变化时,ListView不会显示。

当我添加任何视图元素(标签、按钮等)时对于包含ListView的视图,它不会出现。但是,当我使用任何其他视图元素将ListView移动到网格时,StateContainer将正确显示。如果网格不包含StateContainer以外的其他元素,则StateContainer无法正确显示。

我不知道这里出了什么问题。对于我来说,带有其他视图元素的网格并不是解决方案,因为我的页面不应该包含任何其他元素,比如StateContainer。

下面是一个再现问题的示例:

我很抱歉有很多代码:)我不知道问题出在哪里。

States.cs

代码语言:javascript运行复制public enum States

{

Loading,

Success

}StateCondition.cs

代码语言:javascript运行复制[ContentProperty("Content")]

public class StateCondition : View

{

public object State { get; set; }

public View Content { get; set; }

}StateContainer.cs

代码语言:javascript运行复制[ContentProperty("Conditions")]

public class StateContainer : ContentView

{

public List Conditions { get; set; } = new();

public static readonly BindableProperty StateProperty =

BindableProperty.Create(nameof(State), typeof(object), typeof(StateContainer), null, BindingMode.Default, null, StateChanged);

private static void StateChanged(BindableObject bindable, object oldValue, object newValue)

{

var parent = bindable as StateContainer;

if (parent != null)

parent.ChooseStateProperty(newValue);

}

public object State

{

get { return GetValue(StateProperty); }

set { SetValue(StateProperty, value); }

}

private void ChooseStateProperty(object newValue)

{

if (Conditions == null && Conditions?.Count == 0) return;

var stateCondition = Conditions

.FirstOrDefault(condition =>

condition.State != null &&

condition.State.ToString().Equals(newValue.ToString()));

if (stateCondition == null) return;

Content = stateCondition.Content;

}

}MainPage.xaml

代码语言:javascript运行复制

MainPage.xaml.cs

代码语言:javascript运行复制public partial class MainPage : ContentPage

{

private States _state;

private int[] _someData;

public MainPage()

{

InitializeComponent();

this.BindingContext = this;

SomeData = new[] { 1, 2, 3, 4, 5 };

State = States.Success;

// it can be executed from outside the page

_ = Task.Run(ExecuteSomeWorkAsync);

}

public States State

{

get => _state;

private set

{

if (_state != value)

{

_state = value;

OnPropertyChanged();

}

}

}

public int[] SomeData

{

get => _someData;

private set

{

if (_someData != value)

{

_someData = value;

OnPropertyChanged();

}

}

}

public async Task ExecuteSomeWorkAsync()

{

await Task.Delay(2000);

State = States.Loading;

await Task.Delay(2000);

// generate new data for displaying

Random rnd = new();

var data = Enumerable.Range(0, 5).Select(n => rnd.Next(0, 5)).ToArray();

SomeData = data;

State = States.Success;

}

}