为什么要使用一个以上用户控件模板化控件? [英] Why use a templated control over a UserControl?

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

问题描述

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



比方说,我想创建一个简单的控制,它包含了一些。的东西,就像在左,一对夫妇在右边的TextBlocks的图像的网格



我的意思是,简单的东西,如:

 <电网HEIGHT =100> 
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =0.3 */>
< ColumnDefinition WIDTH =0.7 */>
< /Grid.ColumnDefinitions>
<图像源/Assets/someRandomImage.png/>
< StackPanel的Grid.Column =1
VerticalAlignment =中心>
< TextBlock的文本=一些文字
保证金=10,0,10,0
字号=24
粗细=SemiLight
TextTrimming =CharacterEllipsis/>
< TextBlock的文本=一些随机的描述......
保证金=10,5,10,0
字号=18
粗细=轻
=前景灰色
TextWrapping =包装
TextTrimming =CharacterEllipsis/>
< / StackPanel的>
< /网格和GT;



我将创建与此内容的用户控件,所以我能看到它在XAML设计师同时我工作的UI,我想补充背后的用户控件代码的所有属性和DependencyProperties。



然后我看到了另一种方法是将使用模板控制,所以我不得不创建一个从Control类继承的类,然后用上面的XAML代码为模板,并将其应用到自定义控制,并添加有逻辑的所有的休息。



当然,我也不得不在x补充:Name属性的一些控件中的UI元素,能够与他们进行互动,但你的想法

我想知道,是不是确定使用这两种方法之一,或者最好使用一个特别的,为什么?
另外,我喜欢使用用户控件,因为我可以在设计器窗口中看到他们,而是我不能够做到这一点与模板,我不得不运行应用程序和控制的创建一个实例看看它实际的样子。



感谢您的帮助,我想我不是唯一一个带着这个疑问,所以我希望这个问题会帮助别人好:D



塞尔吉奥


解决方案

用户控件




  • A 用户控件是轻松了许多与Visual Studio或混合创建给你一个像样的设计视图支持。

  • 您通常使用它来编写你的应用程序从多个控件的观点。

  • 它最适合全屏或全窗口的意见,或者如果您有想要掰开较小,可能可重用的代码块复杂的看法。

  • 这样的观点经常备份与相应的视图模式如果选择采用MVVM模式。


  • 一个问题了用户控件是,当你可以在您的应用程序的多个地方重复使用它 - 这是很难做出细微调整,它的外观或行为在不同的地方在你的应用程序,因为它不使用模板和UI树在构造函数中加载。


  • 这通常是单一的应用程序范围内唯一可重复使用的。



自定义控制




  • A 自定义的控制或在某些情况下模板控制最适合UI的一小块,供应一个目的 - 它可视化一个单一的,特定类型的信息

  • 一个模板控件可以有其模板改变调整为特定用例的视觉效果。它可以让你有一个按钮,看起来像一个应用程序默认的按钮,圆形之一,另外一个在另一个完全由图片。这使得它更具可重用这是有道理的,如果你让多个应用程序或想分享您与世界真棒控制。

  • 一个写得很好的自定义控件通常是在多个应用程序可重复使用因为它不依赖于特定的应用程序的业务逻辑。

  • 它通常从现有平台控制派生,如按钮切换按钮 ContentControl中滑块文本框的ListView 来添加或覆盖它的逻辑。在有些情况下,虽然当它是有道理使一个从无到有,继承虚拟抽象的控制的ItemsControl RangeBase 图形甚至 FrameworkElement的(最后两个都没有模板)。

  • 模板化控件的可视树模板时加载可能迟当控制的知名度首先从改变倒塌发生时加载可见,允许推迟UI的装载部分来获得性能的提升。

  • 因为控制模板只装载一次,这是理想的任何的ItemsControl DataTemplate中(列表,GridView的,等等)内使用。如果你使用一个用户控件,你的表现真的受到影响,因为该用户控件XAML解析一遍又一遍。



自定义面板



A 自定义面板是另一个类型的UI元素,允许自定义如何勾画出它的孩子。


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

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>

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.

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.

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.

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.

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

Sergio

解决方案

UserControl

  • 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.

  • 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.

  • It is typically only reusable within the scope of the single app.

Custom control

  • 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

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

这篇关于为什么要使用一个以上用户控件模板化控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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