访问wpf dll的ViewModel

时间:2014-07-18 18:21:12

标签: c# wpf dll mvvm

我有一个wpf MultiROIStats.dll模式,视图,ViewModel。这是视图的C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MultiROIStats
{

    using ViewModel;

    //xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    /// <summary>
    /// Interaction logic for MultiROIStats.xaml
    /// </summary>
    public partial class MultiROIStats : Window
    {
        public MultiROIStats()
        {          
            InitializeComponent();
            DataContext = new MultiROIStatsViewModel();
        }
    }
}

要使用此MultiROIStats,dll,我将其插入另一个项目的参考信息。现在我需要访问插入的MultiROIStats.dll的ViewModel(那里的一些方法)。我想知道我该怎么做?我启动了插入MultiROIStats.dll的对象,但找不到我想在其ViewModel中使用的方法:

 private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            MultiROIStats mroi = new MultiROIStats();

            mroi.Show();   

           // here should be mroi.viewmode.dothings() ... but I don't know how to access it

        }

2 个答案:

答案 0 :(得分:1)

您应该可以通过将DataContext转换为视图模型类型来实现它:

MultiROIStats mroi = new MultiROIStats();
mroi.Show();   

var viewmodel = mroi.DataContext as MultiROIStatsViewModel;
if (viewmodel != null)
    viewmodel.dothings();

答案 1 :(得分:1)

var window = new MultiROIStats();
window.Show();   

var vm = window.DataConntext as MultiROIStatsViewModel;
vm.DoThings();