Closed FreakyAli closed 9 months ago
It does throw if it's null, so I believe that's by design. The PlatformView and VirtualView shouldn't be null if you ask for them, so it may be that it's null at all is the bug, it shouldn't need to be something you check for. @PureWeen Is that right?
@drasticactions The problem is even if i remove these null checks later on when I use them, the PlatformView would be null i.e. it will crash anyway!
don't use append from the ctor, that'll append to the static which will keep appending
Yea, I feel like something else in the code is removing the platformview.
If you follow our code against the stack trace the PlatformView should be set here https://github.com/dotnet/maui/blob/net7.0/src/Core/src/Handlers/Element/ElementHandler.cs#L50
And then UpdateProperties
is called here which would can correlate to your stack trace
@PureWeen In that case where do you suggest I append?
@FreakyAli I think one option would be to call the mapping in the constructor of your class that inherits the view, not the handler. At least there is an example given for a ContentPage docs that appends handlers in its constructor.
public FreakyEntry()
{
AppendMappings();
}
public partial void AppendMappings();
Then you could implement the method for instance like so on iOS:
public partial void AppendMappings()
{
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("FreakyEntry", (handler, view) =>
{
if (view is FreakyEntry freakyEntry && handler is FreakyEntryHandler freakyEntryHandler)
{
if (freakyEntry .PlatformView is not null && freakyEntryHandler.VirtualView is not null)
{
freakyEntryHandler.PlatformView.TextContentType = UITextContentType.TelephoneNumber;
freakyEntryHandler.PlatformView.KeyboardType = UIKeyboardType.PhonePad;
}
}
});
}
and then append the handler in MauiProgram
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<FreakyEntry , Handler.FreakyEntryHandler>();
}
and FreakyEntryHandler only needs to inherit from EntryHandler, but could also do other stuff that you would do OnConnect().
FWIW, I came across this error in my Android app.
In the app my MainPage navigated to another page like so
private async void ToDo_OnClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new ToDo());
}
With the ToDo page open I would close the app without leaving the page (and not just navigate away to another app). Upon opening/launching the app again I would get this PlatformView cannot be null error caught in the debugger on the base.OnStart() event here
public class MainActivity : MauiAppCompatActivity
{
protected override void OnStart()
{
base.OnStart();
...
}
The way I think I solved the issue was to remove the ToDo page from the navigation stack
public partial class ToDo : ContentPage
{
...
protected override async void OnDisappearing()
{
await Navigation.PopAsync(); // fix for "Platform View Cannot be null here" error.???
base.OnDisappearing();
}
...
}
I'm not sure if I handled this correctly, but it has allowed me to move on with development.
Hopefully, this will shed some light.
@borrmann I tried that and it still crashes, I can get around it by adding a try-catch, but I think the MAUI team should fix this...
Verified this issue using Visual Studio 17.10.0 Preview 1 (maui version:8.0.6). The issue is not reproduced on Android (API34/33)/iOS (17.0/17.2) platforms using the sample project.
Hi @FreakyAli. 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.
This seems to be fixed with version 8.0.7, Thanks!
Description
I have a custom control that has a handler that Appends Mapping to it, When I do that and have a check for PlatformView, the application crashes at the null check when I close and reopen the app, Will add more information in the reproduction steps.
The Handler:
Steps to Reproduce
https://github.com/dotnet/maui/assets/31090457/3031749e-e66e-4315-a514-2d390c40c087
Link to public reproduction project repository
https://github.com/FreakyAli/Maui.FreakyControls/tree/entry_issue/MAUI.FreakyControls/Samples
Version with bug
7.0.92
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI, Yes, this used to work in Xamarin.Forms
Last version that worked well
6.0
Affected platforms
iOS, Android
Affected platform versions
Android API 33, iOS 16
Did you find any workaround?
No, which is a major problem because I am using this handler in one of my apps which has a login screen and I am sure other apps will have a similar problem too. The weirdest part is that my literal null check i.e.
PlatformView != null
is crashing which means it's probably crashing in the getter so there probably is no workaround that I can come up with for it.Let me know if i can help with anything else.
Relevant log output