4

zIn .net Maui application I have the following problem: my collection view worked fine in debug mode, but in release mode instead of an image and two labels, the collection view shows only a white rectangle. The onClick function worked properly: This is xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:VegaMaui"
             xmlns:model="clr-namespace:VegaMaui.Model"
             x:Class="VegaMaui.MenuDetails"   x:DataType="vm:MenuDetailsViewModel"        
             Title="Ապրանքներ"
             BackgroundColor="#f9f9f9">   
    <CollectionView 
            RemainingItemsThreshold="5"
            RemainingItemsThresholdReachedCommand="{Binding LoadMoreS}"
            SelectionMode="Single"
            SelectionChanged="OnProdSelectionChanged"
            ItemsSource="{Binding ProdListS}"
            x:Name="prodcollect">
        <CollectionView.ItemsLayout>
            <GridItemsLayout Orientation="Vertical"
                        Span="2" />
        </CollectionView.ItemsLayout>       
        <CollectionView.ItemTemplate>            
            <DataTemplate x:DataType="model:ProductImg">                    
                <Grid Margin="5,5,5,5" Padding="1" >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />                           
                    </Grid.ColumnDefinitions>
                    <Image
                        Grid.Row="0"                           
                        Grid.Column="0"                         
                        Source= "{Binding firstPicture}"                        
                        Aspect="AspectFill"/>                    
                    <Label Grid.Row="1"
                        Grid.Column="0"                      
                        FontAttributes="Bold"
                        BackgroundColor="White"
                        LineBreakMode="WordWrap"
                        Text="{Binding product_Name}"
                        VerticalOptions="Start" />
                    <Label Grid.Row="2"
                        Grid.Column="0"                      
                        FontAttributes="Bold"
                        LineBreakMode="WordWrap"
                        BackgroundColor="White"
                        Text="{Binding product_DisPrice, StringFormat='{0:F0} դր.'}"
                        TextColor ="Red" FontSize="Default"
                        VerticalOptions="Start" />
                    <Label Grid.Row="3"
                        Grid.Column="0"                                   
                        LineBreakMode="WordWrap"
                        BackgroundColor="White"
                        IsVisible="{Binding IsShowMode}"
                        Text="{Binding product_Price, StringFormat='{0:F0} դր.'}"
                        TextColor="#40414a"  FontSize="Default" 
                        TextDecorations="Strikethrough"
                        VerticalOptions="Start" />

                </Grid>                    
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>   
</ContentPage>

This is ViewModel:

namespace VegaMaui
{
    public  class MenuDetailsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int startindex = 1;
        private int endindex = 10;        
        private string SubGrInd = "";
        private readonly VegaApi ApiControl;
        private void OnPropertyChanged(string propertyName) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        public MenuDetailsViewModel(string SubGroup)
        {
            SubGrInd = SubGroup;    
             ApiControl = new();
            // Task.Run(() => this.LoadData(SubGroup)).Wait();
            ProdListS = new ObservableCollection<Model.Product>();
            new Thread(() =>
            {
                LoadDataS();
            }).Start();
            
        }        
        public async Task LoadDataS()
        {           
            var data =  await ApiControl.GetMenuDetAsync(SubGrInd, startindex,endindex);
            ProdListS = new ObservableCollection<Model.Product>(data);
            PropertyChanged.Invoke(this,new PropertyChangedEventArgs(nameof(ProdListS)));
        }
        public ICommand LoadMoreS => new Command(async () =>
        {           
            startindex += 16;
            endindex += 16;
                ObservableCollection<Model.Product> data = null;
                new Thread(() =>
                {
                     data=  ApiControl.GetMenuDetAsync(SubGrInd, startindex, endindex).Result;          
                    foreach (var item in data)
                    {
                        ProdListS.Add(item);
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ProdListS)));
                    }               
                }).Start();            
        });       
        public ObservableCollection<Model.Product> ProdListS { get; set; } 
    }
}
    }        
    public async Task LoadDataS()
    {           
        var data =  await ApiControl.GetMenuDetAsync(SubGrInd, startindex,endindex);
        ProdListS = new ObservableCollection<Model.Product>(data);
        PropertyChanged.Invoke(this,new PropertyChangedEventArgs(nameof(ProdListS)));
    }
    public ICommand LoadMoreS => new Command(async () =>
    {           
        startindex += 16;
        endindex += 16;
            ObservableCollection<Model.Product> data = null;
            new Thread(() =>
            {
                 data=  ApiControl.GetMenuDetAsync(SubGrInd, startindex, endindex).Result;          
                foreach (var item in data)
                {
                    ProdListS.Add(item);
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ProdListS)));
                }               
            }).Start();            
    });       
    public ObservableCollection<Model.Product> ProdListS { get; set; } 
}

}

public partial class MenuDetails : ContentPage { public string[] ImgList;

private MenuDetailsViewModel _mdvm;
public MenuDetails(string subGID)
{        
    BindingContext = _mdvm = new MenuDetailsViewModel(subGID);
    InitializeComponent();    
   
}
async void OnProdSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    
    Model.Product prodItem = this.prodcollect.SelectedItem as Model.Product;
    int id_Param = prodItem.product_Id;
    VegaApi ApiControl = new();
    List<Model.ProdParams> ParamList = await ApiControl.GetParamDetAsync(id_Param.ToString());      
    ProdInformation ProfilePage = new ProdInformation();
    ProfilePage.Title = this.Title;
    ProfilePage.BindingContext = prodItem;        
    VerticalStackLayout PropsStack = ProfilePage.FindByName<VerticalStackLayout>("InfoLayout");
    //Label NmLabel = ProfilePage.FindByName<Label>("prodname");
    //NmLabel.Text = prodItem.product_Name;
    //Label PrLabel = ProfilePage.FindByName<Label>("prodprice");
    //PrLabel.Text = prodItem․+ " դր.";
   
    // Adding Images carousel view      
    string[] picarray = prodItem.product_PicturePath.Split('@');
        System.Collections.ObjectModel.ObservableCollection<Promo> Data = new System.Collections.ObjectModel.ObservableCollection<Promo>();      
    Data.Add(new Promo() { ImgSource =prodItem.firstPicture});
    if (picarray.Count() > 1)
    {          
        for (int k = 1; k < picarray.Count(); k++)
        {
            if (picarray[k] != null && picarray[k].Length > 3)
            {
                Data.Add(new Promo() { ImgSource = "https://vega.am/image/"+ picarray[k] });
            }
        }
    }
   
   CarouselView CurCarousel= ProfilePage.FindByName<CarouselView>("pcarousel");
    CurCarousel.ItemsSource = Data;
    //  StackLayout slayout = new StackLayout();    
   
   
   // PropsStack.Add(primage);
    List<int> TitleIds = (from el in ParamList select el.Title_Id).Distinct().ToList();
    if (TitleIds != null && TitleIds.Count > 0)
    {
        for (int j = 0; j<TitleIds.Count; j++)
        {
            List<Model.ProdParams> subLst= ParamList.Where(p => p.Title_Id == TitleIds[j]).ToList();
            if(subLst != null && subLst.Count > 0)
            {
                Label ltitle= new Label();
                ltitle.Text = subLst[0].Title_Name;
                ltitle.FontSize = 18;
                ltitle.TextColor = Color.FromRgb(16, 26, 175);
                ltitle.Padding = new Thickness(3, 5, 3, 5);
                ltitle.HorizontalOptions = LayoutOptions.Start;
                PropsStack.Add(ltitle);
                Grid CurGrid = new Grid
                {
                    
                    ColumnDefinitions =
                     {
                        new ColumnDefinition{Width = new GridLength(5, GridUnitType.Star)},
                        new ColumnDefinition{Width = new GridLength(3, GridUnitType.Star)}
                     }
                };                    
                for (int q=0;q< subLst.Count;q++)
                {     
                    CurGrid.AddRowDefinition(new RowDefinition());
                   
                    
                    CurGrid.Add(new Label
                    {
                        
                        Text = subLst[q].Param_Name+" ",
                        LineBreakMode = LineBreakMode.WordWrap,
                        MaxLines =2,
                        HorizontalOptions = LayoutOptions.Start,
                        VerticalOptions = LayoutOptions.Center
                    },0,q);
                    CurGrid.Add(new Label
                     {
                        Text = subLst[q].Param_Value,
                        LineBreakMode = LineBreakMode.WordWrap,
                        MaxLines = 2,
                        HorizontalOptions = LayoutOptions.Start,
                        VerticalOptions = LayoutOptions.Center
                    },1,q);
                }
                PropsStack.Add(CurGrid);
            }
        }
    }        
    await Navigation.PushAsync(ProfilePage);
}

}

Alisa
  • 43
  • 3
  • Was data loaded completely in ProdListS CollectionView in release mode? – Liqun Shen-MSFT Apr 10 '23 at 05:30
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), there seems to be a lot of irrelevant code in your example. Also, it's hard to read the code properly as it's not well formatted here. Make sure that the indentation is consistent and you use the code tags with back ticks (```) correctly. – Julian Apr 11 '23 at 07:05

1 Answers1

2

I see bound ItemsSource:

ItemsSource="{Binding ProdListS}"

Specified ProductImg model:

<DataTemplate x:DataType="model:ProductImg">

And yet in code you have:

public ObservableCollection<Model.Product> ProdListS { get; set; } 

This is what you should work on.

It is perfectly fine to mismatch models, even do not specify one as DataType of your DataTemplate. As long as you debug.

In release things are not the same.

Julian
  • 5,290
  • 1
  • 17
  • 40
H.A.H.
  • 2,104
  • 1
  • 8
  • 21
  • 3
    Yes, the datatype mismatch is one question. But why it worked in debug mode? I am just curious about that. – Liqun Shen-MSFT Apr 10 '23 at 08:34
  • 1
    @LiqunShen-MSFT https://stackoverflow.com/questions/75283345/collectionview-working-in-debug-but-not-in-release-in-net-maui Another guy, asked me the same question, few months ago. I do not know. I can avoid problems caused by this "feature" very well, but I do not know the underlying reason for this. – H.A.H. Apr 11 '23 at 07:28
  • 2
    Okay, that was really incredible, Just let op try. Thanks for your share. – Liqun Shen-MSFT Apr 11 '23 at 07:47
  • Thank You very much for your answer. I didn't notice the mismatch․Thanks a lot – Alisa Apr 11 '23 at 10:28
  • @Alisa Do not forget to always specify the DataTypes, as well. Even if it is List. Good luck with your project. – H.A.H. Apr 11 '23 at 10:31
  • 1
    Thank You for help. I will always specify the data type – Alisa Apr 11 '23 at 10:40