Silverlight View 通过 MEF 将 DependencyProperty 导出到 ViewModel

发布于 2024-10-05 08:54:56 字数 1403 浏览 2 评论 0原文

我需要 ViewModel 构造函数中的视图中的一个 DependencyProperty:

我的问题:MEF 不会 SatisfyImports() '因为它标记有一个或多个 ExportAttributes' (这是例外)

这是 VIEW 的代码结构:

public class MyView : UserControl
{
    [Export(MethodTypes.ChartType)]
    public Charts MyChartType
    {
        get
        {
            object k = GetValue(ChartTypeProperty);
            Charts f = (Charts)Enum.Parse(typeof(Charts), k.ToString(), true);
            return f;
        }
        set
        {
            SetValue(ChartTypeProperty, value);
        }
    }

    [Import(ViewModelTypes.GenericChartViewModel)]
    public object ViewModel
    {
        set
        {
            DataContext = value;
        }
    }

    public MyView()
    {
        InitializeComponent();

        if (!ViewModelBase.IsInDesignModeStatic)
        {
            // Use MEF To load the View Model
            CompositionInitializer.SatisfyImports(this);
        }
    }
}

和 VIEWMODEL:

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.GenericChartViewModel)]
public class GenericChartViewModel
{
    [ImportingConstructor]
    public GenericChartViewModel([Import(MethodTypes.ChartType)] Charts forChartType)
    {
        string test = forChartType.ToString();
    }
}

请给我任何提示,或者建议通过 mef 传递参数的更好解决方案。

在我的情况下,我现在只需要传递 dependentecyproperty...

谢谢

I have a need of one DependencyProperty from a View in my ViewModel constructor:

My problem: MEF wouldn't SatisfyImports() 'because it is marked with one or more ExportAttributes' (that is the exception)

This is the code structure for the VIEW:

public class MyView : UserControl
{
    [Export(MethodTypes.ChartType)]
    public Charts MyChartType
    {
        get
        {
            object k = GetValue(ChartTypeProperty);
            Charts f = (Charts)Enum.Parse(typeof(Charts), k.ToString(), true);
            return f;
        }
        set
        {
            SetValue(ChartTypeProperty, value);
        }
    }

    [Import(ViewModelTypes.GenericChartViewModel)]
    public object ViewModel
    {
        set
        {
            DataContext = value;
        }
    }

    public MyView()
    {
        InitializeComponent();

        if (!ViewModelBase.IsInDesignModeStatic)
        {
            // Use MEF To load the View Model
            CompositionInitializer.SatisfyImports(this);
        }
    }
}

and the VIEWMODEL:

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.GenericChartViewModel)]
public class GenericChartViewModel
{
    [ImportingConstructor]
    public GenericChartViewModel([Import(MethodTypes.ChartType)] Charts forChartType)
    {
        string test = forChartType.ToString();
    }
}

Please give me any hints on this or maybe suggest a better solution for passing parameters through mef

In my case, I would need to pass only dependecyproperty's for now...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2024-10-12 08:54:56

您的解决方法并不是很好。您不能从 ChartTypes 中删除导出并将其手动传递给想要的人吗?我认为视图模型只是一个对此感兴趣的人。

Your work around isn't really good.. can't you remove the export from ChartTypes and pass it manually to whoever wants it? I presume the viewmodel is only one insterested in it..

無心 2024-10-12 08:54:56

我成功地做到了!

我使用:而不是默认构造函数中的代码,

    void MyView_Loaded(object sender, RoutedEventArgs e)
    {
        if (!ViewModelBase.IsInDesignModeStatic)
        {
            var catalog = new TypeCatalog(typeof(GenericChartViewModel));
            var container = new CompositionContainer(catalog);

            container.ComposeParts(this);
        }
    }

并且 dependencyproperty 值已正确传播到 ViewModel
(必须在加载控件后执行此操作,否则该属性将具有默认值)

但是,如果有人可以:

  • 告诉我如何从另一个非引用程序集生成目录?

谢谢

I managed to put this through !

Instead of the code in the default constructor, I use:

    void MyView_Loaded(object sender, RoutedEventArgs e)
    {
        if (!ViewModelBase.IsInDesignModeStatic)
        {
            var catalog = new TypeCatalog(typeof(GenericChartViewModel));
            var container = new CompositionContainer(catalog);

            container.ComposeParts(this);
        }
    }

and the dependencyproperty value is correctly propagated to the ViewModel
(must do this after control is loaded, or the property will have its default value)

However, I would be very grateful if someone could:

  • tell me how generate a catalog from another non-referenced assembly?

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文