none
用户控件设置Image的Source报XmlParseException RRS feed

  • 问题

  • 用户控件XAML如下
    <UserControl x:Class="SPOSS2012.SpossUserControl.ImageTextBox"
                 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" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <Border BorderThickness="1" BorderBrush="Black">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="10*"/>
                        <ColumnDefinition Width="50*"/>
                    </Grid.ColumnDefinitions>
                    <Image Name="userimage"  IsHitTestVisible="False" Grid.Column="0"></Image>
                    <TextBox BorderThickness="0" Name="usertext"  Grid.Column="1"></TextBox>
                </Grid>
            </Border>
        </Grid>
    </UserControl>

    CS:

    public partial class ImageTextBox : UserControl
        {
    
            public ImageTextBox()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 图片依赖属性
            /// </summary>
            public static DependencyProperty ImageSourceProperty = DependencyProperty.Register
            (
                "ImageSource",
                typeof(BitmapImage),
                typeof(ImageTextBox),
                new FrameworkPropertyMetadata(
                    new PropertyChangedCallback(OnImageSourceChanged)
                    )
            );
    
            /// <summary>
            /// 图片资源成员
            /// </summary>
            public BitmapImage ImageSource
            {
                get { return (BitmapImage)GetValue(ImageSourceProperty); }
                set { SetValue(ImageSourceProperty, value); }
            }
    
            /// <summary>
            /// 图片资源发生改变
            /// </summary>
            /// <param name="ob"></param>
            /// <param name="e"></param>
            public static void OnImageSourceChanged(DependencyObject ob, DependencyPropertyChangedEventArgs e)
            {
                ((ImageTextBox)ob).userimage.Source = ((ImageTextBox)ob).ImageSource;
            }
    
    
            /// <summary>
            /// 文本依赖属性
            /// </summary>
            public static DependencyProperty TextProperty = DependencyProperty.Register
            (
                "Text",
                typeof(string),
                typeof(ImageTextBox),
                new FrameworkPropertyMetadata(
                    new PropertyChangedCallback(OnTextChanged))
            );
    
            /// <summary>
            /// 文本成员
            /// </summary>
            public string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }
    
            /// <summary>
            /// 文本发生改变
            /// </summary>
            /// <param name="ob"></param>
            /// <param name="e"></param>
            public static void OnTextChanged(DependencyObject ob, DependencyPropertyChangedEventArgs e)
            {
                ((ImageTextBox)ob).usertext.Text = ((ImageTextBox)ob).Text;
            }
    
        }

    设计时刻图片都可以加载,以及显示:

    运行时就报错,提示:

    2012年4月29日 5:23

答案

  • 已解决,谢谢关注,

    /// <summary>
           
    /// 图片资源成员
           
    /// </summary>
           
    public BitmapImage ImageSource
           
    {
               
    get { return (BitmapImage)GetValue(ImageSourceProperty); }
               
    set { SetValue(ImageSourceProperty, value); }
           
    }
    应为ImageSource类型

    • 已标记为答案 猪宝贝 2012年4月29日 8:29
    2012年4月29日 8:29