【问题标题】:How To include assembly in WPF?如何在 WPF 中包含程序集?
【发布时间】:2013-10-31 16:41:59
【问题描述】:

我已经创建了一个用户控件,并在第二个用户控件中传递了它的程序集。我已经在 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>

第二个 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();
        }
    }
}

【问题讨论】:

    标签: c# .net wpf xaml user-controls


    【解决方案1】:

    试试这个:

    用户控件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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 2013-03-02
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多