VS没有在命名空间中检测转换器功能

时间:2015-06-08 16:20:15

标签: c# xaml

我正在使用VS2013并构建Windows应用商店应用。解析器未检测到species_to_chart_itemsource_converter函数。具体错误:

  

名称' ...'在命名空间中不存在' ...'

我甚至尝试过使用clr-namespace:Sustenance_V_1._0但是徒劳无功。我也尝试在新的.cs文件中定义新的命名空间,但似乎没有任何效果。我已经提到过这个问题:xaml parser is not detecting my converter

请帮忙。

PS:我在MainPage中定义了一个类物种但是没有在这里显示。

这是我的MainPage.xaml.cs文件:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Xaml.Shapes;
    using WinRTXamlToolkit.Controls.DataVisualization.Charting;


    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

    namespace Sustenance_V_1._0
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {


            //===========Converters==========//
            public class species_to_chart_itemsource_converter : IValueConverter
            {
                public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                {
                    var sp = parameter as species;
                    List<Population> data = new List<Population>();
                    data.Add(new Population() { Name = "Healthy", Amount = sp.healthy });
                    data.Add(new Population() { Name = "Healthy", Amount = sp.sick });
                    return data;

                }

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

            public MainPage()
            {
                this.InitializeComponent();

                add_species(all_species);
                link_members(all_species);
                LoadChartContents(all_species);
            }

            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
            }
       }
   }

这是MainPage.xaml文件的顶部:

<Page
    xmlns:Charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sustenance_V_1._0"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:DataVisualization="using:WinRTXamlToolkit.Controls.DataVisualization" xmlns:Controls="using:WinRTXamlToolkit.Controls" x:Name="page"

    xmlns:l="clr-namespace:Sustenance_V_1._0"

    x:Class="Sustenance_V_1._0.MainPage"
    mc:Ignorable="d">
    <Page.Resources>
        <Style TargetType="Image">
            <Setter Property="Stretch" Value="Uniform"/>
        </Style>
        <local:species_to_chart_itemsource_converter x:Key="chart_converter"/>

    </Page.Resources>

1 个答案:

答案 0 :(得分:0)

现在设置它的方式,它试图在species_to_chart_itemsource_converter命名空间中找到Sustenance_V_1._0,但它不在那里。你将它隐藏在MainPage类中。因此,您需要在其前面添加MainPage.species_to_chart_itemsource_converter或将转换器移到MainPage类之外(最好放在自己的文件中)。

namespace Sustenance_V_1._0
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            add_species(all_species);
            link_members(all_species);
            LoadChartContents(all_species);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }

    //This could be in a different file, but I placed it outside the class here just to illustrate.

    //===========Converters==========//
    public class species_to_chart_itemsource_converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var sp = parameter as species;
            List<Population> data = new List<Population>();
            data.Add(new Population() { Name = "Healthy", Amount = sp.healthy });
            data.Add(new Population() { Name = "Healthy", Amount = sp.sick });
            return data;

        }

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