Hello everybody,this question is continuous to my previous question.
so after that question with some testing, I came to know that, I can add winform user control to WPF window.
But the Problem is in Transfering the data between WPF main window and Winform user control or Problem with painting.
here are source filesMy Main Window
namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private EyeTrackerBrowser _trackerBrowser; private IEyeTracker _connectedTracker; private WindowsFormsControlLibrary1.UserControl1 status; public MainWindow() { InitializeComponent(); loadControl(); System.Windows.Forms.Application.EnableVisualStyles(); _trackerBrowser = new EyeTrackerBrowser(); _trackerBrowser.EyeTrackerFound += EyetrackerFound; } private void loadControl() { status = new UserControl1(); System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); host.Child = status; this.mygrid.Children.Add(host); } private void EyetrackerFound(object sender, EyeTrackerInfoEventArgs e) { if (e.EyeTrackerInfo != null && e.EyeTrackerInfo.ProductId == "XX") { _connectedTracker = e.EyeTrackerInfo.Factory.CreateEyeTracker(); _connectedTracker.StartTracking(); _connectedTracker.GazeDataReceived += _connectedTracker_GazeDataReceived; } } private void WindowLoaded(object sender, RoutedEventArgs e) { _trackerBrowser.StartBrowsing(); } private void _connectedTracker_GazeDataReceived(object sender, GazeDataEventArgs e) { var gd = e.GazeDataItem; status.OnGazeData(gd); } private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e) { // Shutdown browser service _trackerBrowser.StopBrowsing(); if (_connectedTracker != null) { _connectedTracker.Dispose(); } } } }
namespace WindowsFormsControlLibrary1 { public partial class UserControl1: UserControl { private Point3D _leftEye; private Point3D _rightEye; private int _leftValidity; private int _rightValidity; private float _diameter; private SolidBrush _brush; private SolidBrush _eyeBrush; private Queue<IGazeDataItem> _dataHistory; private static int HistorySize = 30; private static int BarHeight = 25; private static int EyeRadius = 8; public UserControl1() { InitializeComponent(); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); _dataHistory = new Queue<IGazeDataItem>(HistorySize); _brush = new SolidBrush(Color.Red); _eyeBrush = new SolidBrush(Color.White); _leftValidity = 4; _rightValidity = 4; } public void OnGazeData(IGazeDataItem gd) { // Add data to history _dataHistory.Enqueue(gd); // Remove history item if necessary while (_dataHistory.Count > HistorySize) { _dataHistory.Dequeue(); } _leftValidity = gd.LeftValidity; _rightValidity = gd.RightValidity; _diameter = gd.RightPupilDiameter; _leftEye = gd.LeftEyePosition3DRelative; _rightEye = gd.RightEyePosition3DRelative; Invalidate(); } public void Clear() { _dataHistory.Clear(); _leftValidity = 0; _rightValidity = 0; _leftEye = new Point3D(); _rightEye = new Point3D(); Invalidate(); } private SolidBrush Brush { get { if (_leftValidity == 4 && _rightValidity == 4) { _brush.Color = Color.Red; } else if (_leftValidity == 0 && _rightValidity == 0) { _brush.Color = Color.Lime; } else if (_leftValidity == 2 && _rightValidity == 2) { _brush.Color = Color.Orange; } else { _brush.Color = Color.Yellow; } return _brush; } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // Compute status bar color _brush.Color = ComputeStatusColor(); // Draw bottom bar e.Graphics.FillRectangle(_brush, new Rectangle(0, Height - BarHeight, Width, BarHeight)); // Draw eyes if (_leftValidity <= 2) { RectangleF r = new RectangleF((float)((1.0 - _leftEye.X) * Width - EyeRadius), (float)(_leftEye.Y * Height - EyeRadius), 2 * EyeRadius, 2 * EyeRadius); e.Graphics.FillEllipse(_eyeBrush, r); } if (_rightValidity <= 2) { RectangleF r = new RectangleF((float)((1 - _rightEye.X) * Width - EyeRadius), (float)(_rightEye.Y * Height - EyeRadius), 2 * EyeRadius, 2 * EyeRadius); e.Graphics.FillEllipse(_eyeBrush, r); } } private Color ComputeStatusColor() { if (!Enabled) return Color.Gray; int quality = 0; int count = 0; foreach (IGazeDataItem item in _dataHistory) { if (item.LeftValidity == 4 && item.RightValidity == 4) { quality += 0; } else if (item.LeftValidity == 0 && item.RightValidity == 0) { quality += 2; } else { quality++; } count++; } float q = (count == 0 ? 0 : quality / (2F * count)); if (q > 0.8) { return Color.Lime; } if (q < 0.1) { return Color.Red; } return Color.Red; } } }
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" xmlns:wf="clr-namespace:System.Windows.Forms.Integration;assembly=System.Windows.Forms.Integration" xmlns:controls="clr-namespace:WindowsFormsControlLibrary1;assembly=WindowsFormsControlLibrary1" AllowsTransparency="True" Title="MainWindow" Height="350" Width="525" Loaded="WindowLoaded" Closing="WindowClosing"><Grid x:Name="mygrid" ClipToBounds="True" ></Grid></Window>
in this case, my IntelliSense can not recognize the usercontrol. So I tried to add it programmitically.
Still, it did not work., It showing Xaml parse exception at the starting.
Could someone help me with this?