【发布时间】:2017-06-22 14:50:00
【问题描述】:
我正在开发 Revit 插件,并希望使用 mvvm light。我的问题是程序运行我的 dll 并且我没有 app.xaml 来将 viewmodellocator 的单例声明为应用程序的资源,将其声明为资源的最佳方法是什么?
Revit 将我的代码作为在实现 IExternalCommand 接口的解决方案中的单独类中声明的命令运行。我可以在调用我的 MainWindow 之前从后面的代码中设置这个资源吗?或者我可以将它设置为我的 mainwindow.xaml 的资源吗?
已编辑
到目前为止,我们已经尝试了代码 belov,但在第一次运行命令时出现错误,但在第二次使用命令时没有错误。
来自命令类 CheckerCommand.cs 的示例代码
[Transaction(TransactionMode.Manual)]
public class CheckerCommand : IExternalCommand
{
#region IExternalCommand Members Implementation
public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
{
new ViewModelLocator();
MainWindow window = new MainWindow(revit);
try
{
WindowInteropHelper helper = new WindowInteropHelper(window)
{
Owner = Autodesk.Windows.ComponentManager.ApplicationWindow
};
window.ShowDialog();
return Result.Succeeded;
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
// Element selection cancelled.
return Result.Cancelled;
}
catch (Exception e)
{
message = e.ToString();
try
{
MessageBox.Show(message, "Fatal error");
window.Close();
}
catch
{
return Result.Failed;
}
return Result.Succeeded;
}
}
#endregion
}
这是 MainWindow.xaml 上的一些示例代码
<Window x:Class="QMChecker.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:QMChecker.Views"
xmlns:vm="clr-namespace:QMChecker.ViewModel"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvm="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="QMChecker" Height="700" Width="1200">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/QMChecker;component/Resources/CombinedResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>
</Window>
来自代码隐藏 MainWindow.xaml.cs 的示例代码
public partial class MainWindow : Window
{
public MainWindow(ExternalCommandData _revit)
{
try
{
InitializeComponent();
}
catch (Exception e)
{
throw e;
}
ServiceLocator.Current.GetInstance<MainViewModel>().Revit = _revit;
}
}
来自 ViewModelLocator.cs 的示例代码
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
///
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<SpatialContainmentViewModel>();
SimpleIoc.Default.Register<SpatialContainmentDemandViewModel>();
SimpleIoc.Default.Register<RunChecksViewModel>();
SimpleIoc.Default.Register<SpatialContainmentParameterSummaryViewModel>();
}
public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } }
public SpatialContainmentViewModel SpatialContainment { get { return ServiceLocator.Current.GetInstance<SpatialContainmentViewModel>(); } }
public SpatialContainmentDemandViewModel SpatialContainmentDemand { get { return ServiceLocator.Current.GetInstance<SpatialContainmentDemandViewModel>(); } }
public RunChecksViewModel RunChecks { get { return ServiceLocator.Current.GetInstance<RunChecksViewModel>(); } }
public SpatialContainmentParameterSummaryViewModel SpatialContainmentParameterSummary { get { return ServiceLocator.Current.GetInstance<SpatialContainmentParameterSummaryViewModel>(); } }
public static void Cleanup()
{
SimpleIoc.Default.Unregister<MainViewModel>();
SimpleIoc.Default.Unregister<SpatialContainmentViewModel>();
SimpleIoc.Default.Unregister<SpatialContainmentDemandViewModel>();
SimpleIoc.Default.Unregister<RunChecksViewModel>();
SimpleIoc.Default.Unregister<SpatialContainmentParameterSummaryViewModel>();
}
}
而且我必须在每个 UserControl 上使用 ResourceDictionary
<UserControl.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>
</UserControl.Resources>
【问题讨论】:
-
显示一些你迄今为止尝试过的代码。
-
您找到解决此问题的方法了吗?
标签: c# mvvm-light