何时在 UserControl 上使用模板化控件? [英] When to use a templated control over a UserControl?

查看:25
本文介绍了何时在 UserControl 上使用模板化控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些有关如何在 WinRT 中创建自定义控件的教程,但我有一个问题.

I was looking some tutorials on how to create custom controls in WinRT, and I have a question.

假设我想创建一个简单的控件,其中包含一些内容,例如左侧有一个图像和右侧有几个 TextBlock 的 Grid.

Let's say I want to create a simple control that contains some stuff, like a Grid with an image on the left and a couple of TextBlocks on the right.

我的意思是,一些简单的事情:

I mean, something simple like:

<Grid Height="100">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.3*"/>
        <ColumnDefinition Width="0.7*"/>
    </Grid.ColumnDefinitions>
    <Image Source"/Assets/someRandomImage.png"/>
    <StackPanel Grid.Column="1"
                VerticalAlignment="Center">
        <TextBlock Text="Some text"
                   Margin="10,0,10,0"
                   FontSize="24"
                   FontWeight="SemiLight"
                   TextTrimming="CharacterEllipsis"/>
        <TextBlock Text="Some random description..."
                   Margin="10,5,10,0"
                   FontSize="18"
                   FontWeight="Light"
                   Foreground="Gray"
                   TextWrapping="Wrap"
                   TextTrimming="CharacterEllipsis"/>
    </StackPanel>
</Grid>

我将使用此内容创建一个 UserControl,这样我就可以在处理其 UI 时在 XAML 设计器中看到它,并且我将在后面的 UserControl 代码中添加所有属性和 DependencyProperties.

I would create a UserControl with this content, so I'd be able to see it in the XAML Designer while I'm working on its UI, and I'd add all the Properties and DependencyProperties in the UserControl code behind.

然后我看到另一种方法是使用模板控件,所以我必须创建一个继承自 Control 类的类,然后使用上面的 XAML 代码作为模板并将其应用于自定义控件和在那里添加所有其余的逻辑.

Then I saw that another approach would be to use a Template control, so I'd have to create a class that inherits from the Control class, then use the above XAML code as a template and apply it to the custom control and add all the rest of the logic there.

当然,我还必须将 x:Name 属性添加到控件内的一些 UIElements 中,以便能够与它们交互,但您明白了.

Of course, I'd also have to add the x:Name property to some those UIElements inside the control to be able to interact with them, but you get the idea.

我想知道,可以使用这两种方法中的任何一种,还是最好使用一种,为什么?此外,我喜欢使用 UserControls,因为我可以在设计器窗口中看到它们,而我无法使用模板来做到这一点,我必须运行该应用程序并创建一个控件实例才能查看它的内容实际上看起来像.

I was wondering, is it ok to use either one of these two methods, or is better to use one in particular, and why? Also, I like using UserControls since I can see them in the Designer window, and instead I wouldn't be able to do that with a Template, I'd have to run the app and create an instance of the control to see what it actually looks like.

谢谢你的帮助,我想我不是唯一一个有这个疑问的人,所以我希望这个问题也能帮助其他人:D

Thanks for your help, I think I'm not the only one with this doubt, so I hope this question will help others as well :D

塞尔吉奥

推荐答案

TLDR

自定义(模板化)控件允许应用使用模板属性来替换控件的内部元素树.如果您不需要/希望您的控件具有重新模板化功能,那么使用 UserControl 会更容易.

A custom (templated) control allows an app to use the Template property to replace the control’s internal element tree. If you don’t need/want your control to have that re-templating feature, then use a UserControl as it’s easier.

用户控件

  • UserControl 使用 Visual Studio 或 Blend 更容易创建,为您提供不错的设计视图支持.
  • 您通常使用它来从多个控件组成应用中的视图.'
  • 它最适合全屏或全窗口视图,或者如果您想将复杂的视图分解为更小的、可能可重用的代码块.
  • 如果您选择采用 MVVM 模式,这种视图通常会得到相应的视图模型的支持.

  • A UserControl is a lot easier to create with Visual Studio or Blend giving you a decent design view support.
  • You typically use it to compose a view in your app from multiple controls.'
  • It works best for full screen or full-window views or if you have complex views that you want to break apart in smaller, possibly reusable code chunks.
  • Such view is often backed with a corresponding view model if you choose to adopt the MVVM pattern.

UserControl 的一个问题是,虽然您可以在应用程序的多个位置重复使用它 - 很难对其在不同位置的外观或行为进行轻微调整您的应用程序,因为它不使用模板,并且 UI 树已加载到构造函数中.

One problem with a UserControl is that while you can reuse it in multiple places in your app - it is difficult to make slight adjustments to the way it looks or behave in different places in your app since it doesn't use templates and the UI tree is loaded in the constructor.

自定义控件

  • 自定义控件 或在某些情况下模板化控件 最适合服务于单一目的的一小块 UI - 它可视化单个特定类型的信息.
  • 模板化控件可以更改其模板以调整特定用例的视觉效果.它允许您在一个应用程序中拥有一个看起来像默认按钮的按钮,在另一个应用程序中拥有一个圆形按钮,而在另一个应用程序中一个仅由图像组成.如果您制作多个应用或想与全世界分享您的出色控制,这将使其更具可重用性.
  • 编写良好的自定义控件通常可在多个应用中重复使用,因为它不依赖于特定应用的业务逻辑.
  • 它通常源自现有的平台控件,例如 ButtonToggleButtonContentControlSliderTextBoxListView 添加或覆盖其逻辑.但在某些情况下,从头开始制作一个是有意义的,子类化虚拟抽象"ControlItemsControlRangeBaseShape 甚至 FrameworkElement(最后两个不是模板化的).
  • 在加载模板时加载模板化控件的可视化树,这可能会在控件的可见性首次从 Collapsed 更改为 Visible 时发生,这允许推迟加载部分 UI 以获得性能改进.
  • 因为控件模板只加载一次,所以它们非常适合在任何 ItemsControl DataTemplate(列表、网格视图等)中使用.如果您要使用 UserControl,您的性能可能真的会受到影响,因为 UserControl XAML 会被一遍又一遍地解析.
  • A custom control or in some cases templated control is best suited for a small chunk of UI that serves a single purpose - it visualizes a single, specific type of information.
  • A templated control can have its template changed to adjust the visuals for particular use case. It allows you to have a button that looks like a default button in one app, a rounded one in another and one made solely up of images in yet another. It makes it more reusable which makes sense if you make more than one app or want to share your awesome control with the world.
  • A well written custom control is usually reusable in more than one app since it doesn't depend on business logic of particular app.
  • It typically derives from an existing platform control, such as Button, ToggleButton, ContentControl, Slider, TextBox or ListView to add to or override its logic. There are cases though when it makes sense to make one from scratch, subclassing "virtually abstract" Control, ItemsControl, RangeBase, Shape or even FrameworkElement (the last two are not templated).
  • The visual tree of a templated control is loaded when the template is loaded which might occur as late as when the control's visibility is first changed from Collapsed to Visible which allows to defer loading parts of your UI to get performance improvements.
  • Because the control template is only loaded once, these are ideal for use inside any ItemsControl DataTemplate (lists, gridviews, etc). If you were to use a UserControl, your performance could really suffer because the UserControl XAML is parsed over and over again.

自定义面板

custom panel 是另一种类型的 UI 元素,允许自定义其子项的布局方式.

A custom panel is yet another type of UI element that allows to customize how it lays out its children.

这篇关于何时在 UserControl 上使用模板化控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆