.NET MAUI (Xamarin Forms) 页面/自定义视图何时被销毁?
我是 Xamarin/.NET MAUI 应用程序开发的新手。我开始为 Android 设备开发示例 .NET MAUI 应用程序。
我试图了解页面和我的自定义视图如何/何时被销毁(处置)。我阅读了一些网页,但我无法真正理解 .NET MAUI(或 Xamarin)中的工作原理。
我有三个页面:MainPage
、SecondPage
、TestMapPage
。SecondPage
有一个导航到 TestMapPage
的按钮。它实例化一个 TestMapPage
对象并将其传递给 Navigation.PushAsync()
。TestMapPage
包含一个自定义视图 TestMapView
,它由我的自定义视图渲染器 TestMapViewRenderer
渲染。我在渲染器中创建一个 MapView
对象(来自 Naxam.Mapbox.Droid
),并在 TestMapPage
中显示地图。地图出现在模拟器上并且工作正常。
我认为当我导航回 MainPage< 时,SecondPage
、TestMapPage
和 TestMapView
(以及 TestMapViewRenderer 中的所有对象)将被销毁/代码>。但是,当我在渲染器中的 Dispose()
上设置断点并导航回 中的 SecondPage
或 MainPage
时,它永远不会被命中。
我的问题:
SecondPage
、TestMapPage
、TestMapView
以及视图和视图渲染器中的所有其他对象是否像MapboxMap
> 当我返回MainPage
时保存在某个地方?- 页面和视图何时被销毁/处置?
- 如果这些页面对象保留在某个地方直到应用程序关闭,这是正常行为吗?
- 如果不是正常行为,我该如何解决?
我担心内存泄漏...
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
// ...
private async void OnGoToSecondPageClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new SecondPage());
}
}
SecondPage.xaml.cs
public partial class SecondPage : ContentPage
{
// ...
private async void OnMapShowClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new TestMapPage());
}
}
TestMapPage.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"
xmlns:local="clr-namespace:MapTest"
x:Class="MapTest.TestMapPage">
<StackLayout Margin="5">
<local:TestMapView
x:Name="map"
VerticalOptions="FillAndExpand"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ContentPage>
TestMapView.cs
public class TestMapView : View { }
TestMapViewRenderer.cs
public partial class TestMapViewRenderer : ViewRenderer<TestMapView, Android.Views.View>
{
private MapboxMap map;
public TestMapViewRenderer(Context context) : base(context) {}
protected override void OnElementChanged(ElementChangedEventArgs<TestMapView> e)
{
base.OnElementChanged(e);
// ...
if (Control == null)
{
var mapView = new MapView(Context);
SetNativeControl(mapView);
mapView.GetMapAsync(this);
}
}
public void OnMapReady(MapboxMap map)
{
this.map = map;
this.map.SetStyle(Resources.GetString(Resource.String.mapbox_style_satellite), this);
}
protected override void Dispose(bool disposing)
{
// A breakpoint never hits on this line. Why?
base.Dispose(disposing);
}
// ...
}
I'm new to Xamarin/.NET MAUI application development. I started to develop a sample .NET MAUI application for Android device.
I'm trying to understand how/when a page and my custom view are destroyed (disposed of). I read some web pages but I can't really understand how things work in .NET MAUI (or Xamarin).
I have three pages: MainPage
, SecondPage
, TestMapPage
.SecondPage
has a button that navigates to TestMapPage
. It instantiates a TestMapPage
object and passes it to Navigation.PushAsync()
.TestMapPage
contains a custom view TestMapView
, which is rendered by my custom view renderer TestMapViewRenderer
. I create a MapView
object (from Naxam.Mapbox.Droid
) in the renderer and show the map in TestMapPage
. The map appears on the emulator and it works fine.
I thought that SecondPage
, TestMapPage
and TestMapView
(and all the objects in TestMapViewRenderer) will be destroyed when I navigate back to MainPage
. However, when I set a break point on Dispose()
in the renderer and navigate back to SecondPage
or MainPage
in , it never gets hit.
My questions:
- Are the
SecondPage
,TestMapPage
,TestMapView
and all the other objects in the view and view renderer likeMapboxMap
kept somewhere when I go back toMainPage
? - When are pages and views destroyed/disposed of?
- If those page objects are kept somewhere until the application shuts down, is it normal behaviour?
- If not normal behaviour, how do I fix it?
I'm worried about memory leak...
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
// ...
private async void OnGoToSecondPageClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new SecondPage());
}
}
SecondPage.xaml.cs
public partial class SecondPage : ContentPage
{
// ...
private async void OnMapShowClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new TestMapPage());
}
}
TestMapPage.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"
xmlns:local="clr-namespace:MapTest"
x:Class="MapTest.TestMapPage">
<StackLayout Margin="5">
<local:TestMapView
x:Name="map"
VerticalOptions="FillAndExpand"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ContentPage>
TestMapView.cs
public class TestMapView : View { }
TestMapViewRenderer.cs
public partial class TestMapViewRenderer : ViewRenderer<TestMapView, Android.Views.View>
{
private MapboxMap map;
public TestMapViewRenderer(Context context) : base(context) {}
protected override void OnElementChanged(ElementChangedEventArgs<TestMapView> e)
{
base.OnElementChanged(e);
// ...
if (Control == null)
{
var mapView = new MapView(Context);
SetNativeControl(mapView);
mapView.GetMapAsync(this);
}
}
public void OnMapReady(MapboxMap map)
{
this.map = map;
this.map.SetStyle(Resources.GetString(Resource.String.mapbox_style_satellite), this);
}
protected override void Dispose(bool disposing)
{
// A breakpoint never hits on this line. Why?
base.Dispose(disposing);
}
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知;
as far as I know;