2
我正在構建一個wp7應用程序。Silverlight 4:用一點邏輯顯示數據的模式?
我有一個UserControl
,顯示新聞文章標題,預告片和圖像。整個類是很短:
public partial class StoryControl : UserControl
{
public Story Story { get; private set; }
public StoryControl()
{
InitializeComponent();
}
internal StoryControl(Story story) : this()
{
this.Story = story;
Teaser.Text = story.Teaser;
Headline.Text = story.Title;
if (story.ImageSrc == null)
{
Thumbnail.Visibility = Visibility.Collapsed;
} else
{
Thumbnail.Source = new BitmapImage(story.ImageSrc);
}
}
}
以及相應的XAML:
<Grid x:Name="LayoutRoot" Background="Transparent" Margin="0,0,0,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image x:Name="Thumbnail" Grid.Column="0" Width="89" HorizontalAlignment="Left" VerticalAlignment="Top" />
<!-- sometimes there's a hanging word in the headline that looks a bit awkward -->
<TextBlock x:Name="Headline" Grid.Column="1" Grid.Row="0" Style="{StaticResource PhoneTextAccentStyle}" TextWrapping="Wrap" HorizontalAlignment="Left" FontSize="23.333" VerticalAlignment="Top" />
<TextBlock x:Name="Teaser" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}" TextWrapping="Wrap" VerticalAlignment="Top" Width="384"/>
</Grid>
有沒有辦法做用更少的代碼隱藏和XAML多?使用綁定的一些方法可將Headline
和Teaser
的文本綁定到Story
的屬性,而如果Story
爲空則不會崩潰?
關於該圖像?我在那裏有一些邏輯;有沒有辦法在XAML中自動執行該操作,或者我堅持使用C#?
看起來不錯,雖然我有'booleanToVisiblityConverter' 。我需要用靜態資源做些什麼? – 2010-11-04 18:37:40
已編輯爲將實際轉換器包含在靜態資源中。通常你會聲明這個更高級別,比如Window或App – 2010-11-04 22:09:20
好的。我收到一個無法找到的錯誤。 Windows Phone 7不支持嗎? – 2010-11-05 00:36:07