Silverlight 4 - 在运行时从资源字典中检索纯色画笔?

发布于 2024-12-12 06:09:06 字数 479 浏览 6 评论 0原文

我正在尝试从 C# 检索资源字典 (Styles.xaml) 中预定义的纯色画笔。

问题是,当我运行以下代码时,什么也没有发生:

private void LinkContinue_MouseEnter(object sender, MouseEventArgs e)
{
    this.LinkContinue.Background = (SolidColorBrush)Resources["HoverColorBrush"];
}

但是,如果我在代码中显式设置背景,它运行得很好:

private void LinkContinue_MouseLeave(object sender, MouseEventArgs e)
{
    this.LinkContinue.Background = new SolidColorBrush(Colors.Gray);
}

有什么想法吗?

I am trying to retrieve a solid color brush pre-defined in a resource dictionary (Styles.xaml) from C#.

The problem is that when i run the following code nothing happens:

private void LinkContinue_MouseEnter(object sender, MouseEventArgs e)
{
    this.LinkContinue.Background = (SolidColorBrush)Resources["HoverColorBrush"];
}

However if I set the background in code explicitly it runs fine:

private void LinkContinue_MouseLeave(object sender, MouseEventArgs e)
{
    this.LinkContinue.Background = new SolidColorBrush(Colors.Gray);
}

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧夏天 2024-12-19 06:09:06

语法 Resources["HoverColorBrush"] 查找存储在当前对象的资源中的资源。在本例中,是包含方法 LinkContinue_MouseEnter 的类实例的资源。

如果您想使用与 {StaticResource HoverColorBrush} 相同的查找机制,则需要使用 FindResource 方法:

但是,如幸运的是,Silverlight 不支持 FindResource,因此您需要直接在 Styles.xaml 中定义的 ResourceDictionary 中查找资源,或推出您自己的 FindResource

以下是关于此主题的博客文章和示例代码:

The syntax Resources["HoverColorBrush"] looks up a resource stored in the resources of the current object. In this case, the resources of the instance of the class that contains the method LinkContinue_MouseEnter.

If you want to use the same lookup mechanism that {StaticResource HoverColorBrush} would use, you need to use the FindResource method instead:

But, as luck would have it, Silverlight does not support FindResource and so you need to either look up the resource directly in the ResourceDictionary defined in Styles.xaml, or roll your own FindResource.

Here is a blog article on this topic with sample code:

九命猫 2024-12-19 06:09:06

所以答案是 Application.Current.Resources["ResourceName"] as SolidColorBrush!谁会知道 Resources 对象指向页面的资源字典?呃呃呃

So the answer was Application.Current.Resources["ResourceName"] as SolidColorBrush! Who would've known that the Resources object points to the resource dictionary for the page? UGHHH

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