WPF中‘UserControl’命名空间被当作类型使用的错误解决
问题解决:'UserControl' 是命名空间却被当作类型使用
错误原因
你的CardDay类所在的命名空间是WeatherApp.UserControl,而WPF内置的控件类型也叫UserControl(来自System.Windows.Controls命名空间),编译器被同名的命名空间和类型搞混了,把你要继承的UserControl当成了当前的命名空间,所以抛出错误。
两种解决办法
办法1:重命名命名空间(推荐)
把命名空间改成和控件类型不冲突的名字,比如WeatherApp.UserControls(加个s),同时同步修改XAML里的类路径。
修正后的后端代码(CardDay.xaml.cs):
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace WeatherApp.UserControls { public partial class CardDay : UserControl { public CardDay() { InitializeComponent(); } public string Day { get { return (string)GetValue(DayProperty); } set { SetValue(DayProperty, value); } } public static readonly DependencyProperty DayProperty = DependencyProperty.Register("Day", typeof(string), typeof(CardDay)); public string MaxNum { get { return (string)GetValue(MaxNumProperty); } set { SetValue(MaxNumProperty, value); } } public static readonly DependencyProperty MaxNumProperty = DependencyProperty.Register("MaxNum", typeof(string), typeof(CardDay)); public string MinNum { get { return (string)GetValue(MinNumProperty); } set { SetValue(MinNumProperty, value); } } public static readonly DependencyProperty MinNumProperty = DependencyProperty.Register("MinNum", typeof(string), typeof(CardDay)); public ImageSource Source { get { return (ImageSource)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(CardDay)); } }
修正后的XAML代码(CardDay.xaml):
<UserControl x:Class="WeatherApp.UserControls.CardDay" 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" Name="cardDay"> <Border CornerRadius="10" Width="85" Padding="10" Margin="0 0 10 0" BorderThickness="1"> <Border.Style> <Style TargetType="Border"> <Setter Property="Background" Value="White"/> <Setter Property="BorderBrush" Value="#e9e9e9e9"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="#03a9f4"/> </Trigger> </Style.Triggers> </Style> </Border.Style> <StackPanel> <TextBlock Text="{Binding Path=Day, ElementName=cardDay}" TextAlignment="Center" FontSize="14" FontWeight="SemiBold"/> <Image Source="{Binding Path=Source, ElementName=cardDay}" Width="30" Margin="0 10 0 10"/> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <TextBlock Text="{Binding Path=MaxNum, ElementName=cardDay}" TextAlignment="Center" FontWeight="SemiBold" Margin="0 0 8 0"/> <TextBlock Text="{Binding Path=MinNum, ElementName=cardDay}" TextAlignment="Center" FontWeight="SemiBold" Background="#7a7a7a"/> </StackPanel> </StackPanel> </Border> </UserControl>
办法2:使用完全限定类型名
如果不想修改命名空间,直接在继承时写出UserControl的完整命名空间路径,明确告诉编译器你要引用的是控件类型:
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace WeatherApp.UserControl { // 这里用System.Windows.Controls.UserControl代替UserControl public partial class CardDay : System.Windows.Controls.UserControl { public CardDay() { InitializeComponent(); } // 后面的依赖属性代码保持不变 public string Day { get { return (string)GetValue(DayProperty); } set { SetValue(DayProperty, value); } } public static readonly DependencyProperty DayProperty = DependencyProperty.Register("Day", typeof(string), typeof(CardDay)); public string MaxNum { get { return (string)GetValue(MaxNumProperty); } set { SetValue(MaxNumProperty, value); } } public static readonly DependencyProperty MaxNumProperty = DependencyProperty.Register("MaxNum", typeof(string), typeof(CardDay)); public string MinNum { get { return (string)GetValue(MinNumProperty); } set { SetValue(MinNumProperty, value); } } public static readonly DependencyProperty MinNumProperty = DependencyProperty.Register("MinNum", typeof(string), typeof(CardDay)); public ImageSource Source { get { return (ImageSource)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(CardDay)); } }
内容的提问来源于stack exchange,提问作者FahadShaheb
相关产品推荐
相关产品推荐

