如何在XAML中创建类实例?

时间:2021-10-17 18:09:34

I want to create simple utility class that has no visual elements and create it in XAML so I can define databindings. I tried to create class derived from DependencyObject and create it in Window.Resources section but it doesn't call any constructor.

我想创建一个没有可视元素的简单实用程序类,并在XAML中创建它,以便定义databindings。我尝试创建从DependencyObject派生的类并在窗口中创建它。参考资料部分,但它不调用任何构造函数。

3 个解决方案

#1


4  

You can instantiate your class in the app.xaml, just add your namespace to it with

您可以在app.xaml中实例化您的类,只需将名称空间添加到其中

xmlns:yourNamespace="clr-namespace...."

It is easy the intellisense helps.

这很容易理解。

And then in Application.Resources you create your class

然后在应用程序。创建类的资源

<Application.Resources>
   <yourNamespace:YourClass x:Key="yourClassInstanteName" />      
</Application.Resources>

I hope this helps you.

我希望这对你有帮助。

#2


1  

Looks like instances are created when you actually use them. I've found dirty workaround for this problem - to place FindResource("myClass"); in main form constructor.

看起来就像在实际使用实例时创建的实例。我找到了解决这个问题的方法——放置FindResource(“myClass”);主要形式的构造函数。

#3


0  

I know i am posting on an old Question but i came across this while trying to find the answers myself. The code big L posted was indeed correct:

我知道我在张贴一个老问题,但我在试图找到答案时遇到了这个问题。我贴出的密码确实是正确的:

xmlns:yourNamespace="clr-namespace...."

Place a copy in the Application Resources:

在申请资源中放置一份副本:

<Application.Resources>
   <yourNamespace:YourClass x:Key="yourClassInstanteName" />      
</Application.Resources>

The additional key to this information is that the class needs to have a default constructor. So in the Class source you should have a method like so:

这个信息的另一个关键是类需要有一个默认的构造函数。在类源中,你应该有这样的方法:

public yourClassName()

#1


4  

You can instantiate your class in the app.xaml, just add your namespace to it with

您可以在app.xaml中实例化您的类,只需将名称空间添加到其中

xmlns:yourNamespace="clr-namespace...."

It is easy the intellisense helps.

这很容易理解。

And then in Application.Resources you create your class

然后在应用程序。创建类的资源

<Application.Resources>
   <yourNamespace:YourClass x:Key="yourClassInstanteName" />      
</Application.Resources>

I hope this helps you.

我希望这对你有帮助。

#2


1  

Looks like instances are created when you actually use them. I've found dirty workaround for this problem - to place FindResource("myClass"); in main form constructor.

看起来就像在实际使用实例时创建的实例。我找到了解决这个问题的方法——放置FindResource(“myClass”);主要形式的构造函数。

#3


0  

I know i am posting on an old Question but i came across this while trying to find the answers myself. The code big L posted was indeed correct:

我知道我在张贴一个老问题,但我在试图找到答案时遇到了这个问题。我贴出的密码确实是正确的:

xmlns:yourNamespace="clr-namespace...."

Place a copy in the Application Resources:

在申请资源中放置一份副本:

<Application.Resources>
   <yourNamespace:YourClass x:Key="yourClassInstanteName" />      
</Application.Resources>

The additional key to this information is that the class needs to have a default constructor. So in the Class source you should have a method like so:

这个信息的另一个关键是类需要有一个默认的构造函数。在类源中,你应该有这样的方法:

public yourClassName()