OxyPlot - 此 PlotModel 已被其他一些 PlotView 控件使用

问题描述 投票:0回答:1

我正在使用 WPF 和 .NET 6.0。我的应用程序基本上是 Josh Smith 的 MVVM 演示,替换了业务逻辑。绘图代码位于与应用程序分开的 .NET 6.0 项目中。该应用程序在单独的选项卡上具有独立的工作区。我可以打开多个选项卡,有些带有绘图,有些没有,但是如果我尝试返回到带有绘图的选项卡,我会看到上面的错误消息。当我尝试返回带有绘图的选项卡时,我可以看到视图在引发异常之前再次实例化。我在 .NET Framework 4.7.2 应用程序中使用相同的代码,没有任何问题。

我的 XAML:

<UserControl x:Class="DataPlot.Views.BasicTrackView"
             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:DataPlot.Views"
             xmlns:oxy="http://oxyplot.org/wpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <oxy:PlotView x:Name="Plot" Model="{Binding PlotModel}" Grid.Column="1" MinHeight="{Binding MinimumPlotHeight}">
            <oxy:PlotView.DefaultTrackerTemplate>
                <ControlTemplate>
                    <oxy:TrackerControl Position="{Binding Position}" LineExtents="{Binding PlotModel.PlotArea}">
                        <oxy:TrackerControl.Background>
                            <LinearGradientBrush EndPoint="0,1">
                                <GradientStop Color="#f0e0e0ff" />
                                <GradientStop Offset="1" Color="#f0ffffff" />
                            </LinearGradientBrush>
                        </oxy:TrackerControl.Background>
                        <oxy:TrackerControl.Content>
                            <TextBlock Text="{Binding}" Margin="7" />
                        </oxy:TrackerControl.Content>
                    </oxy:TrackerControl>
                </ControlTemplate>
            </oxy:PlotView.DefaultTrackerTemplate>
        </oxy:PlotView>

    </Grid>
</UserControl>

背后代码:

public partial class BasicTrackView : UserControl
{
    public BasicTrackView()
    {
        InitializeComponent();
    }
}

我在 ItemsControl 中有几个这样的图:

<UserControl x:Class="DataPlot.Views.TrackContainerView"
             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:DataPlot.Views"
             xmlns:vm="clr-namespace:DataPlot.ViewModels"
             xmlns:helpers="clr-namespace:DataPlot.Helpers"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type vm:BasicTrackViewModel}">
            <local:BasicTrackView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:GenericTrackViewModel}">
            <local:GenericTrackView IsActive="{Binding IsActive}"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:CollapsableTrackViewModel}">
            <local:CollapsableTrackView IsActive="{Binding IsActive}"/>
        </DataTemplate>
    </UserControl.Resources>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="Controls?" Width="60" Height="25" HorizontalAlignment="Right" Margin="0,2,20,2" Grid.Column="4"
               HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="LightGray">
            <Label.ToolTip>
                <ToolTip>
                    <DockPanel Width="200" Height="75">
                        <TextBox Text="Zoom: Ctrl + Right Mouse, or&#x0a;Zoom with mouse wheel&#x0a;Pan: Right Mouse" AcceptsReturn="True"/>
                    </DockPanel>
                </ToolTip>
            </Label.ToolTip>
        </Label>

        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <ItemsControl Grid.Row="1" ItemsSource="{Binding TrackViewModels}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Grid helpers:GridHelpers.RowCount="{Binding TrackViewModels.Count}" helpers:GridHelpers.StarRows="All"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Grid.Row" Value="{Binding RowIndex}" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </ScrollViewer>

    </Grid>
</UserControl>

我可以采取什么措施来阻止应用程序尝试重新创建已存在的视图?

c# wpf .net-6.0
1个回答
0
投票

我想感谢 bigcrazal(在他的评论中)通过 edvinasz 提供的代码链接提供了一个简单的解决方案。由于链接可能会变坏,我将把代码放在下面。在我的问题中,我提到我在旧的 .NET Framework 4.7.2 项目中使用此代码没有问题。我现在相信这是因为我在过去的应用程序中使用了静态选项卡。

using System;
using System.Linq;
using System.Reflection;
using OxyPlot;

/// <summary>
/// Use this sub implementation of the <see cref="PlotModel"/> if the view will be declared using data template.
/// Because views will be automatically generated, and new view will be different this causes current version to throw an error.
/// </summary>
public class ViewResolvingPlotModel : PlotModel, IPlotModel
{
    private static readonly Type BaseType = typeof(ViewResolvingPlotModel).BaseType;
    private static readonly MethodInfo BaseAttachMethod = BaseType
        .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
        .Where(methodInfo => methodInfo.IsFinal && methodInfo.IsPrivate)
        .FirstOrDefault(methodInfo => methodInfo.Name.EndsWith(nameof(IPlotModel.AttachPlotView)));

    void IPlotModel.AttachPlotView(IPlotView plotView)
    {
        //because of issue https://github.com/oxyplot/oxyplot/issues/497 
        //only one view can ever be attached to one plotmodel
        //we have to force detach previous view and then attach new one
        if (plotView != null && PlotView != null && !Equals(plotView, PlotView))
        {
            BaseAttachMethod.Invoke(this, new object[] { null });
            BaseAttachMethod.Invoke(this, new object[] { plotView });
        }
        else
        {
            BaseAttachMethod.Invoke(this, new object[] { plotView });
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.