WPF .net8 use Dundas chart of System.Windows.Forms

install-package Microsoft.Windows.Compatibility

install-package System.Windows.Forms -version 4.0.0.0

install-package System.Data.SqlClient --version 4.9.0

install-package System.Windows.Forms.DataVisualization -version 1.0.0-prerelease.19212.2

 

 

 

 

 

 

 

The type or namespace name 'Integration' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)


Assembly 'WindowsFormsIntegration' was not found. Verify that you are not missing an assembly reference. Also, verify that your project and all referenced assemblies have been built.

 

The solution is to add  <UseWindowsForms>true</UseWindowsForms> in csproj file as below

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="9.0.6" />
    <PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
    <PackageReference Include="System.Windows.Forms" Version="4.0.0" />
    <PackageReference Include="System.Windows.Forms.DataVisualization" Version="1.0.0-prerelease.19212.2" />
  </ItemGroup>

</Project>

 

 

Save the above csproj file and rebuild it will resolve errors.

 

 

App.xaml.cs(10,32,10,43): error CS0104: 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'System.Windows.Application'

And full namespace System.Windows.Application

public partial class App : System.Windows.Application
{
}

 

 

 

 

 

 

//All code

//xaml
<Window x:Class="WpfApp50.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp50"
        mc:Ignorable="d"
        xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        Title="Dundas Charts in .NET 8"
        WindowState="Maximized">
    <Grid>
        <wfi:WindowsFormsHost Name="windowsFormsHost">
            <!-- Chart will be added programmatically -->
        </wfi:WindowsFormsHost>
    </Grid>
</Window>


//cs
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp50
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Chart dundasChart;

        public MainWindow()
        {
            InitializeComponent();
            InitializeDundasChart();
        }

        private void InitializeDundasChart()
        {
            Random rnd = new Random();
            // Create the WinForms Chart control
            dundasChart = new Chart
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };

            // Add a chart area
            var chartArea = new ChartArea();
            dundasChart.ChartAreas.Add(chartArea);

            // Create and configure series
            var series = new Series
            {
                Name = "DataSeries",
                ChartType = SeriesChartType.Column,
                Color = System.Drawing.Color.SteelBlue,
                //MarkerStyle = MarkerStyle.Circle,
                BorderWidth = 5
            };
            series.ChartType = SeriesChartType.Spline;
            // Add sample data
            for (int i = 0; i < 20; i++)
            {
                series.Points.AddXY((i + 1).ToString(), 10 * Math.Sin(i * 10));
            }


            dundasChart.Series.Add(series);

            // Add chart to WindowsFormsHost
            windowsFormsHost.Child = dundasChart;
        }
    }
}

 

posted @ 2025-07-05 19:55  FredGrit  阅读(23)  评论(0)    收藏  举报