WPF 在设计时获取类型?
好吧,我有一个继承 usercontrol(view) 的控件,我像使用用户控件(基本控件)一样使用它,现在问题是,如果我这样做,
MessageBox.Show(this.GetType().ToString());
我在运行时和设计时收到不同的消息,在设计时我得到 View 和我运行时获取继承视图的xaml文件的类名...
如何在设计时获取继承类类型而不是基类?
这里有一些代码:
首先我们有视图类
public class View : UserControl
{
public override void OnApplyTemplate()
{
MessageBox.Show(this.GetType().ToString());
base.OnApplyTemplate();
}
}
然后我们有一个 XAML 文件:
<local:View x:Class="WpfApplication2.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</local:View>
现在如果您在 VisualStudio 2010 中编译并打开“WpfApplication2.Test”,您将看到一个消息框,上面写着“WpfApplication2.View”..
但是如果您将测试控件放在主窗口中,然后按运行(F5),您将得到 WpfApplication2.Test ..我想要的是在设计时具有与运行时相同的响应...
Well i have a control that inherits usercontrol(view) and im using it as you use a usercontrol (a base control) now here is the problem if i do
MessageBox.Show(this.GetType().ToString());
i get different messages in runtime and design time, in design time i get View and i runtime i get the class name of the xaml file inheriting the view...
How can i get the inheriting class type in design time instead of the base class?
Here comes some code:
First we have the view Class
public class View : UserControl
{
public override void OnApplyTemplate()
{
MessageBox.Show(this.GetType().ToString());
base.OnApplyTemplate();
}
}
Then we have a XAML file:
<local:View x:Class="WpfApplication2.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</local:View>
now if you compile and open "WpfApplication2.Test" in VisualStudio 2010 you will get a message box that says "WpfApplication2.View"..
But if you place the Test control in your MainWindow and press Run(F5) you get WpfApplication2.Test.. what i want is to have the same response in design time that i have in run time...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是 Visual Studio 2010 中的 XAML 设计器不会实例化代码隐藏中声明的实际类。相反,它仅实例化其基类。
如果您考虑一下,当您修改 XAML 时,您实际上是在修改代码隐藏中声明的类,因为它是一个部分类,与从 XAML 创建的另一个部分相结合。因此设计者无法创建类的实例:它仍在创建中。
我认为如果不编写以某种方式与 Visual Studio 本身交互以询问实际正在设计的文件的代码,您将无法完成您所追求的目标。
您至少可以使用检查 DesignerProperties.GetIsInDesignMode() 来保护您的代码。
请参阅以下链接以获取一些相关信息:
WPF 设计器加载失败疑难解答
当 VS 2008 XAML 设计器视图尝试呈现 GUI 时会调用什么?
不要在 WPF 设计器 (Cider) 中执行此操作!
Well, the problem is that the XAML designer in Visual Studio 2010 does not instantiate the actual class declared in the code-behind. Instead, it only instantiates its base class.
If you think about it, as you modify your XAML, you are actually modifying the very class declared in the code-behind since it is a partial class combined with another part created from the XAML. So the designer can't create an instance of your class: it's still being created.
I don't think you're going to be able to accomplish what you're after without writing code that somehow interacts with Visual Studio itself to ask what file is actually being designed.
You can at least guard your code using a check for DesignerProperties.GetIsInDesignMode().
See these links for some related information:
Troubleshooting WPF Designer load failures
What gets called when the VS 2008 XAML Designer view tries to render the GUI?
Don't do that in the WPF Designer (Cider)!
当您设计派生控件时,VS2010 Designer (Cider) 会实例化基类的实例。你对此无能为力。
The VS2010 Designer (Cider) is instantiating an instance of the base class when you design a derived control. There's nothing you can do about it.
Petoj,我认为你应该问自己/描述为什么你想知道类型的名称,以及为什么它在设计时不同时会造成麻烦。除非你在与风车作斗争并且在我看来不会得到合理的答案。
更新 - 简单解决方法的伪代码:
更新 2: 在设计时,无法获取后代类的名称设计的。该问题可能应该以不同的方式解决,而不是取决于实际类的名称。
Petoj, I think you should ask yourself / describe why you want to know the name of the type and why it's causing troubles when it differs on design time. Unless you're fighting windmills and won't get a reasonable answer in my opinion.
Update — pseudocode of a simple workaround:
Update 2: On design-time, there is no way to get the name of the descendant class being designed. The problem should be probably solved in a different way, not depending on the name of the actual class.
我仍在学习 WPF,所以这可能不是您想要的。
在设计时和运行时, this.GetType().ToString();在消息框中返回“WpfApplication2.View”。
因此视图在两种模式下都会返回。我会声明我对您的代码做了一点小小的更改。
我将其标记为部分类,而不仅仅是类,因为 XAML 显然是从 .cs 文件中分离出来的。但我不认为这会成为问题。
Im still learning WPF so this probably isn't what you are looking for.
In design time and runtime, this.GetType().ToString(); returns to me "WpfApplication2.View" in the message box.
So view is being returned in both modes. I will state I have made one slight change to your code.
I have it marked as a partial class instead of just class, as the XAML is obviously split out from the .cs file. I would not think this would be the problem though.