2013-10-31 146 views
0

我已經創建了一個usercontrol並在第二個usercontrol中傳遞了它的程序集。我已經在Xaml文件中編寫了正確的命名空間,但它仍然在說CLR clr-namespace is not defined in assembly如何解決此問題?誰能幫我。我的代碼是:如何在WPF中包含程序集?

首先用戶控件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(); 
     } 
    } 
} 

回答

0

試試這個:

的UserControl1:

<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> 

UserControl2:

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

我提出數據& StringToColorConverter 「編譯」 後,樣品名稱空間,然後一切工作正常。

而且也有在你的代碼命名空間不匹配:

正確:

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

錯誤:

<xmlns:local="clr-namespace:DesktopApplication.Roles"> <UserControl.Resources> 
相關問題