AutoCAD自定义扩展WPF UpDown

时间:2016-02-10 13:31:15

标签: c# wpf xaml autocad wpf-extended-toolkit

基于Extended WPF Toolkit我想在Alignments上为AutoCAD Stations创建自己的CommonNumericUpDown<Station>。这在StandAlone-WPF-App中非常好用。如果我从磁盘上的任何位置在AutoCAD中对其进行网络加载,我会得到一个XamlParseException“方法或操作未实现。”,但是将我的dll放在AutoCAD-install-directory中它也可以正常工作。创建我自己的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>

2 个答案:

答案 0 :(得分:1)

下载并安装Process Explorer,选择acad.exe进程并在上下文菜单中选择属性... .NET程序集标签,检查哪些程序集已加载。可能是由另一个扩展程序加载的扩展WPF工具包的旧版本。

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

答案 1 :(得分:0)

经过几天的尝试后,我发现在删除Extended WPF Toolkit后错误仍然存​​在。 我还发现了以下问题:WPF 4.0 Application load error in some environments

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

解决了这个问题