0

ユーザーコントロールを作成し、そのアセンブリを2番目のユーザーコントロールに渡しました。Xamlファイルに正しい名前空間を書きましたが、それでもCLR clr-namespace is not defined in assemblyこの問題を解決するにはどうすればよいですか? 誰でも私を助けることができますか?私のコードは次のとおりです。

最初の UserControl XAML ファイル:

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

2 番目の UserControl .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 に答える