C# WPF 在用户控件中绘制 OxyPlot
C# WPF Draw OxyPlot in UserControl
我想在用户控件中使用 OxyPlot 绘制坐标系。不幸的是,有两个错误。我不知道他们为什么站在那里。当按下 MainWindow 应用程序中的某个按钮时,此 UserControl 随后会出现在我的 MainWindow 中。
谁能告诉我错误在哪里并修复它?
这适用于另一个应用程序。这也不是 UserControl,而是 MainWindow。
错误:
"UCScreen" does not contain a definition for "Title", and you could
not find a title extension method that accepts a first UCVoucher type
argument
The name "creator" does not exist in the current context.
代码中的错误我已经用注释标出来了
用户控件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OxyPlot;
using OxyPlot.Series;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Globalization;
namespace Vorschau
{
/// <summary>
/// Interaktionslogik für UCVorschau.xaml
/// </summary>
public partial class UCVorschau : UserControl
{
public UCVorschau()
{
InitializeComponent();
DataContext = this; //Here is an error
this.Title = "Vorschaubild";
}
public IList<DataPoint> Points { get; private set; }
/// <summary>
/// Einstelungs-Fenster wird geöffnet
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//btEinstellung
private void btGenerate_Click(object sender, RoutedEventArgs e)
{
DateTime startZeit = DateTime.Now;
Cursor = Cursors.Wait;
double zufallszahlX;
double zufallszahlY;
double XMax = 10;
double XMin = 0;
double YMax = 10;
double YMin = 0;
// Zur Erstellung des Seeds
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
String u = h.ToString() + m.ToString() + s.ToString();
int iu = Int32.Parse(u);
Random zufall = new Random(iu);
Console.WriteLine("______________");
CultureInfo en = new CultureInfo("en-US", false); // Damit ein Punkt ist anstatt ein Komma
DataContext = this;
this.Points = new List<DataPoint>();
System.IO.File.WriteAllText(((Environment.CurrentDirectory + @"\files\koordinaten.txt")), string.Empty);
using (var fileStream = new FileStream(String.Format(Environment.CurrentDirectory + @"\files\koordinaten.txt"), FileMode.OpenOrCreate))
using (var streamWriter = new StreamWriter(fileStream))
{
for (int i = 1; i <= 10; i++)
{
zufallszahlX = zufall.NextDouble() * (XMax - XMin) + XMin;
zufallszahlY = zufall.NextDouble() * (YMax - YMin) + YMin;
//Console.WriteLine("( " + zufallszahlX + " / " + zufallszahlY + " )" + " |" + i);
streamWriter.WriteLine("( " + zufallszahlX.ToString(en.NumberFormat) + " / " + zufallszahlY.ToString(en.NumberFormat) + " )" + " |" + i);
creator.addPoint(zufallszahlX, zufallszahlY); //Here is an error
Points.Add(new DataPoint(zufallszahlX, zufallszahlY));
}
ls.ItemsSource = Points;
}
Cursor = Cursors.Arrow;
DateTime endZeit = DateTime.Now;
TimeSpan gemesseneZeit = endZeit - startZeit;
// statusbar.Text = "Gemessen Zeit für den Durchlauf: " + gemesseneZeit;
}
}
}
<UserControl x:Class="Vorschau.UCVorschau"
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:Vorschau"
xmlns:oxy="http://oxyplot.org/wpf"
mc:Ignorable="d" Height="461" Width="624">
<Canvas HorizontalAlignment="Left" Height="461" VerticalAlignment="Top" Width="624">
<Button x:Name="btGenerate" Content="Generiere Koordinaten" Canvas.Left="25" Canvas.Top="409" Click="btGenerate_Click"/>
<oxy:Plot x:Name="oxyPlot" Title="{Binding Title}" Height="245" Canvas.Left="298" Canvas.Top="32" Width="273" Background="#FFD1CFD0">
<oxy:Plot.Series>
<oxy:LineSeries x:Name="ls" ItemsSource="{Binding Points}" LineStyle="None" MarkerType="Square" MarkerSize="5" MarkerFill="Black"/>
</oxy:Plot.Series>
</oxy:Plot>
</Canvas>
</UserControl>
UserControl
class 没有 Title
属性 但 Plot
class 有。你可能想设置这个:
public UCVorschau()
{
InitializeComponent();
DataContext = this; //Here is an error
oxyPlot.Title = "Vorschaubild";
}
或者您应该在 UserControl
class 中添加一个 Title
属性:
public UCVorschau()
{
InitializeComponent();
DataContext = this; //Here is an error
this.Title = "Vorschaubild";
}
public string Title { get; set; }
那么您在 XAML 标记中定义的绑定应该可以工作。
关于 "creator" 我不知道那是什么。由于您要将 DataPoint
添加到 Points
集合中,我想您最好删除这一行。或者您可以将 DataPoint
直接添加到您在 XAML 标记中定义的 "ls" LineSeries
:
ls.addPoint(zufallszahlX, zufallszahlY);
我想在用户控件中使用 OxyPlot 绘制坐标系。不幸的是,有两个错误。我不知道他们为什么站在那里。当按下 MainWindow 应用程序中的某个按钮时,此 UserControl 随后会出现在我的 MainWindow 中。
谁能告诉我错误在哪里并修复它?
这适用于另一个应用程序。这也不是 UserControl,而是 MainWindow。
错误:
"UCScreen" does not contain a definition for "Title", and you could not find a title extension method that accepts a first UCVoucher type argument
The name "creator" does not exist in the current context.
代码中的错误我已经用注释标出来了
用户控件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OxyPlot;
using OxyPlot.Series;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Globalization;
namespace Vorschau
{
/// <summary>
/// Interaktionslogik für UCVorschau.xaml
/// </summary>
public partial class UCVorschau : UserControl
{
public UCVorschau()
{
InitializeComponent();
DataContext = this; //Here is an error
this.Title = "Vorschaubild";
}
public IList<DataPoint> Points { get; private set; }
/// <summary>
/// Einstelungs-Fenster wird geöffnet
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//btEinstellung
private void btGenerate_Click(object sender, RoutedEventArgs e)
{
DateTime startZeit = DateTime.Now;
Cursor = Cursors.Wait;
double zufallszahlX;
double zufallszahlY;
double XMax = 10;
double XMin = 0;
double YMax = 10;
double YMin = 0;
// Zur Erstellung des Seeds
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
String u = h.ToString() + m.ToString() + s.ToString();
int iu = Int32.Parse(u);
Random zufall = new Random(iu);
Console.WriteLine("______________");
CultureInfo en = new CultureInfo("en-US", false); // Damit ein Punkt ist anstatt ein Komma
DataContext = this;
this.Points = new List<DataPoint>();
System.IO.File.WriteAllText(((Environment.CurrentDirectory + @"\files\koordinaten.txt")), string.Empty);
using (var fileStream = new FileStream(String.Format(Environment.CurrentDirectory + @"\files\koordinaten.txt"), FileMode.OpenOrCreate))
using (var streamWriter = new StreamWriter(fileStream))
{
for (int i = 1; i <= 10; i++)
{
zufallszahlX = zufall.NextDouble() * (XMax - XMin) + XMin;
zufallszahlY = zufall.NextDouble() * (YMax - YMin) + YMin;
//Console.WriteLine("( " + zufallszahlX + " / " + zufallszahlY + " )" + " |" + i);
streamWriter.WriteLine("( " + zufallszahlX.ToString(en.NumberFormat) + " / " + zufallszahlY.ToString(en.NumberFormat) + " )" + " |" + i);
creator.addPoint(zufallszahlX, zufallszahlY); //Here is an error
Points.Add(new DataPoint(zufallszahlX, zufallszahlY));
}
ls.ItemsSource = Points;
}
Cursor = Cursors.Arrow;
DateTime endZeit = DateTime.Now;
TimeSpan gemesseneZeit = endZeit - startZeit;
// statusbar.Text = "Gemessen Zeit für den Durchlauf: " + gemesseneZeit;
}
}
}
<UserControl x:Class="Vorschau.UCVorschau"
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:Vorschau"
xmlns:oxy="http://oxyplot.org/wpf"
mc:Ignorable="d" Height="461" Width="624">
<Canvas HorizontalAlignment="Left" Height="461" VerticalAlignment="Top" Width="624">
<Button x:Name="btGenerate" Content="Generiere Koordinaten" Canvas.Left="25" Canvas.Top="409" Click="btGenerate_Click"/>
<oxy:Plot x:Name="oxyPlot" Title="{Binding Title}" Height="245" Canvas.Left="298" Canvas.Top="32" Width="273" Background="#FFD1CFD0">
<oxy:Plot.Series>
<oxy:LineSeries x:Name="ls" ItemsSource="{Binding Points}" LineStyle="None" MarkerType="Square" MarkerSize="5" MarkerFill="Black"/>
</oxy:Plot.Series>
</oxy:Plot>
</Canvas>
</UserControl>
UserControl
class 没有 Title
属性 但 Plot
class 有。你可能想设置这个:
public UCVorschau()
{
InitializeComponent();
DataContext = this; //Here is an error
oxyPlot.Title = "Vorschaubild";
}
或者您应该在 UserControl
class 中添加一个 Title
属性:
public UCVorschau()
{
InitializeComponent();
DataContext = this; //Here is an error
this.Title = "Vorschaubild";
}
public string Title { get; set; }
那么您在 XAML 标记中定义的绑定应该可以工作。
关于 "creator" 我不知道那是什么。由于您要将 DataPoint
添加到 Points
集合中,我想您最好删除这一行。或者您可以将 DataPoint
直接添加到您在 XAML 标记中定义的 "ls" LineSeries
:
ls.addPoint(zufallszahlX, zufallszahlY);