【问题标题】:Using Google Analytics in Silverlight 4在 Silverlight 4 中使用 Google Analytics
【发布时间】:2011-07-19 08:54:11
【问题描述】:


我正在尝试将谷歌分析集成到我的 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'类型的集合添加值引发异常。”

我在这里错过了什么吗?

【问题讨论】:

    标签: c# silverlight xaml google-analytics-api


    【解决方案1】:

    如果您的浏览器用完了,您似乎必须在 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>
    

    【讨论】:

    • 如您的邮件中所述,我已将其添加为以下 但它显示错误,因为在“WebAnalyticsService”类型中找不到附加属性“服务”。我正在使用最新版本 1.4.10
    • 感谢您的信息。我在 App.xaml 文件中添加了信息。现在我正在尝试在 Mainpage.xaml 中添加 Trigger 事件,但出现以下错误:“将值添加到 'System.Windows.Interactivity.TriggerCollection' 类型的集合引发异常。” 我包含的代码如下如下:
    • 如果您编辑原始帖子以包含这些代码示例会更好。阅读这篇文章的其他人更容易阅读和理解。此外,您的 EventTrigger 中似乎缺少 EventName。我猜它应该是“已加载”。
    • 参见我上面的“EDIT 2”修正。
    • @henik,我尝试了 Edit 2 选项,但仍然遇到同样的错误。似乎这种行为在那里造成了一些问题,我现在无法识别它。一旦我得到更多的输入,我会更新你。非常感谢您的帮助:)
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2022-07-07
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多