【问题标题】:ILScene does not update in WindowsFormsHostILScene 不会在 WindowsFormsHost 中更新
【发布时间】:2014-07-28 00:08:22
【问题描述】:

我正在尝试使用 ILLinePlot 在使用 WindowsFormsHost 的 WPF 应用程序中绘制实时数据。我已经根据这两个 SO 问题开始: ILScene in WindowsFormsHost & ILNumeric continuous rendering plots.

我的代码是:

XAML:

<Window x:Class="Pulse_Generator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:forms="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        Title="MainWindow" Height="350" Width="525"
        Loaded="ILView_OnLoaded">
    <Grid Name="grid1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <forms:WindowsFormsHost x:Name="WindowsFormsHost" Margin="5" />
    </Grid>
</Window>

C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private ILPanel ilPanel;

    private void IlPanelOnLoad(object sender, EventArgs eventArgs)
    {
        using (ILScope.Enter())
        {
            // generate some dummy data
            int N = 50000;
            ILArray<float> x = ILMath.vec<float>(0, N-1);
            ILArray<float> y = ILMath.tosingle(ILMath.rand(N));
            ILArray<float> A = ILMath.zeros<float>(2, N);
            A["0;:"] = x;
            A["1;:"] = y;

            ilPanel.Scene.Add(new ILPlotCube(){
                new ILLinePlot(A)
            });

            ilPanel.BeginRenderFrame += (o, args) =>
            {
                using (ILScope.Enter())
                {
                    var linePlot = ilPanel.Scene.First<ILLinePlot>();
                    var posBuffer = linePlot.Line.Positions;
                    ILArray<float> data = posBuffer.Storage;
                    // update the plot with some new dummy data
                    data["1;:"] = ILMath.tosingle(ILMath.randn(1, posBuffer.DataCount));
                    linePlot.Line.Positions.Update(data);
                    ilPanel.Scene.First<ILPlotCube>().Reset();
                    linePlot.Configure();
                }
            };
            // start running
            ilPanel.Clock.TimeMilliseconds = 15;
            ilPanel.Clock.Running = true;
        }
    }

    private void ILView_OnLoaded(object sender, RoutedEventArgs e)
    {
        ilPanel = new ILPanel();
        ilPanel.Load += IlPanelOnLoad;
        WindowsFormsHost.Child = ilPanel;
    }
}

问题在于,只有在绘图上有鼠标交互(单击/拖动/滚动)或调整窗口大小时,绘图才会更新(并且 BeginRenderFrame 似乎只被触发)。

这大概是因为时钟在 WindowsFormsHost 内没有正常运行?

【问题讨论】:

    标签: c# wpf ilnumerics windowsformshost


    【解决方案1】:

    在没有太多 wpf 经验的情况下,我认为您是对的:ILNumerics 绘图面板中的时钟使用 WPF 中未引发的 Application.Idle。

    我在 SO 中发现了一篇类似的帖子,其中描述了一种解决方法:Application.Idle event not firing in WPF application

    根据那个帖子,你可以试试

    ComponentDispatcher.ThreadIdle += (sender, e) => System.Windows.Forms.Application.RaiseIdle(e);
    

    【讨论】:

      猜你喜欢
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 2020-12-17
      相关资源
      最近更新 更多