简体   繁体   English

声明对象时可以调用类的方法吗?

[英]Can you call a class' method when you declare the object?

I have a class, where I declare it, but that class gets added as an item to another bigger class. 我有一个类,我声明它,但该类作为项添加到另一个更大的类。 Is there a way to call the Init() method in the same statement as the call? 有没有办法在与调用相同的语句中调用Init()方法? Similar to defining public properties/variables when you call the constructor. 与调用构造函数时定义公共属性/变量类似。 I don't want to call the Init() method in the constructor because it messes with the WPF Designer. 我不想在构造函数中调用Init()方法,因为它与WPF Designer混淆。

 FitsView fv = new FitsView();
 fv.Init();

You could use a static function to do that: 您可以使用静态函数来执行此操作:

public static FitsView CreateFitsView()
{
    var fv = new FitsView();
    fv.Init();
    return fv;
}

Then you simply call that static function instead of new FitsView() 然后你只需调用静态函数而不是new FitsView()

You could also try hooking a custom event to your FitsView if it knows when it's ready to be initialized? 您还可以尝试将自定义事件挂接到FitsView,如果它知道何时可以初始化它?

And use it like this: 并像这样使用它:

FitsView fv = new FitsView();
fv.someCustomEvent += (o,e) => { fv.Init(); };

Similar to the StringBuilder.Append you could alter Init to return a reference to the object. StringBuilder.Append类似,您可以更改Init以返回对该对象的引用。

Public FitsView Init()
{
    //Do stuff

    return this;
}

Then: 然后:

FitsView fv = new FitsView().Init();

If the designer gets problematic because of your init method there are two reasons I can think of: 如果设计人员因为你的init方法而出现问题,我可以想到两个原因:

  • It is because something you do in Init method needs locality of your application (reading resources or files or using hardware) 这是因为你在Init方法中所做的事情需要你的应用程序的位置(读取资源或文件或使用硬件)
  • Calling your Init method needs some external assemblies to be loaded dynamically. 调用Init方法需要动态加载一些外部程序集。

For the first matter you may want to check: 首先,您可能需要检查:

  1. For your class: Is there a DesignMode property in WPF? 对于您的类: WPF中是否有DesignMode属性?
  2. For your view model: http://blog.laranjee.com/how-to-get-design-mode-property-in-wpf/ 对于您的视图模型: http//blog.laranjee.com/how-to-get-design-mode-property-in-wpf/

Also people in here pointed out this bug so please beware (hosting wpf in winforms): https://connect.microsoft.com/VisualStudio/feedback/details/620001/system-componentmodel-designerproperties-getisindesignmode-does-not-work-if-the-wpf-is-hosted-on-a-winform#tabs 此处的人也指出了这个bug,请注意(在winforms中托管wpf): https ://connect.microsoft.com/VisualStudio/feedback/details/620001/system-componentmodel-designerproperties-getisindesignmode-does-not-work- 如果最WPF的是托管式-A-WinForm的#标签

For the second matter you can wrap your Init method in another let's say InitWrapper and do your design mode check for wrapper method. 对于第二个问题,你可以将你的Init方法包装在另一个假设的InitWrapper然后检查包装方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么时候应该宣布私人或公共课程? - When should you declare a class private or public? 你能在结构中声明 object 类型吗 - Can you declare object types in struct 您可以像在类字段中一样以编程方式调用方法吗? - Can you programmatically call a method as if it were a class field? 你可以在另一个类中声明一个类的列表吗? - Can you declare a List of a class within another class? 当你声明一个没有实例的对象时,.Net会做什么? - What does .Net do when you declare an object without an instance? 从另一个sup类的子类调用方法时,如何从超类的子类执行方法? - How can you execute a method from a sub class of a super class, when you are calling it from a sub class from another sup class? 在列表视图中调用 select 项时调用方法 - Call a method when you select item in a listview 你能在WebBrowser中声明一个InputScope吗? - Can you declare an InputScope in a WebBrowser? 你可以将一个匿名对象作为json发布到webapi方法,它是否将json对象的属性与webapi调用的参数相匹配? - Can you post an anonymous object as json to a webapi method and it match the properties of the json object to the parameters of the webapi call? 在调用“WebSecurity”类的任何其他方法之前,必须调用“WebSecurity.InitializeDatabaseConnection”方法 - You must call the “WebSecurity.InitializeDatabaseConnection” method before you call any other method of the “WebSecurity” class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM