通过x:Class给ResourceDictionary内添加事件处理

windxx 2012-08-17 11:14:09
我想给存在于CustomControl的Template里的事件添加处理程序(Storyboard.Completed发生的时候我想弹出一个对话框),但却发生了异常,请大家帮忙看下哪里错了,我的代码如下:
xaml代码:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:xClassTest"
x:Class="xClassTest.Class1"
>


<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Viewbox>
<Grid x:Name="TheGrid" Width="50" Height="50">
<Rectangle x:Name="TheRect" Fill="LightBlue"/>
</Grid>
</Viewbox>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Grid.MouseEnter" SourceName="TheGrid">
<BeginStoryboard>
<Storyboard Completed="Storyboard_Completed">
<DoubleAnimation To="100" Storyboard.TargetName="TheGrid"
Storyboard.TargetProperty="Width"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Grid.MouseLeave" SourceName="TheGrid">
<BeginStoryboard>
<Storyboard Completed="Storyboard_Completed">
<DoubleAnimation To="50" Storyboard.TargetName="TheGrid"
Storyboard.TargetProperty="Width"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

.cs codes:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace xClassTest
{
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}

public partial class Class1 : ResourceDictionary
{
public Class1()
{
InitializeComponent();
}
void Storyboard_Completed(object sender, EventArgs e)
{
MessageBox.Show("Completed!");
}
}
}

调用CustomControl1的代码:

<Window x:Class="xClassTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:xClassTest"
Title="Window1" Height="300" Width="300">
<Canvas>
<local:CustomControl1/>
</Canvas>
</Window>


以上代码Build没提示错误,但是Run的时候在Window1构造函数的InitializeComponent()方法处提示:"无法重新初始化 ResourceDictionary 实例。 对象“xClassTest.Class1”行 4 位置 5 处的错误。"

后来我发现,只要在CustomControl1的模板xaml文件那里的根元素添加: x:Class="xClassTest.Class1"
这一行就有这个错误提示,请问是什么问题呢?如果要实现我开始描述的要求(Storyboard Completed事件发生的时候在C#代码部分做处理)应该怎么做呢?

非常感谢您的帮助。
...全文
487 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
windxx 2012-08-20
  • 打赏
  • 举报
回复
是不是我的想法有什么错误?可以通过其他方式实现?如果是的话,请大家指点下,谢谢
windxx 2012-08-17
  • 打赏
  • 举报
回复
谢谢你的提示~
不用的话好像就是语法错误了,提示出错,说缺少partial。我也不是很清楚,也是一步一步摸索的,在网上找了很久也没答案,希望大家帮助!
小小农民 2012-08-17
  • 打赏
  • 举报
回复
public partial class Class1 : ResourceDictionary
public partial class不太熟,是不是这个导致的啊,直接用public class行不?
,顶下
windxx 2012-08-17
  • 打赏
  • 举报
回复
是不是我表述的不清楚,如果是请大家提醒下啊。如果有知道的,请不要因为简单不稀罕告诉我啊,都卡了几天了,真心希望大家能帮下忙,非常感谢!

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧