如何使松散的 Xaml 内容感知自定义控件
我有一个松散的 XAML 文件......
<Style
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
TargetType="{x:Type local:CustomControl}">
<Setter Property="HoverOpacity" Value="1.0"/>
</Style>
我想在运行时加载它。当我这样做时,我收到一个异常,指出“类型引用找不到名为“CustomControl”的公共类型。”如何让松散的 XAML 了解我的命名空间?
我需要使用 HoverOpacity
,它是 CustomControl
的依赖属性。这是我当前用于加载 XAML 的代码:
var resource = Application.GetResourceStream(new Uri("pack://application:,,,/Assets/HoverStyle.xaml"));
XamlReader.Load(resource.Stream);
顺便说一句,我意识到 XAML 很简单,我可以在代码中插入 Style
,但这是一个 hello world XAML;它会变得更加复杂,涉及动画等。
PS 另一种解决方案是将 XAML 文件附加到从 Panel 派生的自定义控件(不会使 Visual Studio 2008 崩溃)的方法,或者轻松附加触发器、数据触发器、进入操作和退出操作的方法到自定义控件。
I have a loose XAML file...
<Style
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
TargetType="{x:Type local:CustomControl}">
<Setter Property="HoverOpacity" Value="1.0"/>
</Style>
... that I want to load at runtime. When I do I get an exception stating, "Type reference cannot find public type named 'CustomControl'." How can I make the loose XAML aware of my namespace?
I need to use HoverOpacity
which is a dependency property of the CustomControl
. Here is the code that I am currently using to load the XAML:
var resource = Application.GetResourceStream(new Uri("pack://application:,,,/Assets/HoverStyle.xaml"));
XamlReader.Load(resource.Stream);
BTW, I realize that the XAML is simple and I could just insert the Style
in code, but this is a hello world XAML; its going to become a lot more complex, involving animations and such.
P.S. Another solution would be a way of either attaching a XAML file to a custom control derived from Panel (one that doesn't crash Visual Studio 2008) or a way of easily attaching triggers, data-triggers, entry-actions, and exit actions to custom controls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
天哪,我终于明白了。我需要使用命名空间指定程序集名称;像这样:
我将把答案归功于任何能在接下来的两天内回答我的“PS”问题的人。整个情况看起来有点潮湿,所以我对替代方案非常感兴趣。
谢谢 :)
Gosh darn it, I figured it out. I needed to specify the assembly name with the namespace; like so:
I'll give answer credit to anyone who could answer my "P.S." question within the next two days. This whole situation seems a little wet, so I'd be really interested in alternatives.
Thanks :)