maui icon indicating copy to clipboard operation
maui copied to clipboard

ListView not displaying until I swipe the screen

Open Artiom-Evs opened this issue 3 years ago • 7 comments

Description

I trying to implement StateContainer by Patrick McCurley in my .NET MAUI application. It works correctly when the ListView displayed for the first time. But ListView is not displaying when state changes again until I swipe the screen.

I found this problem in this closed issue at the 3rd point.

A critical error occurs when I trying to replace ListView to CollectionView: System.InvalidCastException: 'Specified cast is not valid.'.

P.S. In Windows it works well.

Steps to Reproduce

Here is an example that reproduces the problem:

TestListUpdating.zip

States.cs

public enum States
{
    Loading, 
    Success
}

StateCondition.cs

[ContentProperty("Content")]
public class StateCondition : View
{
    public object State { get; set; }
    public View Content { get; set; }
}

StateContainer.cs

[ContentProperty("Conditions")]
public class StateContainer : ContentView
{
    public List<StateCondition> 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(stateCondition =>
                stateCondition.State != null &&
                stateCondition.State.ToString().Equals(newValue.ToString()));

        if (stateCondition == null) return;

        Content = stateCondition.Content;
    }
}

MainPage.xaml

<ContentPage ...>

    <state:StateContainer State="{Binding State}">

        <state:StateCondition State="Loading">
            <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                <ActivityIndicator IsRunning="True" />
                <Label Text="Updating data..." />
            </StackLayout>
        </state:StateCondition>

        <state:StateCondition State="Success">
            <ListView ItemsSource="{Binding SomeData}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding . }" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </state:StateCondition>

    </state:StateContainer>

</ContentPage>

MainPage.xaml.cs

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;
    }
}

Link to public reproduction project repository

https://github.com/Artiom-Evs/TestListUpdating

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 10

Did you find any workaround?

ListView displays correctly when I put ListView in Grid as shown below:

...
<state:StateCondition State="Success">
    <Grid>
        <ListView ItemsSource="{Binding SomeData}">
                ...
        </ListView>
    </Grid>
</state:StateCondition>
...

But it is work only in this example, in my main project it is not work.

Relevant log output

No response

Artiom-Evs avatar Oct 02 '22 18:10 Artiom-Evs

This happens to me every 5 - 10 deploys in debug. When it happens, PushAsync doesn't work in shell either, I don't know if it's related. I have to clean the solution and uninstall the app when it happens.

marcoablanco avatar Oct 03 '22 10:10 marcoablanco

Possibly related #7799

jfversluis avatar Oct 03 '22 14:10 jfversluis

Does this also happen whenever you don't involve the code for that StateContainer? And could you provide the repro as a GitHub repository, thanks!

jfversluis avatar Oct 03 '22 14:10 jfversluis

Hi @Artiom-Evs. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Oct 03 '22 14:10 ghost

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

ghost avatar Oct 07 '22 15:10 ghost

This is a constant problem. I have now spent 2 days trying to fix this problem and sending feedback to tech support is a useless thing for MS.

I get the items the first time and then I found out that if I click around in the empty list it will show the items. There is no way this should be closed and whatever you do to tell MS about this problem should be done as this makes the ListView useless

sqlguy6212 avatar Oct 08 '22 21:10 sqlguy6212

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Nov 11 '22 15:11 ghost

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 2.0. Not repro on windows platform with sample project. https://github.com/Artiom-Evs/TestListUpdating image

homeyf avatar Jul 10 '23 08:07 homeyf

Hi @Artiom-Evs. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Jul 10 '23 08:07 ghost