WPF 实时图表 - XAML 代码未链接到 C# 代码
WPF Live Charts - XAML code not linking to C# code
我正在为实时图表苦苦挣扎。我正在使用 WPF。
我想构建一个条形图,按腰带颜色显示空手道俱乐部的成员数量。我的学习项目之一。
按照他们的文档:
https://lvcharts.net/App/examples/v1/wpf/Basic%20Column
我在 xaml 中遇到错误:
d:DataContext="{d:DesignInstance local:Charts}"
'The name "Charts" does not exist in namespace "clr-namespace:KarateClub" '
和
LabelFormatter="{绑定格式器}"
'Method Formatter not found in type Charts'
如果我删除图表显示的 DataContext 代码,但它不使用我的任何值。
我一定是错过了如何将 link XAML 代码转换为 C# 代码...?
我弄错了 class/namespace 路径吗?
我的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;
}
}
}
您所遵循的示例并未说明 d:DataContext="{d:DesignInstance local:Charts}"
。
它说 d:DataContext="{d:DesignInstance local:BasicColumn}"
。
您目前正在使用 Charts
的设计时实例,它是由设计者生成的。此实例是自动生成的,不包含任何数据。这是默认行为,由标记扩展 DesignInstanceExtension
的 IsDesignTimeCreatable
属性 控制。默认情况下 IsDesignTimeCreatable
returns 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
我正在为实时图表苦苦挣扎。我正在使用 WPF。
我想构建一个条形图,按腰带颜色显示空手道俱乐部的成员数量。我的学习项目之一。
按照他们的文档: https://lvcharts.net/App/examples/v1/wpf/Basic%20Column
我在 xaml 中遇到错误:
d:DataContext="{d:DesignInstance local:Charts}" 'The name "Charts" does not exist in namespace "clr-namespace:KarateClub" '
和
LabelFormatter="{绑定格式器}" 'Method Formatter not found in type Charts'
如果我删除图表显示的 DataContext 代码,但它不使用我的任何值。 我一定是错过了如何将 link XAML 代码转换为 C# 代码...? 我弄错了 class/namespace 路径吗?
我的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;
}
}
}
您所遵循的示例并未说明 d:DataContext="{d:DesignInstance local:Charts}"
。
它说 d:DataContext="{d:DesignInstance local:BasicColumn}"
。
您目前正在使用 Charts
的设计时实例,它是由设计者生成的。此实例是自动生成的,不包含任何数据。这是默认行为,由标记扩展 DesignInstanceExtension
的 IsDesignTimeCreatable
属性 控制。默认情况下 IsDesignTimeCreatable
returns 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