Binding converter to XAML in windows phone 8 app

半世苍凉 提交于 2019-12-30 11:11:09

问题


my .xaml page code:

<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;
        }
    }
}

it is showing an error

"the name NotConverter does not exists in the namespace clr-namespace:MVVM1Test.Resources.Converters"

But i have added the converter class also to the solution.


回答1:


When you place items in a resource dictionary, you must give them keys. The x:Name property is redundant (unless you need to access the object from code behind).

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

Also, the error you're getting could be a design-time problem. Try to compile and run after the change I suggested.




回答2:


This may help you

xmlns:MyAppConverters="clr-namespace:MyApp.Converters"

and

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

I have given the example you can change the class name as yours




回答3:


I had a similar name space problem. For me, restarting visual studio seemed to solve the problem. Ofcourse, you must ensure the namespace is correctly defined in your IValueConverter Class e.g namespace MVVM1Test.Resources.Converters

Save and restart visual studio.



来源:https://stackoverflow.com/questions/19510556/binding-converter-to-xaml-in-windows-phone-8-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!