Completed
Last Updated: 13 Jul 2022 11:19 by ADMIN
Release Release 2.0.0
Scofield
Created on: 07 Apr 2022 17:02
Category: Popup
Type: Bug Report
1
Popup: NullReferenceException When Setting PlacementTarget in CodeBehind

Hi Team,

You can replicate this behavior with the following code in a preview14 MAUI project with Telerik UI for MAUI v0.6.0.

Notes:

  • All other UI for MAUI prerequisites have been met, as this work when being defined in XAML instead.
  • I have tested other page elements for the PlacementTarget, like this.Content and RootGrid, same exception.
  • I am deploying to Windows, so I am not sure if this happens in iOS/Android/MacCatalyst

Repro

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.MainPage"
             BackgroundColor="{DynamicResource SecondaryColor}">
    <Grid x:Name="RootGrid">
        <Label Text="Stuff"  />
    </Grid>
</ContentPage>

MainPage.xaml.cs

using Telerik.XamarinForms.Primitives;

namespace MyApp;

public partial class MainPage : ContentPage
{
    RadPopup popup;
    
    public MainPage()
    {
        InitializeComponent();

        popup = new RadPopup();
        popup.PlacementTarget = this;
        popup.Placement = PlacementMode.Center;

        popup.IsModal = true;
        popup.OutsideBackgroundColor = Color.FromArgb("#FF0000");

        popup.Content = new Label { Text = "Hello Telerik Popup!" };
    }
}

Screenshot of Breakpoint

StackTrace

   at Telerik.RadMauiControlsHelper.<>c.<.cctor>b__0_0()
   at Telerik.RadMauiCoreHelper.get_MauiContext()
   at Telerik.XamarinForms.Primitives.RadPopup.SetPopupIsOpen(Boolean value)
   at Telerik.XamarinForms.Primitives.RadPopup.UpdatePopup()
   at Telerik.XamarinForms.Primitives.PopupBase.OnPlacementTargetChanged()
   at Telerik.XamarinForms.Primitives.PopupBase.<>c.<.cctor>b__113_3(BindableObject b, Object o, Object n)
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
   at Telerik.XamarinForms.Primitives.PopupBase.set_PlacementTarget(VisualElement value)

 

1 comment
ADMIN
Lance | Senior Manager Technical Support
Posted on: 08 Apr 2022 13:25

Hello Scofield,

I'm writing to let you know that we have fixed the issue and it will be available in the next update.

The underlying problem has to do with how long it takes for .NET MAUI to instantiate the initial context for Main. As a temporary workaround, you can add a short delay after MainPage is created before creating the RadPopup.

Here is an example:

using Telerik.XamarinForms.Primitives;

namespace PopupNreTest;

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

    protected override async void OnAppearing()
    {
        base.OnAppearing();

        await Task.Delay(1000);

		popup = new RadPopup();
		popup.PlacementTarget = this;
		popup.Placement = PlacementMode.Center;

		popup.IsModal = true;
		popup.OutsideBackgroundColor = Color.FromArgb("#FF0000");

		popup.Content = new Label { Text = "Hello Telerik Popup!" };
	}
}

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.