Revit二次开发的IExternalEventHandler

发布时间 2023-04-14 23:03:20作者: HRDK

看了revit二次开发书籍中关于IExternalEventHandler的用法,个人认为过于麻烦,且在实现外部事件并改变winform或wpf表格的内容时,代码的功能直接过于纠缠,作者根据wpf+prism框架重新写了一个外部事件的demo

xaml语言如下

<Window x:Class="RevitDevFrame.Views.TestView"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RevitDevFrame.Views"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="TestView" Height="450" Width="800">
    <Grid>
        <Button Content="测试按钮" VerticalAlignment="Center" HorizontalAlignment="Center" Command="{Binding TestCommand}"></Button>
    </Grid>
</Window>

viewmodel如下

using Autodesk.Revit.UI;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RevitDevFrame.ViewModels
{
    public class TestViewModel : BindableBase, IExternalEventHandler
    {
        #region 构造器
        public TestViewModel() {
            externalEvent = ExternalEvent.Create(this);
            TestCommand = new DelegateCommand(Test);
        }


        #endregion

        #region 字段
        private ExternalEvent externalEvent;
        
        #endregion

        #region 字段-属性

        #endregion

        #region 委托-方法
        public DelegateCommand TestCommand { get;private set; }
        private void Test()
        {
            externalEvent.Raise();
        }
        #endregion

        #region 方法
        public void Execute(UIApplication app)
        {
            TaskDialog.Show("Revit", "Hello World!");
        }

        public string GetName()
        {
            return "Test";
        }
        #endregion

    }
}

根据改代码即可实现点击按钮输出 helloworld对话框