AutoCAD 自定义扩展 WPF UpDown

本文关键字:UpDown WPF 扩展 自定义 AutoCAD | 更新日期: 2023-09-27 18:34:05

基于 Extended WPF Toolkit 我想为 AutoCAD 站点沿路线创建自己的CommonNumericUpDown<Station>。这在独立 WPF 应用程序中工作得很好。如果我从磁盘上的任何位置将其网络加载到AutoCAD中,我会得到一个XamlParseException"该方法或操作未实现",但是将我的dll放在AutoCAD安装目录中也可以正常工作。创建我自己的CommonNumericUpDown<Double>也很好用。该错误也仅在设置UpDownBase的属性之一时出现。AutoCAD 在哪里欺骗我?

这是我的代码。为了尽可能简化,我扩展了UpDownBase<T>.

using System;
using System.Windows;
using Xceed.Wpf.Toolkit.Primitives;
using Autodesk.AutoCAD.Runtime;
namespace My.AutoCAD.UITest
{
    class UpDownBaseDoubleUpDown : UpDownBase<double>
    {
        protected override double ConvertTextToValue(string text)
        {
            return double.Parse(text);
        }
        protected override string ConvertValueToText()
        {
            return Value.ToString();
        }
        protected override void OnDecrement()
        {
            Value -= 1;
        }
        protected override void OnIncrement()
        {
            Value += 1;
        }
        protected override void SetValidSpinDirection() { }
        static UpDownBaseDoubleUpDown()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UpDownBaseDoubleUpDown), new FrameworkPropertyMetadata(typeof(UpDownBaseDoubleUpDown)));
        }
    }
    public struct Station : IFormattable, IComparable<Station>
    {
        public double Value { get; set; }
        public Station(double value) { Value = value; }
        public int CompareTo(Station other)
        {
            return Value.CompareTo(other.Value);
        }
        public static Station Parse(string s) { return new Station(double.Parse(s)); }
        public override string ToString() { return Value.ToString(); }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            return Value.ToString(format, formatProvider);
        }
    }
    public class UpDownBaseStationUpDown : UpDownBase<Station>
    {
        protected override Station ConvertTextToValue(string text)
        {
            return Station.Parse(text);
        }
        protected override string ConvertValueToText()
        {
            return Value.ToString();
        }
        protected override void OnDecrement()
        {
            Value = new Station(Value.Value - 1);
        }
        protected override void OnIncrement()
        {
            Value = new Station(Value.Value + 1);
        }
        protected override void SetValidSpinDirection() { }
        static UpDownBaseStationUpDown()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UpDownBaseStationUpDown), new FrameworkPropertyMetadata(typeof(UpDownBaseStationUpDown)));
        }
    }
    /// <summary>Interaction logic for StationUpDownTest.xaml</summary>
    public partial class StationUpDownTest : Window
    {
        public StationUpDownTest()
        {
            InitializeComponent();
        }
    }
    public class Commands
    {
        [CommandMethod("TestMgdStationUpDown")]
        public void TestMgdStationUpDown()
        {
            var dlg = new StationUpDownTest();
            Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowModalWindow(dlg);
        }
    }
}

Generic.xaml:

<ResourceDictionary 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:local="clr-namespace:My.AutoCAD.UITest"
                    xmlns:core="clr-namespace:Xceed.Wpf.Toolkit.Core;assembly=Xceed.Wpf.Toolkit"
                    >
    <ResourceDictionary.MergedDictionaries>
        <core:VersionResourceDictionary AssemblyName="Xceed.Wpf.Toolkit" SourcePath="NumericUpDown/Themes/Generic.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="local:UpDownBaseStationUpDown" BasedOn="{StaticResource NumericUpDown}" />
    <Style TargetType="local:UpDownBaseDoubleUpDown" BasedOn="{StaticResource NumericUpDown}" />
</ResourceDictionary>

StationUpDownTest.xaml:

<Window
             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:My.AutoCAD.UITest"
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" x:Class="My.AutoCAD.UITest.StationUpDownTest"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <!-- doesn't work -->
        <local:UpDownBaseStationUpDown HorizontalAlignment="Left" Margin="55,106,0,0" VerticalAlignment="Top" Width="136" Height="47"
                                       AllowSpin="False"
                                       />
        <!-- works -->
        <local:UpDownBaseDoubleUpDown HorizontalAlignment="Left" Margin="55,106,0,0" VerticalAlignment="Top" Width="136" Height="47" 
                                    AllowSpin="False"
                                    />
    </Grid>
</Window>

AutoCAD 自定义扩展 WPF UpDown

下载并安装进程资源管理器,选择 acad.exe 进程,然后在上下文菜单的"属性..."的".NET 程序集"选项卡中,检查加载了哪些程序集。可能有旧版本的扩展 WPF 工具包由另一个扩展加载。

您还可以使用 Fuslogvw 检查是否没有绑定问题。

经过几天的尝试,我发现即使在删除扩展 WPF 工具包后,错误仍然存在。我还发现了以下问题:某些环境中的WPF 4.0应用程序加载错误

我的同事通过附加到AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;并返回Assembly.LoadFile (assemblyPath);解决了问题