WPF中的OnRender方法 [英] OnRender method in wpf

查看:1873
本文介绍了WPF中的OnRender方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用OnRender方法显示行号的类.现在,我需要在Text Changed事件中从其他类调用该方法.如何触发OnRender方法

i have class which displays line no using OnRender Method. now i need to call that method from other class on Text Changed event. how to trigger OnRender method

public class EditorControl : Control
{
    void EditorTextBoxControl_TextChanged(object sender, TextChangedEventArgs e)
            {
                if(_bIsNewScriptLoaded)
                RaiseEditorTextChangedEvent();
        //need to call here
     }
}
 public class LineMargin : FrameworkElement
{
 protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
 {
	Rect rect = EditorText.GetRectFromCharacterIndex(charIndex);
         	   //this is the height of each line in textEditor.
         	   if (rect.IsEmpty == true)
          	      lineHeight = 0;
          	  else
          	      lineHeight = rect.Height;

         	   for (int i = 0; lineNumber <= _Controller.GetLineCount(); i++, lineNumber++)
           	 {
            	  	  //var foreground = (Brush)GetValue(Control.ForegroundProperty);
                
              	 	 FormattedText text = new FormattedText(
                                                        lineNumber.ToString(CultureInfo.CurrentCulture),
                                                        CultureInfo.GetCultureInfo("en-us"),
                                                        FlowDirection.LeftToRight,
                                                        new Typeface("Verdana"),
                                                        emSize,
                                                        Brushes.SteelBlue);

           	    	 drawingContext.DrawText(text, new Point(renderSize.Width - text.Width,
                                                        (lineHeight * i)  + 2));//2 is a buffer as the number should be displayed little below the rectangle height.
         	   }	
}
}

推荐答案

错误的方法.您无需重新渲染任何内容,因此,无需调用OnRender.如果导致重新渲染,则无论如何都会调用此方法.这是此事件受到虚拟保护而不是公开保护的原因.也许您的错误是重写并从此方法调用代码.

想一想:如果您想在每次渲染时都调用代码,就不会遇到这个问题.因此,即使在某些情况下不进行渲染时,您也需要调用此代码,所以问题是:为什么要将这段代码放在OnRender中?看来您只是找不到更好的地方.现在,由于您没有解释此活动的最终目标,因此您无需再对此部分发表评论,请自己考虑一下.

现在,如果您仍然想采用这种方法,那么一个正式且安全的解决方案是:将 instance 设置为另一个方法(例如F),然后将您执行的所有代码放在此处将方法放入F.现在,从OnRender调用F -这将为您提供您现在拥有的功能.另外,将访问修饰符internal或什至public(从其他程序集访问)添加到F-这将使其可从其他类调用.就这样.

但是,由于上述原因,我认为这没有用.您似乎试图滥用WPF.再说一次,我没什么要补充的,因为您没有分享这项活动的目标.

-SA
Bad approach. You don''t need to re-render anything, so, no need to call OnRender. If you cause re-rendering, this method will be called anyway. It''s the reason why this event is virtual protected, not public. Maybe your mistake is overriding and calling your code from this method.

Think about it: if you wanted to call your code every time rendering happens, you would not have this problem. Consequently, you need to call this code even in some cases when rendering does not happen, so the question is: why putting this code in OnRender? It looks like you simply could not find better place. Now, no more comment on this part, as you did not explain the ultimate goal of this activity — think about it by yourself.

Now, if you still want to follow this approach, a formal and safe resolution is this: make instance another method (let''s say, F) and put there all the code you execute in the method into F. Now, call F from OnRender — this will give your the functionality you have right now. Additionally, add the access modifier internal or even public (access from other assemblies) to F — this will make it callable from other classes. That''s it.

However, I don''t think this is useful by the reasons I explained above. You seemingly trying to abuse WPF. Again, I have nothing useful to add to it as you did not share the goal of this activity.

—SA


,通过调用obj.InvalidateVisual()是WPF Framework中的已定义方法,我们可以将其用于将元素重新呈现到布局中
by calling obj.InvalidateVisual() its a defined method in WPF Framework we can use it for re rendering element to the layout


这篇关于WPF中的OnRender方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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