视图模型必须是公开的.我该如何处理? [英] viewmodel have to be public. How should I deal with that?

查看:21
本文介绍了视图模型必须是公开的.我该如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎在这里总结了我的问题:双重检查 - 是否有意义有内部视图模型类?

Pretty much it summarizes my problem here: Double check - does it ever make sense to have internal viewmodel class?

我有controls.DLL,我想保留这个自定义控件绑定和视图模型的内部.然而,这似乎是不可能的.

I have controls.DLL and I'd like to keep this custom control bindings and viewmodel's internal. However, this doesn't seem to be possible.

你是如何解决这个问题的?我看到的唯一方法 - 不要使用绑定..

How do you get around that? The only way I see it - don't use bindings..

推荐答案

为什么要为自定义控件提供视图模型?我假设您将视图模型对象分配给 DataContext 属性,但这几乎总是一个错误:DataContext 应该可供消费者随意使用和滥用.换句话说,如果您的自定义控件的使用者显式设置 DataContext,会发生什么情况?听起来您的控件将停止工作并抛出一堆 xaml 绑定错误.

Why do you have a view model for a custom control? I assume you're assigning the view model object to the DataContext property, but this is almost always a mistake: the DataContext should be available to consumers to use and abuse as they please. Stated another way, what happens if a consumer of your custom control explicitly sets the DataContext? It sounds like your control will stop working and throw a bunch of xaml binding errors.

自定义控件本质上是不美观的.没有模型或视图模型,只有一个视图.该视图是 .cs 文件.您通过 themes/generic.xaml 文件提供默认外观,但消费者应该能够提供他们自己的模板.如果您将它们绑定到视图模型,则它们还需要知道如何创建视图模型实例及其所有依赖项.您刚刚创建了高度耦合的代码.DI 容器可以放松耦合,但这只会将类之间的关系从耦合"降级为相关".我说,为什么消费者甚至需要知道这些信息?

A custom control is inherently lookless. There is no model or view model, just a view. That view is the .cs file. You supply a default look via your themes/generic.xaml file, but consumers should be able to supply their own template. If you're tying them to a view model, they also need to know how to create a view model instance and all of its dependencies. You've just created highly coupled code. DI containers can loosen the coupling, but that just downgrades the relationship between classes from "coupled" to "related". I say, why do consumers even need to know that information?

更好的方法是将控件的所有属性作为依赖属性提供.然后您的 generic.xaml 可以提供一个控件模板,该模板使用更高效的 TemplateBinding 将属性/对象绑定到您的控件.如果您需要从业务对象填充这些依赖项属性,请公开另一个 IBusinessObject 类型的依赖项属性,并在该对象的 PropertyMetaData 更改处理程序中设置派生值.如果您的 IBusinessObject 类型包含一个属性,该属性是另一个实现 INotifyPropertyChanged 的​​类,您可能应该 (1) 重新考虑您的对象图或 (2) 使用子类在代码中创建一个 Bnding 对象.

A better approach is to provide all of the properties for your control as dependency properties. Then your generic.xaml can provide a control template that uses the more efficient TemplateBinding to bind properties/objects to your control. If you need to populate these dependency properties from a business object, expose another dependency property of type IBusinessObject and set the derived values in that object's PropertyMetaData changed handler. If your IBusinessObject type contains a property which is yet another class which implements INotifyPropertyChanged, you should probably (1) rethink your object graph or (2) create a Bnding object in code using the subclass.

我认为遵循上述所有建议将消除您关注的问题以及其他问题.将视图模型留给 UserControl.是的,这就是自定义控件令人头疼的原因.把它们做对是相当重要的.

I think following all of the above advice will eliminate the problem about which you're concerned plus the other problems as well. Leave the view models to the UserControls. And yes, this is why custom controls are a MASSIVE headache. Doing them right is fairly involved.

这篇关于视图模型必须是公开的.我该如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆