IsVisible=false 时 Xamarin 表单视图优化

Xamarin forms view optimization when IsVisible=false

我正在寻找最佳实践,以便在切换布局的可见性时优化 Xamarin Forms 应用程序,以便在调整屏幕大小时显示它们。如果 IsVisible=false,则 Xamarin Forms 加载任何布局。所以,我想要优化我的应用程序的是,在 IsVisible=True 时加载布局,如果它变成 False,有没有办法 "unload" 它不需要太多设备的内存?因为UWP没有问题,但是内存不足的手机有。

假设我有下一个代码:

    <StackLayout IsVisible="False" Orientation="Vertical">
        <!-- Make it visible when small screen -->
        <CustomView1></CustomView1>
        <CustomView2></CustomView2>
    </StackLayout>

    <StackLayout IsVisible="True" Orientation="Horizontal">
        <!-- Make it NON visible when small screen -->
        <CustomView1></CustomView1>
        <CustomView2></CustomView2>
    </StackLayout>

Xamarin 会加载相同的视图 2 次。我正在寻找的就像在网络中一样,将其从 DOM 中删除(或者在它可见之前不要将其加载到 DOM 中),但对于 Xamarin Forms。因此,该应用程序在不需要时不会加载视图,我希望在运行时在 window 应用程序调整大小时拥有它。

PS:可以利用OnIdiom.Phone、OnIdiom.Tablet和OnIdiom.Desktop来完成,但最后,如果UWP window 应用调整大小。

You can use Control Templates to change the appearance of a view/page at runtime. This is helpful if you want to re-theme pages or switch between views depending on the context. The property that allows to set a control template is called ControlTemplate which is available in ContentPage and ContentView classes.

参考: https://www.xamboy.com/2019/01/18/using-control-templates-in-xamarin-forms/

您可以定义普通屏幕、大屏幕等的视觉状态,它们会根据当前状态显示或不显示:

Use the Visual State Manager to make changes to XAML elements based on visual states set from code.

The Visual State Manager (VSM) is new in Xamarin.Forms 3.0. The VSM provides a structured way to make visual changes to the user interface from code. In most cases, the user interface of the application is defined in XAML, and this XAML includes markup describing how the Visual State Manager affects the visuals of the user interface.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual-state-manager