Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在另一个框架元素中绘制框架元素 公共类VisualCue:FrameworkElement { 公共列表指示符{get;set;} 公共可视化工具() { this.Indicators=新列表(); } 受保护的重写int VisualChildrenCount { 获取{返回this.Indicators.Count;} } 受保护的重写Visual GetVisualChild(int索引) { 返回此。指标[索引]; } } 公共类指示符:FrameworkElement { 受保护的覆盖void OnRender(DrawingContext上下文) { 上下文。抽屉(画笔。红色, 新笔(黑色,2),新点(0,0),10,10); base.OnRender(上下文); } }_Wpf_Drawing_Frameworkelement - Fatal编程技术网

Wpf 在另一个框架元素中绘制框架元素 公共类VisualCue:FrameworkElement { 公共列表指示符{get;set;} 公共可视化工具() { this.Indicators=新列表(); } 受保护的重写int VisualChildrenCount { 获取{返回this.Indicators.Count;} } 受保护的重写Visual GetVisualChild(int索引) { 返回此。指标[索引]; } } 公共类指示符:FrameworkElement { 受保护的覆盖void OnRender(DrawingContext上下文) { 上下文。抽屉(画笔。红色, 新笔(黑色,2),新点(0,0),10,10); base.OnRender(上下文); } }

Wpf 在另一个框架元素中绘制框架元素 公共类VisualCue:FrameworkElement { 公共列表指示符{get;set;} 公共可视化工具() { this.Indicators=新列表(); } 受保护的重写int VisualChildrenCount { 获取{返回this.Indicators.Count;} } 受保护的重写Visual GetVisualChild(int索引) { 返回此。指标[索引]; } } 公共类指示符:FrameworkElement { 受保护的覆盖void OnRender(DrawingContext上下文) { 上下文。抽屉(画笔。红色, 新笔(黑色,2),新点(0,0),10,10); base.OnRender(上下文); } },wpf,drawing,frameworkelement,Wpf,Drawing,Frameworkelement,在XAML中: public class VisualCue : FrameworkElement { public List<Indicator> Indicators { get; set; } public VisualCue() { this.Indicators = new List<Indicator>(); } protected override int VisualChildrenCount

在XAML中:

public class VisualCue : FrameworkElement
{
    public List<Indicator> Indicators { get; set; }

    public VisualCue()
    {
        this.Indicators = new List<Indicator>();
    }

    protected override int VisualChildrenCount
    {
        get { return this.Indicators.Count; }
    }

    protected override Visual GetVisualChild(int index)
    {
        return this.Indicators[index];
    }
}

public class Indicator : FrameworkElement
{
    protected override void OnRender(DrawingContext context)
    {
        context.DrawEllipse(Brushes.Red, 
            new Pen(Brushes.Black, 2), new Point(0, 0), 10, 10);

        base.OnRender(context);
    }
}


但指标没有被画出来。我缺少什么?

至少您需要在VisualCue中覆盖ArrangeOverride:

<local:VisualCue x:Name="visualCue">
 <local:VisualCue.Indicators>
  <local:Indicator />
 </local:VisualCue.Indicators>
</local:VisualCue>
传递给指示器子级的Rect中的值取决于布局需要。您还应该覆盖MeasureOverride


至少您需要在VisualCue中覆盖ArrangeOverride:

<local:VisualCue x:Name="visualCue">
 <local:VisualCue.Indicators>
  <local:Indicator />
 </local:VisualCue.Indicators>
</local:VisualCue>
传递给指示器子级的Rect中的值取决于布局需要。您还应该覆盖MeasureOverride


您是否尝试过实现
LogicalChildren
方法

protected override Size ArrangeOverride(Size finalSize)
{
    foreach (Indicator indicator in Indicators)
    {
        indicator.Arrange(new Rect(0, 0, 10, 10));
    }

    return finalSize;
}
当我开发我的第一个“框架元素”时,我遇到了各种各样的麻烦


经过广泛的研究后,我基本上发现有许多方法可以覆盖,以使其与
WPF

配合良好。您是否尝试过实现
LogicalChildren
方法

protected override Size ArrangeOverride(Size finalSize)
{
    foreach (Indicator indicator in Indicators)
    {
        indicator.Arrange(new Rect(0, 0, 10, 10));
    }

    return finalSize;
}
当我开发我的第一个“框架元素”时,我遇到了各种各样的麻烦


经过广泛的研究后,我基本上发现有许多方法可以覆盖,以便它能够很好地与WPF配合使用。是的,值得查看WPF中的渲染细节,如scott所说的度量和安排步骤。。是的,值得在wpf中查看渲染细节,如scott所说的度量和安排步骤。。