【问题标题】:Using viewmodellocator on projects without app.xaml declaration在没有 app.xaml 声明的项目上使用 viewmodellocator
【发布时间】: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


【解决方案1】:

老问题,我知道,但经过一番搜索,我在其他地方没有找到答案。我刚刚得到了部分工作,这就是我所拥有的:

MainWindow.xaml.cs

...
public MainWindow()
{
    Resources.Add("Locator", new ViewModelLocator());

    InitializeComponent();
}
...

MainWindow.xaml

<Window 
...

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:local="clr-namespace:AppleCMS.ModelHarvesterAddin"
    DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Window.Resources>
        <ResourceDictionary>
            <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
        </ResourceDictionary>
    </Window.Resources>
...

其余部分是 MVVMLight 的标准配置。我认为关键部分是代码隐藏中的Resources.Add("Locator", new ViewModelLocator());。

这在运行插件时有效。不过,它看起来不像 MVVMLight 的设计时数据。我还没有添加多个视图,但我怀疑每个视图都需要上述内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多