- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在网上搜索了很多站点,但是没有找到任何解决方案。声明是,UserControl和CustomControl之间没有性能差异。
但是我有以下测试类X,UserControl,CustomControl和MainWindow:
public class X : INotifyPropertyChanged
{
private string _title;
public string Title
{
get
{
return _title;
}
set
{
if (value == _title)
{
return;
}
_title = value;
OnPropertyChanged("Title");
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<UserControl x:Class="controlperformance.DisplayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid Name="root" Background="LightGray">
<TextBlock Text="{Binding Title}" />
</Grid>
</UserControl>
public class DisplayControl : Control
{
#region Title
public string Title
{
get
{
return (string)GetValue(TitleProperty);
}
set
{
SetValue(TitleProperty, value);
}
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
typeof(string),
typeof(DisplayControl),
new PropertyMetadata(default(string)));
#endregion
static DisplayControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DisplayControl), new FrameworkPropertyMetadata(typeof(DisplayControl)));
}
}
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DisplayControl}">
<Grid Background="white">
<TextBlock Text="{TemplateBinding Title}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public partial class MainWindow : Window
{
Stopwatch sw = new Stopwatch();
public MainWindow()
{
InitializeComponent();
Loaded += OnLoaded;
sw.Start();
ObservableCollection<X> list = new ObservableCollection<X>();
Random r = new Random();
for (int i = 0; i < 50000; i++)
{
list.Add(new X { Title = r.Next().ToString()});
}
itemscontrol.ItemsSource = list;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
sw.Stop();
MessageBox.Show(sw.Elapsed.ToString());
}
}
<ItemsControl Name="itemscontrol">
<ItemsControl.ItemTemplate>
<!--<DataTemplate DataType="{x:Type Controlperformance:X}">
<Controlperformance:DisplayView DataContext="{Binding}" />
</DataTemplate>-->
<DataTemplate DataType="{x:Type Controlperformance:X}">
<Controlperformance:DisplayControl Title="{Binding Title}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
最佳答案
这是一个较晚的答案,但是基本的区别在于用户控件几乎就像一个窗口,因为它具有控件本身,然后可以向其中添加其他控件,例如按钮,网格,文本框等。窗口和用户控件之间的基本区别在于,用户控件可以并且必须在窗口内显示。
另一方面,“自定义控件”仅是一个控件,它可以用于创建具有特定功能的控件,而该功能没有内置控件,或者可以为现有控件(例如按钮,文本框等)提供特定样式以进行匹配您的应用程序的主题。您还可以通过使用自定义控件向现有控件添加其他功能,例如在文本框中添加标签以显示其用途。
加载时间的不同本质上反射(reflect)了用户控件和自定义控件的不同目的,使用用户控件可以加载控件和该控件中的元素,因此加载时间可能会更长。使用自定义控件时,只有控件本身必须加载,因此它的加载时间不会比大多数内置WPF控件花费的时间长,即按钮“自定义控件”的加载时间不会比内置按钮控件的时间长。
关于wpf - 性能UserControl/CustomControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16847772/
我有一个扩展 Canvas 的类在 WPF 中。 这个类放在一个ScrollViewer . 没有传递对 ScrollViewer 的具体引用进入Canvas , 我想找到 ScrollViewer其
我已经在网上搜索了很多站点,但是没有找到任何解决方案。声明是,UserControl和CustomControl之间没有性能差异。 但是我有以下测试类X,UserControl,CustomContr
如何将 ApplicationCommand.Undo 连接到自定义控件中的处理程序? 一些代码片段将是最受欢迎的...... 最佳答案 this.CommandBindings.Add(new Co
我正在尝试制作一个带有一些自定义属性的自定义控件,但是当尝试绑定(bind)到该属性时它似乎不起作用。 这是我的代码 UserProfile.cs using System; using System
在制作自定义控件时,我试图获取鼠标在 Canvas 上的位置,Canvas 是我的自定义控件内的容器。问题是,当我将我的自定义控件放在测试应用程序中时,只是窗口中的一个网格,我得到的始终是鼠标在窗口本
我正在开发一个自定义控件,它在内部订阅 Touch.FrameReported - 一个静态事件。这有可能导致内存泄漏(在某些情况下确实如此)。 这是我目前的解决方案。在 Loaded/Unloade
我在 WPF 中有一个自定义按钮 ColorPickerButton,并为其应用了样式“ColorPickerButtonStyle”,我必须像这样在 xmal 中应用它;
我正在创建一个具有PropertyChangedCallback依赖项属性的WPF CustomControl。在该Callback方法中,我尝试使用GetTemplateChild()方法从OnAp
我正在尝试将 VideoPreview 自定义控件类中的 IntPtr VidHandle 属性绑定(bind)到其 View 模型 (vm:DebugHiResCameraWindowViewMod
我有多个自定义控件(它是一个下面有标签的按钮)添加到 FLP。我试图通过将一个自定义控件拖到另一个控件上来重新排列添加的自定义控件。在一些帮助下,它可以使用简单的按钮,但不能使用自定义控件。另一个问题
我在自定义控件中设置了自定义属性,如下所示: 然后,我的控件用于我的应用程序 xml 布局,这有效: 但是如果我尝试使用数据绑定(bind)来提供值(value),就像这样: 我
我需要在我的 Angular2+ 组件中集成自定义表单控件: export class HeatMapComponent { ... onMapReady(map: L.Map) { le
我写了一个自定义控件。它是一个带有打开 OpenFileDialog 按钮的文本框。 TextBox 的 Text 属性绑定(bind)到我的依赖属性“FileName”。如果用户通过 OpenFil
这是我提出的 here 的后续问题- 控制类有一个依赖属性—— private static readonly DependencyProperty _Color = DependencyPr
我目前在MVVM中工作,并且为自定义样式的searchTextBox控件创建了一个xaml“TargetType”类。 问题是,我想知道如何将这些文件/在何处放置该文件夹,以便我的项目保持整洁。对于R
我有一个 WPF UserControl - SegmentConrol , 表示一行带有一些文本和一个显示方向 (>) 的数组。 因为我必须自定义此控件样式,所以我决定切换到 CustomContr
我有一个 WPF UserControl - SegmentConrol , 表示一行带有一些文本和一个显示方向 (>) 的数组。 因为我必须自定义此控件样式,所以我决定切换到 CustomContr
我不太清楚何时使用 CustomControl 以及何时使用 UserControl。我基本上知道 CustomControl 允许什么(与模板一起使用时更多自定义)。 我想在 WPF 中制作一个“文
从性能 View 用户控件和自定义控件哪个更好?现在我正在使用用户控件,在特定情况下,我正在创建大约 200 个(大约)此控件的不同实例,但它在加载时有点慢,我需要至少等待 20-30 秒才能完成操作
我快把自己逼疯了...我正在尝试为我的控件提供定义自己的颜色的简单功能。 我的控制: 那是我的 xaml:
我是一名优秀的程序员,十分优秀!