我可以将对象转换为接口或新类吗
我有一个 silverlight 应用程序 在应用程序中,有许多带有数据网格的用户控件,这些控件与采购订单、销售订单、转移等相关。
这些控件的共同点是“文档类型”、“文档编号”。类型结构。
我正在尝试实现一个通用的打印功能,通过该功能我可以获取订单,然后将其发送回主机 erp 系统以创建 PDF 供查看。
我试图避免向所有屏幕添加相同的功能,因此我创建了一个带有打印按钮的打印报告用户控件。用户控件背后有一个视图模型和一些依赖属性。
这个想法是,当用户从一个网格中选择一个订单时,我们将 dependency 属性设置为从网格中选择的项目。为了适应这一点,依赖属性是对象类型。
设置依赖属性后,我可以在 DP 注册中定义的回调方法中选取对象。 Iso e.value 是销售订单、转移订单、采购订单。
private static void UpdatedTheValues(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var sender = d as NavReportPicker;
if (sender != null)
{
PrintObjModel bbb = (PrintObjModel) e.NewValue;
}
}
我可以推动这个属性,因为我可以将它传递给视图模型上的方法,但我不能用它做任何其他事情。
一位同事建议,为了让事情变得更清晰,也许添加一个接口到 Sale Order、transfer ORder、Purcashe ORder 类将使我能够通过调用接口 e 上的方法将对象转换为 Interface 或其他“共享类”。 .g
public NewPrintObj GetNewObject()
{
PrintObj p = new PrintObj;
p.No=1;
p,type=2;
}
这似乎是一个相当好的主意,但我无法将依赖属性对象转换/转换为其他任何对象。
如果我进行强制转换,我会得到无效的强制转换异常,如果我安全地进行强制转换,那么我会得到一个空值。
I have a silverlight app
In the app there are a number of user controls with data grids in them that relate to things such as Purchase Orders, sale orders, transfers etc.
What these have in common is a "document Type", "Document No." type structure.
I am trying to implement a generic priting function whereby i get the Order and then send this back to a HOST erp system to create a PDF for viewing.
I was trying to avoid having to add the same functioanlity to all the screens, so I created a Print Report user control with a print button on it. the user control has a View model behind it and some dependcy properties on it.
The idea is that when the user selects an Order from one of the grids the we set the dependcy prop as the selected item from the grid. To accomodate this the dependency property is of type object.
as the dependcy property is set i can pcik up the obejct in the callback method defined in the regstration of the DP. Iso e.value is either sales Order, Transfer Order, Purchase order.
private static void UpdatedTheValues(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var sender = d as NavReportPicker;
if (sender != null)
{
PrintObjModel bbb = (PrintObjModel) e.NewValue;
}
}
I can push this property around, in that i can pass it to a method on the view model but at no point can i do anythhing else with it.
A colleague suggested that to make things cleaner perhaps adding an interface the to Sale Order, transfer ORder, Purcashe ORder classes would enable me to cast the object to either an Interface or some other "shared class" by calling a method on the interface e..g
public NewPrintObj GetNewObject()
{
PrintObj p = new PrintObj;
p.No=1;
p,type=2;
}
This all seems a fairly good idea, but i am unable to convert/cast the dependcy Property object to anything else.
If i cast i get invalid cast exception, If i safely cast then I get a null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定我理解这些对象是如何相关的(所有订单都来自
PrintObjModel
吗?)我的答案是在依赖属性更改事件中放置一个断点,然后查看
e.NewValue
的内容。然后,您可以查看自己拥有的内容以及是否可以转换为PrintObjModel
。e.NewValue
中的值不是您所期望的,或者您所期望的内容并非源自您所认为的内容。I'm not entirely sure I understand how the objects are all related (do all of the orders derive from
PrintObjModel
?)My answer would be to place a breakpoint in the dependency property change event and look at the content of
e.NewValue
. This will then allow you to see what you do have and whether it should be possible to cast to aPrintObjModel
.Either the value in
e.NewValue
isn't what you're expecting, or the thing which you are expecting doesn't derive from what you think it does.