Hi,
I have an CrossPlattform App, which for the moment runs only in UWP. The App shows a WebView which musted be change to custom renderer because usage of ScriptNotify and InvokeScript. So I did and anything worked fine.
If I use PopModalAsync the App Crashes with
System.InvalidOperationException
HResult=0x80131509
Nachricht = Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.
Quelle = Xamarin.Forms.Platform.UAP
Stapelüberwachung:
bei Xamarin.Forms.Platform.UWP.VisualElementRenderer2.SetNativeControl(TNativeElement control) bei ZApp.UWP.ZAppWebViewRenderer.OnElementChanged(ElementChangedEventArgs
1 e) in C:\Users\Schulz\source\repos\ZApp\ZApp\ZApp.UWP\ZApp\Renderer\WebViewRenderer.cs: Zeile39
bei Xamarin.Forms.Platform.UWP.VisualElementRenderer2.SetElement(VisualElement element) bei Xamarin.Forms.Platform.UWP.VisualElementRenderer
2.Dispose(Boolean disposing)
bei Xamarin.Forms.Platform.UWP.VisualElementRenderer2.Dispose() bei Xamarin.Forms.Platform.UWP.VisualElementExtensions.Cleanup(VisualElement self) bei Xamarin.Forms.Platform.UWP.VisualElementPackager.Dispose(Boolean disposing) bei Xamarin.Forms.Platform.UWP.VisualElementRenderer
2.Dispose(Boolean disposing)
bei Xamarin.Forms.Platform.UWP.VisualElementRenderer`2.Dispose()
bei Xamarin.Forms.Platform.UWP.VisualElementExtensions.Cleanup(VisualElement self)
bei Xamarin.Forms.Platform.UWP.Platform.d__55.MoveNext()
at the places here shown with "Debugger.Break"
protected override void OnElementChanged(ElementChangedEventArgs<ZAppWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
if (e.NewElement != null) // If added don't crash
{
SetNativeControl(new Windows.UI.Xaml.Controls.WebView()); // <==== System.InvalidOperationException
}
else
{ // Running here during PopModalAsync
System.Diagnostics.Debugger.Break();
}
}
if (e.OldElement != null)
{
if (Control != null) // if added don't crash
{
Control.NavigationCompleted -= OnWebViewNavigationCompleted;
Control.ScriptNotify -= OnWebViewScriptNotify;
}
else
{ // Running here during PopModalAsync
System.Diagnostics.Debugger.Break();
}
if (Element != null) // during PopModalAsync again
Element.CallRenderer -= onRequest;
}
if (e.NewElement != null)
{
Control.NavigationCompleted += OnWebViewNavigationCompleted;
Control.ScriptNotify += OnWebViewScriptNotify;
Element.CallRenderer += onRequest;
Control.NavigateToString(Element.Content);
}
}
The problem was solved with the changes above. But only if I rebuild the ViewPage including the Webview every time before showing from scratch. For performance reasons, while the page content never changes, I tried to show the same page again, after one time running through PopModalAsync the App crashes. This technic worked fine without the WebView.
I can not fix the problem, because the reshowing of the existing window crashes with "Control" again is "null" during the first entry and there is no way to restore it. (SetNativeControl is not working, with the same InvalidOperationException.)
The decision to leave with PopModalAsync comes in my WorkerThread and to have PopModalAsync running on MainThread I startet it like that:
Device.BeginInvokeOnMainThread(() =>
{
App.Current.MainPage.Navigation.PopModalAsync();
});
What might be important, the page I am returning to has the same control in place. No idea but may be that will cause the problem. I am using Version 3.1.0.697729. The versioning is a reason while I am working at UWP only for now, because my android part does not compile anymore, which is another story. All is running on Windows 10.
Thanks for any help.