WP8中的切换开关

本文关键字:开关 WP8 | 更新日期: 2023-09-27 18:13:14

我正在VS2013中编写WP8应用程序,我想将ToggleSwitch添加到我的移动应用程序中。为了实现这一目标,我在项目上单击"管理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?)

我该如何修复它?

WP8中的切换开关

如果你已经正确安装了Nuget包,那么下面的代码应该可以正常运行。

        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"/>

您需要在元素调用中使用名称空间标识符作为前缀。这样的

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

这应该能解决你的问题。