UWP XAMLUserControl:在Xaml页面中使用时发生设计时错误,但运行时正常

时间:2018-11-19 04:41:28

标签: c# xaml uwp user-controls design-time

一个C#应用程序和UserControl 该控件是一个包含TextBox的StackPanel。相当琐碎,但我已将实际控制中的其他细节排除在外,以解决此问题。 控件的一个属性Text设置了TextBlock的Text属性。

应用运行时,控件的行为与预期的一样。如页面XAML中所指定,已正确设置测试应用程序的XAML页面上控件实例的Text属性。

但是,尽管在Visual Studio中设计页面时,从XAML选项卡设置的Text属性不会反映在“设计”选项卡中。

UserControl XAML:

<UserControl 
x:Name="customTextBlock"
x:Class="MyToolkit.CustomTextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="340">

<StackPanel  x:Name="spStackPanel"    Margin="0" Padding="0"
                                Width="340" Height="30" 
                                VerticalAlignment="Center" 
                                HorizontalAlignment="Center"
                                 >
    <TextBlock x:Name="tbTextBlock" Text="{Binding ElementName=customTextBlock, Path=Text,Mode=OneWay}"    
                                Height="30"  Margin="0,4,0,0" Padding="0"
                                VerticalAlignment="Center" 
                                HorizontalAlignment="Center"
                                TextAlignment="Center" />
</StackPanel>

隐藏代码:

namespace MyToolkit
{
public sealed partial class TextBlockWithBgColor : UserControl, INotifyPropertyChanged
{  
    public TextBlockWithBgColor()
    {
        this.InitializeComponent();
    }

    public static readonly DependencyProperty TextProperty =
      DependencyProperty.Register("Message", typeof(string), typeof(TextBlock), new PropertyMetadata("Fred", null));


    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { if (Text != value) { SetValue(TextProperty, value); NotifyPropertyChanged("TextProperty"); } }
    } 

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}

默认值Fred在设计时显示:

enter image description here

页面XAML上显示为错误(无法找到属性…):

enter image description here

对此表示任何帮助。

0 个答案:

没有答案