问题描述
我基本上有。事情是,我希望屏幕上有一定数量的矩形,具体取决于屏幕尺寸/分辨率。现在关于这个问题的第一个评论是在基于XAML的技术中创建过程代码中的UI元素是令人沮丧的,麻烦的,可能是一个坏主意,而且显然是错误的。
有没有另一种方法可以实现我的目标?
我能想到的一种方法是拥有至少3x3的矩形,但添加UI代码6x3,并使最后3行可见性崩溃。在应用程序加载中,我可以弄清楚我是否可以放置更多行,并选择性地使它们可见。我仍然想知道是否有更清晰的方法。
我不会在这里粘贴我的代码,因为它与链接问题非常相似,但我会问题,这有助于澄清事情。
我理解XAML应用程序应声明的一般情感尽可能在XAML中使用UI。但我也相信可能过于教条化。如果真的有一个场景,其中设计时候人们在设计时选择的UI元素的布局是未知的,那么为什么不在运行时根据这些信息填充UI?
除了一些明确写出并且引人注目的观点,详细说明为什么UI元素的运行时创建实际上很糟糕,我会在适当的时候继续 。我会很乐意设计尽可能多的用户界面,但是如果有些部分只依赖于运行时已知的信息,那么我将编写代码来实现这一点。
一个人可能会遇到很多非常糟糕的代码和维护方面的麻烦,试图让一个特定的设计目标变成一些狭隘的,规定性的教条。 我继续并发布了你已经链接到的问题的答案。我不知道这是否是你需要的东西,但如果是这样的话,你可以看看。在我看来,这里的主要问题是真的在运行时配置我的用户界面真的很错误吗?我的回答是,正如你所看到的,不,不一定。
I'm basically having the same problem as this guy. The thing is, I want there to be a certain number of rectangles on the screen, depending on the screen size/resolution. Now the first comment on that question says "creating UI elements in procedural code in XAML-based technologies is discouraged, cumbersome, probably a bad idea, and just plain wrong".
So is there another way in which I can achieve my goal?
One way I can think of would be to have a minimum of 3x3 rectangles, but add UI code for 6x3 and make the last 3 rows visibility collapsed. In app load, I could figure out if I could fit more rows, and selectively make them visible. I'd still like to know if there is a cleaner approach.
I won't paste my code here since it's very similar to the linked question, but I will if asked, and it helps to clarify things a bit.
I understand the general sentiment that a XAML app should declare as much of the UI as possible in the XAML. But I also believe it's possible to be too dogmatic about things. If one genuinely has a scenario where the layout of UI elements one would otherwise select during design-time is unknown until run-time, then why not populate the UI at run-time based on that information?
Barring some clearly-written and compelling argument detailing why run-time creation of UI elements is actually bad, I'm going to keep on doing it when appropriate. I will happily design as much of the UI in advance, but if there are parts that depend on information known only at run-time, I will write code to do that.
A person can wind up with a lot of really awful code and maintenance hassles trying to shoe-horn a specific design goal into some constrictive, prescriptive dogma.
I went ahead and posted an answer to the question you've linked to. I don't know if that's actually something you needed, but if so you can look there. It seems to me that the main question here is "is it really so wrong to configure my UI at run-time?" and my answer to that is, as you can see, "no, not necessarily".
这篇关于我可以在运行时决定网格中的矩形数量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!