简体   繁体   English

在UWP的大小触发器中更改控件的父级

[英]Change parent of a control in a size trigger in UWP

I'm creating a Windows 10 Universal app, there are two panels in my main page. 我正在创建Windows 10通用应用程序,主页上有两个面板。

In tablet/desktop mode, both are visible at the same time (just two simple Grid s, one fills the left side and the other one fills the right side) 在平板电脑/桌面模式下,两者都同时可见(仅两个简单的Grid ,一个填充左侧,另一个填充右侧)

However, in phone mode, there's a pivot and user can swipe between panels. 但是,在电话模式下,有一个枢轴,用户可以在面板之间滑动。

(I could use SplitView and hamburger menus which work easily in both Tablet/Desktop and Phone modes, but a pivot makes more sense in my case) (我可以使用SplitView和hamburger菜单,它们在Tablet / Desktop和Phone模式下均可轻松工作,但在我的情况下,枢轴更有意义)

So, What I currently did is, I have two instances of each panel. 因此,我目前要做的是,每个面板都有两个实例。 PhonePanel1 and PhonePanel2 are two grids inside two pages of my pivot control, and TabletPanel1 and TabletPanel2 are two grids which can be shown side by side. PhonePanel1PhonePanel2是我的数据透视控件两页内的两个网格,而TabletPanel1TabletPanel2是两个网格,可以并排显示。 and I use AdaptiveTrigger s to detect page width and switch between these two view methods. 并且我使用AdaptiveTrigger来检测页面宽度并在这两种视图方法之间切换。

However, having two instances of each panel is not a good approach, because those are showing basically the same content. 但是,每个面板有两个实例并不是一个好方法,因为它们显示的内容基本相同。

Is there any better approach to do this? 有没有更好的方法可以做到这一点? Maybe changing Grid's parent when triggers are changing view mode? 也许在触发器更改视图模式时更改Grid的父级? or something better? 还是更好的?

The only way to change the parent that I can think of is to write some code behind that will render them in the new location. 我能想到的更改父级的唯一方法是在其后编写一些代码,以使其在新位置中呈现。 A suitable solution may be to defer the loading of the controls until they are needed. 合适的解决方案可能是推迟加载控件,直到需要它们为止。

x:DeferLoadStrategy=”Lazy“

If you add this property to your controls that are duplicated, they will only be loaded when required. 如果将此属性添加到重复的控件中,则仅在需要时才加载它们。 So on a phone the PC/Tablet view should never be loaded, unless the user is running in Continuum on an external display. 因此,除非用户在外部显示器的Continuum中运行,否则永远不要在PC上加载PC / Tablet视图。

On a PC you may still end up with both loaded (If the user resizes the window) - On a PC there are usually more resources so it's less of a problem there in most cases. 在PC上,你还可以结束了两个装(如果用户调整窗口大小) -在PC上通常有更多的资源,因此在大多数情况下是一个问题较少出现。

I looked into the code behind approach to move the controls to a different parent and I think honestly it's premature to do that when it might not even improve performance. 我研究了将控件移至另一父级的方法背后的代码,但老实说,如果这样做甚至无法提高性能,现在还为时过早。

I have gone for the Defer Load Strategy property and found performance to be fine, and it requires very little code. 我选择了“延迟加载策略”属性,并发现性能很好,并且只需要很少的代码。 My duplicate controls are sharing the same ViewModel so I pretty much just added the property to ensure resources are only used when essential. 我重复的控件共享同一个ViewModel,所以我几乎只添加了该属性以确保仅在必要时使用资源。

There is nothing wrong in your approach. 您的方法没有错。 To reduce memory usage, when adaptive trigger happens for example on the phone, set TabletPanel1 and TabletPanel2 visibility to collapsed. 为了减少内存使用,例如在电话上发生自适应触发时,请将TabletPanel1和TabletPanel2可见性设置为折叠。

If all panels use the same datacontext then leave it as is, if not set datacontext for collapsed controls to null. 如果所有面板都使用相同的数据上下文,则将其保留原样;如果未设置,则将折叠控件的数据上下文设置为null。

您可以将每个面板创建为用户控件,然后只需要定义一次内容,但是每个控件都可以包含在所需的每个布局中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM