2010-09-02 46 views
1

我試圖做一個Facebook的客戶端和我有XAML文件是這樣的:INotifyPropertyChanged的一個用戶控件返回一個無限循環

<UserControl x:Class="UISkripsi3.Sidebar" 
    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:UISkripsi3="clr-namespace:UISkripsi3" 
    xmlns:local="clr-namespace:UISkripsi3"> 
<UserControl.Template> 

     <ControlTemplate TargetType="{x:Type UISkripsi3:Sidebar}"> 
      <DataTemplate x:Key="StatusAndOther" DataType="{x:Type local:NewsFeedResources}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition/> 
          <RowDefinition/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition/> 
         </Grid.ColumnDefinitions> 
         <StackPanel Orientation="Vertical"> 
          <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="10"> 
           <Image x:Name="ProfilePicture" Source="{Binding Path=ProfilePict}" Stretch="Fill" Width="80" Height="80" VerticalAlignment="Top"></Image> 
           <StackPanel Orientation="Vertical" Margin="5"> 
            <TextBlock Text="{Binding Path=UserName}" Style="{StaticResource UserNameStyle}" VerticalAlignment="Top"/> 
            <TextBlock Text="{Binding Path=Message}" Foreground="White" TextWrapping="Wrap" Width="400" VerticalAlignment="Top" HorizontalAlignment="Left"/>          
            <StackPanel Orientation="Horizontal"> 
             <Image x:Name="Icon" Source="{Binding Path=icon}" Width="10" Height="10"></Image> 
             <TextBlock Text=" via " Foreground="White"></TextBlock> 
             <TextBlock Text="{Binding Path=attribution}" Foreground="White"></TextBlock> 
             <StackPanel Orientation="Vertical"> 
             <Button x:Name="LikeButton" VerticalAlignment="Top" Content="like" IsEnabled="{Binding canLike}" ToolTip="Like" Click="LikeButtonClick"> 
              <Button.Style> 
              <Style BasedOn="{StaticResource LikeButtonStyle}" TargetType="Button"> 
               <Setter Property="Visibility" Value="Visible" /> 
               <Style.Triggers> 
               <DataTrigger Binding="{Binding hasLiked}" Value="True"> 
                <Setter Property="Visibility" Value="Collapsed" /> 
               </DataTrigger> 
               </Style.Triggers> 
              </Style> 
              </Button.Style> 
             </Button> 
             <Button x:Name="RemoveLikeButton" VerticalAlignment="Center" Content="remove like" IsEnabled="{Binding hasLiked}" ToolTip="Remove Like" Click="RemoveLikeButtonClick"> 
              <Button.Style> 
              <Style BasedOn="{StaticResource RemoveLikeButtonStyle}" TargetType="Button"> 
               <Setter Property="Visibility" Value="Visible" /> 
               <Style.Triggers> 
               <DataTrigger Binding="{Binding hasLiked}" Value="False"> 
                <Setter Property="Visibility" Value="Collapsed" /> 
               </DataTrigger> 
               </Style.Triggers> 
              </Style> 
              </Button.Style> 
             </Button> 
            </StackPanel> 

            </StackPanel> 
           </StackPanel>        
          </StackPanel> 
        </StackPanel> 
          <StackPanel Orientation="Vertical"> 
         <ListView Name="NewsFeedList" Width="700" Height="500" Margin="10" 
            ItemsSource="{Binding Source={x:Static local:Sidebar.newsFeedResourcesCoba2}}" 
            Background="Black" 
            ItemTemplateSelector="{StaticResource myDataTemplateSelector}" Focusable="False">        
         </ListView> 
        </StackPanel>     
        </Grid>     
       </DataTemplate> 
     </ControlTemplate> 

</UserControlTemplate> 

</UserControl> 

然後,我有這樣的

public class NewsFeedResources : System.Windows.Controls.UserControl, INotifyPropertyChanged 

{  
     public event PropertyChangedEventHandler PropertyChanged; 

     public string UserName { get; set; } 
     public string Id { get; set; } 
     public string Message { get; set; } 
     public string IdPost { get; set; } 
     public byte[] ProfilePict { get; set; } 
     public string DestinationName { get; set; } 
     public string to { get; set; } 
     public byte[] UploadedPict { get; set; } 
     public byte[] Attachment { get; set; } 
     public string Description { get; set; } 
     public string type { get; set; } 
     public string nameWallPost { get; set; } 
     public string captionWallPost { get; set; } 
     public string link { get; set; } 
     public byte[] icon { get; set; } 
     public string attribution { get; set; } 
     public bool canLike 
    { 
     get { return this.canLike; } 
     set 
     { 
      if (this.canLike != value) 
      { 
       this.canLike = value; 
       this.OnPropertyChanged("canLike"); 
      } 
     } 
    } 
    public bool hasLiked 
    { 
     get { return this.hasLiked; } 
     set 
     { 
      if (this.hasLiked != value) 
      { 
       this.hasLiked = value; 
       this.OnPropertyChanged("hasLiked"); 
      } 
     } 
    } 
    private void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

} 

類下面是當我們點擊LikeButton時會出現什麼情況

private void LikeButtonClick(object sender, RoutedEventArgs e) 
    { 

     Button btn = (Button)sender; 
     NewsFeedResources obj = btn.DataContext as NewsFeedResources; 
     if (null != obj) 
     { 
      //MessageBox.Show(obj.IdPost); 
      webClient.DownloadString(obj.IdPost); 
      obj.canLike = false; 
      obj.hasLiked = true; 
     } 
    } 

我想問一下怎樣才能將UI自動從李ke to與INotifyPropertyChanged不同,反之亦然,因爲在我的源代碼中,它遇到了無限循環錯誤get {return this.canLike; }。感謝您的幫助球員

+0

如果你遵循標準的C#命名約定,這應該會少得多。屬性應該是Pascal大小寫的,變量應該是駱駝大小寫的,如果它們是私有字段,就使用可選的_前綴(就像Robaticus的例子)。 – stombeur 2010-09-02 13:48:59

回答

3

我認爲這是因爲你沒有爲canLike設置的私有變量。你得到的是無限的呼喚。

做這樣的事情:

private bool _canLike; 
public bool canLike 
{ 
    get { return this._canLike; } 
    set 
    { 
     if (this._canLike != value) 
     { 
      this._canLike = value; 
      this.OnPropertyChanged("canLike"); 
     } 
    } 

它看起來像你想使用其中的私有成員變量是隱式聲明的自動屬性的方法:在你需要一個

public bool canLike { get; set; } 

正常的屬性(以及它的私有成員變量)。

+0

對我而言,這是最好的方法 – 2016-01-07 08:52:28