我试图将一些代码从Windows Phone 8 silverlight转换为UWP,但有些情况我不明白。
这是错误: 无法分配给属性' StyledText.MyControls.TextBlockProperties.StyledText'。 [线:58位置:58]
错误发生在绑定扩展属性StyledText
的情况下首先,我创建一个类来扩展TextBlock的控件属性
class_eval
XAML如下所示.....
Imports System.Text.RegularExpressions
Imports Windows.UI.Xaml.Markup
Namespace MyControls
Public NotInheritable Class TextBlockProperties
Public Shared Function GetStyledText(obj As DependencyObject) As String
Return DirectCast(obj.GetValue(StyledTextProperty), String)
End Function
Public Shared Sub SetStyledText(obj As DependencyObject, value As String)
obj.SetValue(StyledTextProperty, value)
End Sub
Public Shared ReadOnly StyledTextProperty As DependencyProperty = DependencyProperty.RegisterAttached("StyledText", GetType(String), GetType(TextBlock), New PropertyMetadata(Nothing, AddressOf StyledText_Changed))
Private Shared Sub StyledText_Changed(d As DependencyObject, args As DependencyPropertyChangedEventArgs)
Dim tb As TextBlock = DirectCast(d, TextBlock)
Dim text As String = DirectCast(args.NewValue, String)
If String.IsNullOrEmpty(text) OrElse Not Regex.IsMatch(text, "<Run.*?>.*?</Run>") Then
tb.Text = text
Return
End If
Dim formattedTextBlockXaml As String = "<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" & text & "</TextBlock>"
Try
Dim AuxXml = XamlReader.Load(formattedTextBlockXaml)
'Dim formattedTextBlock = DirectCast(AuxXml, TextBlock)
Dim formattedTextBlock As TextBlock = TryCast(AuxXml, TextBlock)
' detach parsed inlines from the view tree
Dim inlines = formattedTextBlock.Inlines.ToList()
formattedTextBlock.Inlines.Clear()
' add inlines to the specified text block
tb.Inlines.Clear()
For Each inline As Documents.Inline In inlines
tb.Inlines.Add(inline)
Next
Catch ex As Exception
'IGNORA OS ERROS
End Try
End Sub
End Class
End Namespace
最后填充数据的代码:
<Page
x:Class="StyledText.MainPage"
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"
xmlns:ext="using:StyledText.MyControls"
mc:Ignorable="d">
<Page.Resources>
<CollectionViewSource x:Key="AddressGroups"
IsSourceGrouped="True"/>
<DataTemplate x:Key="AddrBookItemTemplate">
<StackPanel VerticalAlignment="Top">
<TextBlock FontWeight="Bold" Text="{Binding FirstName}" />
<TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding Address}" />
<TextBlock Text="{Binding Phone}" />
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Gray" Width="480">
<Border BorderBrush="White" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding FirstName}" FontSize="28" Foreground="White"/>
<!--HERE'S THE ERROR-->
<TextBlock Grid.Row="1" ext:TextBlockProperties.StyledText="{Binding Address}" />
</Grid>
</Border>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
任何人都知道出了什么问题?
答案 0 :(得分:0)
我已经运行了您的代码,首先,您可能错过了XAML代码中的<ListView x:Name="listView1">
。
然后,在Namespace MyControls
中,您应该更改此代码:
Public Shared ReadOnly StyledTextProperty As DependencyProperty = DependencyProperty.RegisterAttached("StyledText", GetType(String), GetType(TextBlock), New PropertyMetadata(Nothing, AddressOf StyledText_Changed))
为:
Public Shared ReadOnly StyledTextProperty As DependencyProperty = DependencyProperty.RegisterAttached("StyledText", GetType(String), GetType(TextBlockProperties), New PropertyMetadata(Nothing, AddressOf StyledText_Changed))
你在这里得到你班级的类型是错误的。 以下是MSDN中的sample
顺便说一句,为什么你使用DependencyObject
来获取Text
的{{1}}?如果您只想获得TextBlock
的{{1}},则可以将代码Address
更改为TextBlock
。您似乎在项目中使用了ext:TextBlockProperties.StyledText="{Binding Address}"
或Text="{Binding Address}"
,但我没有找到它们的任何用途。这个UserControl
控件应该是其中之一吗?