【问题标题】:WPF Live Charts - XAML code not linking to C# codeWPF 实时图表 - XAML 代码未链接到 C# 代码
【发布时间】:2020-06-11 01:43:20
【问题描述】:

我正在为实时图表而苦苦挣扎。我正在使用 WPF。

我想构建一个条形图,按腰带颜色显示空手道俱乐部的成员数量。我的学习项目之一。

按照他们的文档: https://lvcharts.net/App/examples/v1/wpf/Basic%20Column

我在 xaml 中遇到错误:

d:DataContext="{d:DesignInstance local:Charts}" '名称空间 "clr-namespace:KarateClub" 中不存在名称 "Charts" '

LabelFormatter="{绑定格式化程序}" '在图表类型中找不到方法格式化程序'

如果我删除了图表显示的 DataContext 代码,但它不使用我的任何值。 我一定想念如何将 XAML 代码链接到 C# 代码......? 我的类/命名空间路径错误吗?

我的 XAML 代码:

<UserControl x:Class="KarateClub.Charts"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:KarateClub"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="1000" d:DataContext="{d:DesignInstance local:Charts}" >

    <Grid Background="White" Width="1000" Height="550">
        <Grid Background="White" Margin="33,45,239,170">
            <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Left">
                <lvc:CartesianChart.AxisX>
                    <lvc:Axis Title="Belts" Labels="{Binding Labels}"></lvc:Axis>
                </lvc:CartesianChart.AxisX>
                <lvc:CartesianChart.AxisY>
                    <lvc:Axis Title="Members" LabelFormatter="{Binding Formatter}"></lvc:Axis>
                </lvc:CartesianChart.AxisY>
            </lvc:CartesianChart>
        </Grid>
    </Grid>
</UserControl>

我的 C# 代码:

using System;
using System.Windows.Controls;
using LiveCharts;
using LiveCharts.Wpf;

namespace KarateClub
{
    public partial class Charts : UserControl
    {

        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public Func<double, string> Formatter { get; set; }


        public Charts()
        {
            InitializeComponent();

            SeriesCollection = new SeriesCollection
            {
                new ColumnSeries
                {
                    Title = "2020",
                    Values = new ChartValues<double> { 1, 5, 3, 5, 7, 3, 9, 2, 3 }
                }
            };

            Labels = new[] { "White", "Yellow", "Orange", "Green", "Blue", "Purple", "Brown", "Red", "Black" };
            Formatter = value => value.ToString("N");

            DataContext = this;

        }
    }
}

【问题讨论】:

    标签: c# wpf xaml livecharts


    【解决方案1】:

    您当前正在使用由设计器生成的Charts 的设计时实例。此实例是自动生成的,不包含任何数据。这是默认行为,由标记扩展 DesignInstanceExtensionIsDesignTimeCreatable 属性控制。默认情况下IsDesignTimeCreatable 返回false,它指示设计者使用反射(绕过任何构造函数)创建一个假实例。

    要使用正确构造的指定类型的设计时实例,您必须将此属性显式设置为true

    <UserControl x:Class="KarateClub.Charts"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:KarateClub"
                 d:DataContext="{d:DesignInstance local:Charts, IsDesignTimeCreatable=True}" >
      ...
    </UserControl>
    

    现在设计器将使用构造函数而不是反射来创建实例。

    显然,这个 Blend 设计时属性没有得到很好的记录。要了解更多信息,请参阅Microsoft Docs: Design-Time Attributes in the Silverlight Designer

    【讨论】:

    • 感谢您的帮助,根据您的建议,我已将 IsDesignTimeCreatable=True 包括在内。但是,datacontect 仍然突出显示错误“名称“Charts”不存在于命名空间“clr-namespace:KarateClub”'
    • 您是否尝试过重建项目?从我看到的命名空间是正确的,一切都应该工作。如果重建不能修复它,请从 XAML 文件中删除完整的命名空间导入,并让 Visual Studio 为你添加它。如果设计器崩溃,重新启动 VS 也可能会有所帮助。
    • 是的,重建修复了它!感谢您的帮助。
    【解决方案2】:

    您所遵循的示例不是d:DataContext="{d:DesignInstance local:Charts}"

    上面写着d:DataContext="{d:DesignInstance local:BasicColumn}"

    【讨论】:

    • 感谢您的帮助。我放图表的原因是“基本列”是他们示例中的类名,而我的类名是 Charts。我尝试更改它,但我得到了同样的错误。 '名称空间“clr-namespace:KarateClub”中不存在名称“BasicColumn”'
    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多