WP8中的ToggleSwitch

时间:2014-07-16 10:32:54

标签: c# visual-studio xaml windows-phone-8

我在VS2013中编写WP8应用程序,我想将ToggleSwitch添加到我的移动应用程序中。为了达到这个目的,我点击了#34;管理NuGet包" (在我的项目中安装了最新版本)并选择了Windows Phone Toolkit。我有以下XAML代码:

    xmlns:tool="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<ToggleSwitch x:Name="toggleSwitch1" Header="ToggleSwitch" 
              OnContent="On" OffContent="Off" 
              Toggled="ToggleSwitch_Toggled"/>

错误是: 1)The tag 'ToggleSwitch' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. 2)The name "ToggleSwitch" does not exist in the namespace "http://schemas.microsoft.com/client/2007".

同样在WP页面上,以下使用语句using Microsoft.Phone.Controls.Toolkit;收到错误:The type or namespace name 'Toolkit' does not exist in the namespace 'Microsoft.Phone.Controls' (are you missing an assembly reference?)

我该如何解决?

2 个答案:

答案 0 :(得分:3)

如果你已经正确安装了Nuget Package,那么下面的代码应该运行完美。

        xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
         <toolkit:ToggleSwitch x:Name="ToggleSwitch" Header="Toggle Switch" IsChecked="false" Content="Content Goes here" Checked="switch_Checked" Unchecked="switch_Unchecked"/>

答案 1 :(得分:1)

您需要在元素调用中使用命名空间标识符作为前缀。喜欢这个

<tool:ToggleSwitch x:Name="toggleSwitch1" Header="ToggleSwitch" 
          OnContent="On" OffContent="Off" 
          Toggled="ToggleSwitch_Toggled"/>

那应该解决你的问题。