简体   繁体   English

添加的本地资源未显示在WPF页面中

[英]local resource added doesn't show up in WPF page

I have this simple WPF page in which I am adding a local resource, which has its definition in Page's backend file. 我有一个简单的WPF页面,其中添加了本地资源,该资源的定义在Page的后端文件中。

<Page x:Class="WindowsSampleApplication.ValueConverter"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WindowsSampleApplication"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="ValueConverter">
    <Page.Resources>
        <local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
    </Page.Resources>

    <Grid>
        <StackPanel Margin="10">
            <TextBox Name="txtValue" />
            <WrapPanel Margin="0,10">
                <TextBlock Text="Current value is: " />
                <TextBlock Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource YesNoToBooleanConverter}}"></TextBlock>
            </WrapPanel>
            <CheckBox IsChecked="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource YesNoToBooleanConverter}}" Content="Yes" />
        </StackPanel>
    </Grid>
</Page>

and this is the backend file for Page 这是Page的后端文件

namespace WindowsSampleApplication
{
    public partial class ValueConverter : Page
    {
        public ValueConverter()
        {
            InitializeComponent();
        }
        public class YesNoToBooleanConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                switch (value.ToString().ToLower())
                {
                    case "yes":
                        return true;
                    case "no":
                        return false;
                }
                return false;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value is bool)
                {
                    if ((bool)value == true)
                        return "yes";
                    else
                        return "no";
                }
                return "no";
            }
        }
    }
}

I have properly included namespace WindowsSampleApplication in Page level and the same namespace has definition for my resource. 我已经在Page级别正确包含名称空间WindowsSampleApplication ,并且相同的名称空间为我的资源定义了。 But am getting below error when added that local resource 但是添加该local resource时出现以下error

错误图片

I've just started with WPF tutorials from here but only thing I was trying rather than following tutorial was I added a Page instead of Window since I have created a Window already with earlier demos given there and hopefully I have converted everything that a Page needs to have instead of Window . 我刚刚从这里开始学习WPF教程,但是我只是尝试而不是按照教程学习的是添加页面而不是窗口,因为我已经创建了一个带有早先演示的Window ,并且希望我已经转换了页面所需的一切代替Window Anyone have idea on how to resolve this issue? 有人对如何解决此问题有想法吗?

UPDATE 更新

I've also tried wrapping <local..../> withing <ResourceDictionary> as I ready it in SO, but wasn't of much use and error still remained as it is. 我还在SO中准备好使用<ResourceDictionary>包装<local..../>时,但是并没有太大用处,并且仍然保持错误。

Since it looks like you are just trying out WPF, and you put the converter in the codebehind for no special reason, I would recommend the usual way for creating converters: 由于看起来您只是在尝试WPF,并且出于特殊原因将转换器放在代码后方,因此我建议使用通常的创建转换器的方法:

Create a new directory Converters in your WindowsSampleApplication project. 在您的WindowsSampleApplication项目中创建一个新目录Converters Then add a new class file for each converter... 然后为每个转换器添加一个新的类文件...

Now you know where to find your converters, and can easily reuse them. 现在,您知道在哪里可以找到转换器,并且可以轻松地重用它们。

PS: It's possible to put the converter in the codebehind as a nested class, but you should have a really good reason to do it. PS:可以将转换器作为嵌套类放在代码背后,但是您应该有一个很好的理由。 ( Binding converter as inner class? ) 将转换器绑定为内部类吗?

I think yout problem is that you have nested your converter inside ValueConverter page. 我认为您的问题是您已将转换器嵌套在ValueConverter页面中。

[EDIT] [编辑]

There is no possibility of instantiate nested classes so the only option will be to declare converter class directly in WindowsSampleApplication namespace. 实例化嵌套类是不可能的,因此唯一的选择是直接在WindowsSampleApplication名称空间中声明转换器类。

Updated code 更新的代码

namespace WindowsSampleApplication
{
    public partial class ValueConverter : Page
    {
        public ValueConverter()
        {
            InitializeComponent();
        }
        //Removed from here
    }
    public class YesNoToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch (value.ToString().ToLower())
            {
                case "yes":
                case "oui":
                    return true;
                case "no":
                case "non":
                    return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value == true)
                    return "yes";
                else
                    return "no";
            }
            return "no";
        }
    }
}

You need to declare YesNoToBooleanConverter directly in the namespace. 您需要直接在名称空间中声明YesNoToBooleanConverter From MSDN: XAML and Custom Classes for WPF 从MSDN:WPF的XAML和自定义类

Your custom class must be public and support a default (parameterless) public constructor. 您的自定义类必须是公共的,并支持默认(无参数)公共构造函数。 (See following section for notes regarding structures.) Your custom class must not be a nested class. (有关结构的注释,请参阅以下部分。)您的自定义类不得为嵌套类。 Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties. 嵌套类和常规CLR使用语法中的“点”会干扰其他WPF和/或XAML功能(例如附加属性)。

Link: 链接:

https://msdn.microsoft.com/en-us/library/ms753379(v=vs.100).aspx https://msdn.microsoft.com/zh-CN/library/ms753379(v=vs.100).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM