Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

如何在Windows Phone手机上调试Data Binding问题

From Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee -)
 
Line 1: Line 1:
[[Category:Lang-Chinese]][[Category:Windows Phone]]
+
[[Category:Lang-Chinese]][[Category:Files/Data on Windows Phone]][[Category:Debugging on Windows Phone]][[Category:Windows Phone 7.5]][[Category:XAML]][[Category:Code Snippet]]
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
Line 79: Line 79:
  
 
接着在方法 {{Icode|Convert()}}中,你可以看到数据的到达.
 
接着在方法 {{Icode|Convert()}}中,你可以看到数据的到达.
<!-- Translation --> [[en:How to debug data binding problems on Windows Phone]][[Category:Code Snippet]] [[Category:Data Binding]] [[Category:XAML]] [[Category:Debugging]] [[Category:Files/Data on Windows Phone]][[Category:Windows Phone 7.5]]
+
<!-- Translation --> [[en:How to debug data binding problems on Windows Phone]]

Latest revision as of 06:38, 28 June 2013

WP Metro Icon File.png
WP Metro Icon Bug.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

兼容于
文章
翻译:
Hazel Xie
最后由 hamishwillee 在 28 Jun 2013 编辑

这篇文章将会介绍如何在数据绑定到控件之前调试所绑定的数据的问题

[edit] 介绍

Data binding 允许你在XAML中绑定需要的数据. 但是这种绑定非常的直接,而当你的绑定出现问题后,很难定位到问题的所在.

最简单的方法,是创建一个Value Convertor(IValueConverter), 这个Convertor会在数据被访问前获取到数据 .只要在Convertor中设置断点,就可以确认数据是否传输正确,同时,包含所需要的数据.

Tip.pngTip: 如果Convertor没有被触发,那么,在数据绑定到控件之前就已经发生了错误, 可能是数据源的定义或者是不正确的DataContext。

[edit] 可执行的例子

首先,我们来创建一个ListBox的例子:

<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

让我们来创建一个派生自借口IValueConverter的Convertor:

public class DatabindingDebugConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value;
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value;
}
}

然后,你可以定义这个Convertor作为静态资源到XAML中:

<phone:PhoneApplicationPage.Resources>
<local:DataBindingDebugConverter x:Key="DatabindingDebugConverter"/>
</phone:PhoneApplicationPage.Resources>

同时,需要把Convertor绑定到指定的Binding上:

<TextBlock Text="{Binding LineOne, Converter={StaticResource DataBindingDebugConverter}}" 
TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

注意,你必须已经应用了local作为xmlns:

xmlns:local="clr-namespace:EncryptionSample"

接着在方法 Convert()中,你可以看到数据的到达.

This page was last modified on 28 June 2013, at 06:38.
137 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×