0


我正在尝试将谷歌分析集成到我的 silverlight 4 浏览器应用程序中。当我在我的 XAML 中添加我的 Google Analytics 代码时,如下所示:

    <i:Interaction.Behaviors>
        <ga:GoogleAnalytics WebPropertyId="xxxxxxxxxxxxx"/>
    </i:Interaction.Behaviors>

我收到一条错误消息,即 “向‘System.Windows.Interactivity.BehaviorCollection’类型的集合添加值引发异常。”

我在网上找不到太多关于这个的帮助,很少有人指出我的用户控制代码是:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

目前我正在为此使用 VS 2010 和 Blend 4。
感谢您阅读本文,如果您能在这个问题上帮助我,那就太好了。

编辑:

如前所述,我将数据放在这里:

在我的 App.xaml

<Application   
  x:Class="SilverlightApp.slate.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mwa="clr-namespace:Microsoft.WebAnalytics;assembly=Microsoft.WebAnalytics"
             xmlns:ga="clr-namespace:Google.WebAnalytics;assembly=Google.WebAnalytics">


    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/Styles.xaml"/>
                <ResourceDictionary Source="CustomControls.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <mwa:WebAnalyticsService>
            <mwa:WebAnalyticsService.Services>
                <ga:GoogleAnalytics WebPropertyId="XXXXXXXXXXXXX" />
            </mwa:WebAnalyticsService.Services>
        </mwa:WebAnalyticsService>
    </Application.ApplicationLifetimeObjects>
</Application>

在 Grid 下的 Main Page.XAML 中:

  <Button x:Name="btn_library_Icon" Margin="86,2,64,6" Style="{StaticResource menu_librarybuttonstyle}" FontFamily="/SilverlightApp.slate;component/Fonts/Fonts.zip#Segoe UI" FontSize="9.333" Click="btn_library_Icon_Click">
                <i:Interaction.Triggers>
                    <i:EventTrigger SourceName="btn_library_Icon" EventName="Click">
                        <mwab:TrackAction Category="Library Accessed"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <Image x:Name="image" Source="Assets/icon_menu_library.png" Stretch="Fill" Width="23" Height="23"/>
           </Button>

当我运行它时,它给了我一个错误: “向'System.Windows.Interactivity.TriggerCollection'类型的集合添加值引发了异常。”

我在这里想念什么吗?

4

2 回答 2

0

似乎如果您的浏览器用完了,您必须在 app.xaml 中包含一些内容,如下所示:

<Application.ApplicationLifetimeObjects><mwa:WebAnalyticsService/></Application.ApplicationLifetimeObjects>

另外,你运行的是哪个版本?在 1.4.7 版中修复了一个与 OOB 相关的错误:http:
//msaf.codeplex.com/discussions/228657

编辑:我现在已经在一个小型测试应用程序中尝试过了,它对我来说效果很好。尝试设置 google WebPropertyID 时会引发异常,但这可能只是因为我自己没有 Google Analytics 帐户。无论如何,这是我的 App.xaml 的内容:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mwa="clr-namespace:Microsoft.WebAnalytics;assembly=Microsoft.WebAnalytics"
             xmlns:ga="clr-namespace:Google.WebAnalytics;assembly=Google.WebAnalytics"
             x:Class="SilverlightApplication22.App">

    <Application.ApplicationLifetimeObjects>
        <mwa:WebAnalyticsService>
            <mwa:WebAnalyticsService.Services>
                <ga:GoogleAnalytics WebPropertyId="UA-****-1" />
            </mwa:WebAnalyticsService.Services>
        </mwa:WebAnalyticsService>
    </Application.ApplicationLifetimeObjects>

    <Application.Resources>

    </Application.Resources>
</Application>

这是我的参考列表中的屏幕截图。Google.WebAnalytics 的引用版本是 1.4.6.0,Microsoft.WebAnalytics 是 1.4.3.0。我安装的框架版本是1.4.10.0,和你的一样。我猜他们没有用正确的版本号更新所有的 dll。

在此处输入图像描述

编辑2:我刚刚尝试了您发布的按钮代码,以下对我来说很好。我可以在 Fiddler 中看到对 googleanalytics 的请求,它在浏览器和浏览器中都可以使用。我添加了对 Microsoft.WebAnalytics.Behaviors 版本 1.4.6.0 的引用。

<UserControl x:Class="SilverlightApplication22.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:mwab="clr-namespace:Microsoft.WebAnalytics.Behaviors;assembly=Microsoft.WebAnalytics.Behaviors"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">

    <Grid x:Name="LayoutRoot"
          Background="White">
        <i:Interaction.Triggers>
            <i:EventTrigger SourceName="LayoutRoot"
                            EventName="Loaded">
                <mwab:TrackAction Category="App Loaded" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

        <Button x:Name="Btn01"
                Click="Btn01_Click">
            <i:Interaction.Triggers>
                <i:EventTrigger SourceName="Btn01"
                                EventName="Click">
                    <mwab:TrackAction Category="Library Accessed" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            Test
        </Button>


    </Grid>
</UserControl>
于 2011-07-19T09:38:54.067 回答
0

我遇到了完全相同的问题,只是我的项目是 Silverlight 5。

经过大量挖掘,结果发现该项目引用了 Silverlight 工具包的旧版本,特别是版本 3。

一旦我将其更改为使用 SL5 工具包,问题就消失了。

没有解释错误的真正原因,但我希望它无论如何都会有所帮助。

于 2013-11-29T13:34:50.560 回答