2013-10-22 42 views
2

我的.xaml頁面代碼綁定轉換器,以在XAML的Windows Phone 8應用

<phone:PhoneApplicationPage 
    x:Class="MVVM1Test.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:converter="clr-namespace:MVVM1Test.Resources.Converters" 
    mc:Ignorable="d" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    shell:SystemTray.IsVisible="True"> 

    <phone:PhoneApplicationPage.Resources> 
     <converter:NotConverter x:Name="not"></converter:NotConverter> 
    </phone:PhoneApplicationPage.Resources> 

    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Gray"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
    </Grid> 

</phone:PhoneApplicationPage> 

notconverter.cs

namespace MVVM1Test.Resources.Converters 
{ 
    public class NotConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return !(bool)value; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return !(bool)value; 
     } 
    } 
} 

它顯示一個錯誤

「NotConverter的名稱不存在於名稱空間clr-namespace:MVVM1Test.R中esources.Converters「

但我已經將轉換器類也添加到解決方案。

回答

0

這可能會幫助你

的xmlns:MyAppConverters = 「CLR的命名空間:MyApp.Converters」

<phone:PhoneApplicationPage.Resources> 
    <MyAppConverters:CustomConverter x:Key="customConverter"/>  
</phone:PhoneApplicationPage.Resources> 

我已經給了你可以改變類名和你的例子

+0

這個我已經添加到我的xaml文件中: xmlns:converter =「clr-namespace:MVVM1Test.Resources.Converters」 –

2

將項目放置在資源字典中時,必須爲它們提供密鑰。 x:Name屬性是多餘的(除非需要從後面的代碼訪問對象)。

<converter:NotConverter x:Key="not" /> 

此外,您遇到的錯誤可能是設計時問題。嘗試在我建議的更改後編譯並運行。

+1

編譯該項目有幫助! –

+0

這解決了我的問題 – adrian4aes

0

我有一個類似的名稱空間問題。對我而言,重啓視覺工作室似乎解決了這個問題。 Ofcourse,你必須確保命名空間在您的IValueConverter類正確定義 e.g命名空間MVVM1Test.Resources.Converters

保存並重新啓動Visual Studio。