在 Windows Phone 8 应用程序中将转换器绑定到 XAML [英] Binding converter to XAML in windows phone 8 app

查看:25
本文介绍了在 Windows Phone 8 应用程序中将转换器绑定到 XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 .xaml 页面代码:

<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.Resources.Converters 中不存在"

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

推荐答案

在资源字典中放置项目时,必须给它们提供键.x:Name 属性是多余的(除非您需要从后面的代码访问对象).

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.

这篇关于在 Windows Phone 8 应用程序中将转换器绑定到 XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆