如何使松散的 Xaml 内容感知自定义控件

发布于 2024-10-04 00:49:40 字数 920 浏览 0 评论 0原文

我有一个松散的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2024-10-11 00:49:40

天哪,我终于明白了。我需要使用命名空间指定程序集名称;像这样:

<Style
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
    TargetType="{x:Type local:CustomControl}">
    <Setter Property="HoverOpacity" Value="1.0"/>
</Style>

我将把答案归功于任何能在接下来的两天内回答我的“PS”问题的人。整个情况看起来有点潮湿,所以我对替代方案非常感兴趣。

谢谢 :)

Gosh darn it, I figured it out. I needed to specify the assembly name with the namespace; like so:

<Style
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
    TargetType="{x:Type local:CustomControl}">
    <Setter Property="HoverOpacity" Value="1.0"/>
</Style>

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文