访问wpf dll的视图模型
本文关键字:视图 模型 dll wpf 访问 | 更新日期: 2023-09-27 18:13:38
我有一个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
}
您应该能够通过将DataContext
转换为视图模型类型来获得它:
MultiROIStats mroi = new MultiROIStats();
mroi.Show();
var viewmodel = mroi.DataContext as MultiROIStatsViewModel;
if (viewmodel != null)
viewmodel.dothings();
var window = new MultiROIStats();
window.Show();
var vm = window.DataConntext as MultiROIStatsViewModel;
vm.DoThings();