Warning CS0649 Field is never assigned to, and will always have its default value null .net maui

wire_jp 201 Reputation points
2022-12-23T22:45:28.477+00:00

Visual Studio shows a CS0649 error message on line 11: - "Warning CS0649 Field 'CalendarRangePickerPopupSelectedDates._onClosedPopup' is never assigned to, and will always have its default value null SampleApp (net6.0-maccatalyst) C:\Users\User\Documents\CalendarPluginMaui\SampleApp\Views\CalendarRangePickerPopupSelectedDates.xaml.cs 11 Active"

using MauiPopup.Views;  
using SampleApp.Model;  
using SampleApp.ViewModels;  
using System;  
  
namespace SampleApp.Views;  
  
public partial class CalendarRangePickerPopupSelectedDates : BasePopupPage  
{  
#pragma warning disable IDE0044 // Add readonly modifier   
    private Action<CalendarRangePickerResult> _onClosedPopup;   // CS0649 Error occurs here at "_onClosedPopup"  
#pragma warning restore IDE0044 // Add readonly modifier  
    public CalendarRangePickerPopupSelectedDates()  
 {  
        MauiPopup.PopupAction.DisplayPopup(new CalendarRangePickerPopupSelectedDates());  
  
        InitializeComponent();  
  
        MauiPopup.PopupAction.ClosePopup();  
    }  
    protected override void OnAppearing()  
    {  
        base.OnAppearing();  
  
        if (BindingContext is CalendarRangePickerPopupSelectedDatesViewModel vm)  
            vm.Closed += _onClosedPopup;  
    }  
  
    protected override void OnDisappearing()  
    {  
        if (BindingContext is CalendarRangePickerPopupSelectedDatesViewModel vm)  
            vm.Closed -= _onClosedPopup;  
  
        base.OnDisappearing();  
    }  
}  

I also tried to decorate the field with the nullable reference, but it did not work: -

    disable nullable  
     private Action<CalendarRangePickerResult>? _onClosedPopup;  
    restore nullable  

Any assistance will be greatly appreciated.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,908 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. wire_jp 201 Reputation points
    2022-12-24T18:45:02.263+00:00

    I updated the code, change the null references and the issue seemed to be fixed: -

    using MauiPopup.Views;  
    using SampleApp.Model;  
    using SampleApp.ViewModels;  
    using System;  
      
    namespace SampleApp.Views;  
      
    public partial class CalendarRangePickerPopupSelectedDates : BasePopupPage  
    {  
    #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.  
        private readonly Action<CalendarRangePickerResult>? _onClosedPopup;  
      
        public CalendarRangePickerPopupSelectedDates()  
        {  
        }  
    #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.  
        public CalendarRangePickerPopupSelectedDates(Action<CalendarRangePickerResult> onClosedPopup)  
    	{  
            _onClosedPopup = onClosedPopup;         
      
            InitializeComponent();  
              
        }  
        protected override void OnAppearing()  
        {  
            base.OnAppearing();  
      
            if (BindingContext is CalendarRangePickerPopupSelectedDatesViewModel vm)  
                vm.Closed += _onClosedPopup;  
        }  
      
        protected override void OnDisappearing()  
        {  
            if (BindingContext is CalendarRangePickerPopupSelectedDatesViewModel vm)  
                vm.Closed -= _onClosedPopup;  
      
            base.OnDisappearing();  
        }  
    }