why is Navigation.PushModalAsync returns null

Oli Weirdo

when class SettingsPage included only InitializeComponent();, the navigation method worked fine. But when I add some method (below) in class SettingsPage , now It`s returns null.

Bulid and rebuild returns no errors and exceptions.

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            await this.Navigation.PushModalAsync(new SettingsPage()).ConfigureAwait(false);  //Returns Null
    
        }
       
    } 

this is my SettingsPage class :

    public partial class SettingsPage : ContentPage
    {
       readonly INotificationManager notificationManager;
        DateTime _triggerTime;
        public SettingsPage()
        {
            InitializeComponent();


            Device.StartTimer(TimeSpan.FromSeconds(1), OnTimerTick);

            notificationManager = DependencyService.Get<INotificationManager>();
            notificationManager.NotificationReceived += (sender, eventArgs) =>
            {
                var evtData = (NotificationEventArgs)eventArgs;
                ShowNotification(evtData.Title, evtData.Message);
            };
        }

        bool OnTimerTick()
        {
            if (DateTime.Now >= _triggerTime)
            {
                _switch.IsToggled = false;
                DisplayAlert("Alert", " It`s time to drink water :) ", "OK");
                string title = $"Drink Water Reminder";
                string message = $" It`s time to drink water :) ";
                notificationManager.ScheduleNotification(title, message);



                if (_switchVibrate.IsToggled)
                {
                    // Use default vibration length
                    Vibration.Vibrate();

                    // Or use specified time
                    var duration = TimeSpan.FromSeconds(1);
                    Vibration.Vibrate(duration);
                }

            }
            return true;
        }

        void OnTimePickerPropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            if (args.PropertyName == "Time")
            {
                SetTriggerTime();
            }
        }

        void OnSwitchToggled(object sender, ToggledEventArgs args)
        {
            SetTriggerTime();
        }

        void SetTriggerTime()
        {
            if (_switch.IsToggled)
            {
                _triggerTime = DateTime.Today + _timePicker.Time;
                if (_triggerTime < DateTime.Now)
                {
                    _triggerTime += TimeSpan.FromDays(1);
                }
            }
        }
        void ShowNotification(string title, string message)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                var msg = new Label()
                {
                    Text = $"Notification Received:\nTitle: {title}\nMessage: {message}"
                };

            });
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            OnTimerTick();

        }

    }
Saamer

As Jason mentioned, your code in the SettingsPage is causing the exception. If you take a look at your code, and start from the Constructor, you will notice that this line is called before _triggerTime is initialized:

if (DateTime.Now >= _triggerTime)

Hence you are referencing a variable that is null, which is causing an exception. One way to solve this would be to add a check before that line:

if (_triggerTime == null)
    return false;

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Navigation.PushModalAsync가 null을 반환하는 이유

분류에서Dev

Why PrinterState always returns null?

분류에서Dev

Why values in ArrayList always returns null as first value in Java?

분류에서Dev

Why does NVL2 returns NULL when it's not expected?

분류에서Dev

Xamarin Forms에서 Navigation.PushModalAsync ()와 Navigation.PushAsync ()의 차이점은 무엇인가요?

분류에서Dev

Xamarin Forms Reverse PushModalAsync

분류에서Dev

Why does new $class; return null when class_exists($class) returns true?

분류에서Dev

Django GraphQL returns null

분류에서Dev

Malloc returns null

분류에서Dev

Request .Form returns null

분류에서Dev

JSON returns [null,null] in my app

분류에서Dev

CONCAT in stored procedure returns null

분류에서Dev

Thread Timer returns NULL output

분류에서Dev

Scrollview (findviewby id) returns null

분류에서Dev

id property returns null or undefined

분류에서Dev

MySQL count(col) that returns null

분류에서Dev

Why use != null in if statements?

분류에서Dev

Why is @Model null?

분류에서Dev

express + express-graphql helloworld returns null

분류에서Dev

DynamoDB getAccumulatedConsumedCapacity on ItemCollection always returns NULL

분류에서Dev

EF Core Eager loading returns null

분류에서Dev

Ksoap2 returns anytype{} instead of null

분류에서Dev

Android fragments - findFragmentByTag always returns null

분류에서Dev

nested query not running, always returns null.

분류에서Dev

GetBindingExpression returns null when I have a binding

분류에서Dev

Executing Bash Script Returns Null In Java

분류에서Dev

jsp form returns null value in one input

분류에서Dev

request.getParameter returns null in JSP

분류에서Dev

Querying for a contact's structured name returns null

Related 관련 기사

뜨겁다태그

보관