我没有在Google或Stack Overflow上找到任何有用的东西,或者根本没有答案(或者也许我只是不知道要搜索什么)-我能找到的最接近的问题是:在性能低下的原因WPF
但是我想在这个简单的程序中解决这个滞后的原因,也许我只是做的不好。
我正在UI元素的OnRender()中渲染大约2000个点,并且它们之间有线,实际上是在创建线图。没关系,但我想使用MouseMove平移图形。效果很好,但问题是LAG。每当我用鼠标拖动时,我都会期望平滑的更新,我认为重绘2000点并在它们之间有线是在i5 CPU上漫步。但是,即使在我家里的笔记本电脑上分辨率很低时,它的速度也非常慢。因此,我检查了性能分析器。OnRender()函数几乎不使用任何CPU。
事实证明,正在更改和使用大量CPU的是Layout。
“版式”花费最多的时间来完成
现在,我听到了术语“视觉树”的话题,但是在这个简单的项目中几乎没有任何视觉效果。只是主窗口上的UI元素。而且它使用的是绘图上下文,我以为绘图上下文就像位图一样绘制,或者它是使用自己的事件/命中框等绘制UI元素的?因为我想要的只是让UIElement像图像一样工作,而且还要处理鼠标事件,所以我可以拖动整个对象(或使用鼠标滚轮缩放)。
所以问题:
来源如下:
.cs文件
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
namespace SlowChart
{
public class SlowChartClass : UIElement
{
List<Point> points = new List<Point>();
double XAxis_Width = 2000;
double XAxis_LeftMost = 0;
double YAxis_Height = 300;
double YAxis_Lowest = -150;
Point mousePoint;
double XAxis_LeftMostPan = 0;
double YAxis_LowestPan = 0;
public SlowChartClass()
{
for (int i = 0; i < 2000; i++)
{
double cos = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成BitmapFrame
基于UIElement
. 这是我的功能:
private BitmapFrame RenderToBitmap2()
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
VisualBrush aVisualBrush = new VisualBrush(GenerateTestStackPanel());
drawingContext.DrawRectangle(aVisualBrush, new Pen(Brushes.Green, 2), new Rect(new Size(150, 150)));
drawingContext.Close();
renderBitmap.Render(drawingVisual);
return BitmapFrame.Create(renderBitmap);
}
Run Code Online (Sandbox Code Playgroud)
出于测试和调试目的,我使用了一个附加函数来创建一个简单的 StackFrame,它应该创建一个可以表示的有效视觉元素:
private StackPanel GenerateTestStackPanel()
{
// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();
myEllipse.Fill = Brushes.Green;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.Black;
// Set the width and height of …
Run Code Online (Sandbox Code Playgroud) 以下面的窗口布局为例:Grid
定义了一个元素。它有 3 行。每一行都有一个 Button 元素。如何获取RowDefinition
它所属的 Button的对象?谢谢。
注意:通过调用Grid.GetRow(Button element)
,我获得了Grid.Row
该 Button 元素的属性。我不需要那个——而是我需要实际的RowDefinition
对象。
我需要确定鼠标光标是否在给定的上方UIElement
.即使另一个元素放在它上面,该方法也应该有效(因为它有一个更大的Zindex).
我尝试使用MouseEnter/Leave事件,但如果元素不是最顶层的元素,则不会触发mouseenter.
有任何想法吗?
在WPF中,我想TabControl
在我的代码中获取a的角落位置,因此我可以在显示时相应地设置ToolWindow的位置.
我如何获得给定的位置UIElement
?
buildingInfoWindow = new BuildingInfoWindow(); // BuildingWindow : System.Windows.Window
buildingInfoWindow.Owner = this;
//buildingInfoWindow.Left = ?; // relative X coordinate of my TabControl
//buildingInfoWindow.Top = ?; // relative Y coordinate of my TabControl
Run Code Online (Sandbox Code Playgroud) 我在屏幕上有几个UI元素(以编程方式),并且需要一种有效的方式为每个子视图提供z-index设置,因此我可以将某些元素叠加到其他元素上.
有人有解决方案吗?
出什么问题了:
let (x:UIElement) = upcast new TextBlock()
Run Code Online (Sandbox Code Playgroud)
错误是:此处需要类型为"System.ComponentModel.ISupportInitialize"且不可用.您必须添加对程序集'System,Version = 4.0.0 ....'的引用.
TextBlock
是UIElement
... 的子类型
请注意,执行错误消息所说明的内容确实可以解决问题,但为什么有必要做一些基本的转发?
我只看到了这个问题的代码解决方案.我正在寻找一个基于XAML的解决方案,我很安静,你不能把注意力集中在XAML中的UI元素上.
我发现这个旧的MSDN帖子:http://social.msdn.microsoft.com/Forums/en/wpf/thread/09dc837d-4485-4966-b45b-266727bbb90c
有我寻求的解决方案(这只是我猜的WPF)
<Grid FocusManager.FocusedElement="{Binding ElementName=listBox1}">
Run Code Online (Sandbox Code Playgroud)
是否将焦点设置为Silverlight 4 XAML中的TextBox/ListBox ?
UIElement
s 上的DependencyProperties 不支持数据绑定(你得到类似的东西:
"找不到管理FrameworkElement ..")
.如果您尝试,则会收到错误,因为WPF无法解析DataContext.据我所知,如果继承FrameworkElement或Freezable,你将获得绑定支持,但在这种情况下,我不能简单地更改基类.有没有办法让UIElement支持数据绑定?
我试图将DataContext属性添加到UIElement类,如下所示:
FrameworkElement.DataContextProperty.AddOwner(typeof(Bitmap), new
FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
Run Code Online (Sandbox Code Playgroud)
我还尝试通过在绑定表达式中指定"ElementName"来绑定,但我仍然无法解析父DataContext(我认为ElementName显式绑定只会消除解析DataContext的需要).
这是绑定.有问题的类称为"位图".
<Utils:Bitmap Source="{Binding Path=Icon}" />
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" MaxWidth="90" Text="{Binding Path=Name}" TextWrapping="Wrap" TextAlignment="Center"/>
Run Code Online (Sandbox Code Playgroud)
文本块绑定按预期工作,第一个绑定不工作.绑定的viewmodel具有两个属性(我之前绑定到Image类并且它工作).
位图类可以在这个博客上找到:http://blogs.msdn.com/b/dwayneneed/archive/2007/10/05/blurry-bitmaps.aspx
通过一些扩展的绑定诊断,我得到了这个输出:
System.Windows.Data Warning: 65 : BindingExpression (hash=14926099): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Icon; DataItem=null; target element is 'Bitmap' (HashCode=117163); target property is 'Source' (type 'BitmapSource')
System.Windows.Data Warning: 63 : BindingExpression (hash=6195855): Resolving source (last chance)
System.Windows.Data Warning: …
Run Code Online (Sandbox Code Playgroud) 假设我想创建一个直接继承的类,UIElement
并且能够包含一个或多个[外部添加] UIElement
作为子项 - 比如Panel
s和其他容器控件.很容易让班级UIElement
以某种形式或其他形式收集s,但我如何让它们与我的班级一起展示/呈现?
我认为必须将它们添加到可视化树为我自己的孩子,UIElement
以某种方式(或可能的话,手动使他们通过LA去VisualTreeHelper.GetDrawing
,并用做OnRender
的DrawingContext
?但似乎笨拙).
我不是想知道我可以-或者应该-从多个现成的控制,继承一样FrameworkElement
,Panel
,ContentControl
等(如果有的话,我想知道如何,他们正在实施外部添加子元素的显示/渲染,如适用) .
我有理由希望在层次结构中尽可能高,所以请不要给我任何关于为什么XAML/WPF框架"合规"等是好事的讲座等.
uielement ×10
wpf ×6
c# ×4
xaml ×2
.net ×1
bitmap ×1
data-binding ×1
datacontext ×1
drawing ×1
element ×1
f# ×1
grid ×1
iphone ×1
mouseover ×1
objective-c ×1
position ×1
rendering ×1
silverlight ×1
uiimageview ×1
uilabel ×1