【问题标题】:Adding .cs in a ResourceDictionary?在 ResourceDictionary 中添加 .cs?
【发布时间】:2010-03-01 15:14:12
【问题描述】:

我在资源字典中有 DataTemplate,在某些情况下,我需要按钮,但我不知道如何使用背后的代码来管理事件。

我试图像这样在我的资源字典中放置一个类:

<ResourceDictionary 
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Class="SLProject.Templates"
   x:Class="TVTemplate">

我在cs文件中定义了这样的类:

namespace SLProject.Templates
{
    partial class TVTemplate
    { 

    }
}

构建正常,但是当应用程序启动时,我收到以下 XAML 错误:

AG_E_PARSER_BAD_TYPE

我尝试了所有我知道的方法,比如将类类型更改为 ClassModifier,将类设置为 RessourceDictionnary 的继承类……没办法。

有人有想法……

谢谢。

【问题讨论】:

    标签: c# .net xaml silverlight-4.0 expression-blend


    【解决方案1】:

    使用x:Class 属性允许您为ResourceDictionary 定义代码隐藏。 您必须指定类的完整命名空间(即x:Class="WpfApplication.MyClass"),并且此类必须定义为partial(至少VS 2010会抱怨并且没有这样的修饰符不会编译)。

    我模拟了一个简单的例子:

    1.新建一个WPF应用项目(WpfApplication

    2.添加一个新的类文件(TestClass.cs)并粘贴以下代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;
    using System.Windows;
    
    namespace WpfApplication
    {
        public partial class TestClass
        {
            private void OnDoubleClick(object obj, MouseButtonEventArgs args)
            {
                MessageBox.Show("Double clicked!");
            }
        }
    }
    

    3.添加新的ResourceDictionary (Resources.xaml),打开文件并粘贴以下代码

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        x:Class="WpfApplication.TestClass">
        <Style TargetType="{x:Type Label}">
            <EventSetter Event="Label.MouseDoubleClick" Handler="OnDoubleClick"/>
        </Style>
    </ResourceDictionary>
    

    4.最后,打开MainWindow.xaml并输入以下代码

    <Window x:Class="WpfApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary Source="Resources.xaml"/>
        </Window.Resources>
        <Grid>
            <Label Content="Double click here..." HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="Red"/>
        </Grid>
    </Window>
    

    在示例中,我从Style 连接了一个双击事件,因为这是一个需要您从ResourceDictionary 调用一些代码的场景。

    【讨论】:

      【解决方案2】:

      您定义了两次 x:Class 属性,这就是您收到解析器错误的原因。将您的声明更改为此,它应该可以工作:

      <ResourceDictionary 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Class="SLProject.Templates.TVTemplate">
      

      【讨论】:

      • 我查过了,只是复制过去的错误。我已经很好地定义了一次课程。
      【解决方案3】:

      我查过了,只是复制过去的错误。我已经很好地定义了一次课程。

      【讨论】:

        【解决方案4】:

        最好的办法是制作您自己的用户控件并在其中添加您的事件。然后将整个用户控件放入资源字典中。

        【讨论】:

          猜你喜欢
          • 2011-11-20
          • 1970-01-01
          • 2011-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多