0

我创建了一个用户控件并在第二个用户控件中传递了它的程序集。我在 Xaml 文件中编写了正确的命名空间,但仍然说CLR clr-namespace is not defined in assembly如何解决这个问题?谁能帮我。我的代码是:

第一个用户控件 XAML 文件:

<xmlns:local="clr-namespace:DesktopApplication.Roles"> <UserControl.Resources> <local:StringToColorConverter x:Key="StringToColorConverter"/> </UserControl.Resources>

第二个用户控件 .CS 文件:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace DesktopApplication.Admin_Roles
{


    public partial classCategoryTab : UserControl
    {

        public CategoryTab()
        {
            InitializeComponent();


        }


    public class Data
    {
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Color { get; set; }
        public bool isactive { get; set; }
    }
    public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (Color)ColorConverter.ConvertFromString((string)value);
        }

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

1 回答 1

0

尝试这个 :

用户控件1:

<UserControl x:Class="Sample.UserControl1"
             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" 
             mc:Ignorable="d" xmlns:sample="clr-namespace:Sample"
             d:DesignHeight="300"
             d:DesignWidth="300">
        <UserControl.Resources>
        <sample:StringToColorConverter x:Key="ddd"></sample:StringToColorConverter>
        </UserControl.Resources>
        <Grid>            
    </Grid>
</UserControl>

用户控件2:

using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Sample
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }
    }

    public class Data
    {
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Color { get; set; }
        public bool isactive { get; set; }
    }
    public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (Color)ColorConverter.ConvertFromString((string)value);
        }

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

我已将 Data & StringToColorConverter 移至 Sample 命名空间,然后在“编译”后一切正常。

而且您的代码中也存在名称空间不匹配:

正确的:

<xmlns:local="clr-namespace:DesktopApplication.Admin_Roles"> <UserControl.Resources>

错误的:

<xmlns:local="clr-namespace:DesktopApplication.Roles"> <UserControl.Resources>
于 2013-11-26T11:50:31.570 回答