如何调试INPC属性setter没有触发?

时间:2016-01-24 15:55:46

标签: c# properties windows-phone-8.1 inotifypropertychanged

概述: 我已经使用INPC设置了一个属性,该属性在<form name="register" id="signup" method="post" onsubmit="return validateForm()" action="register_user.php"> <div class="header"> <h3>Registration</h3> <p>Complete this form to register.<br/>* required field</p> </div> <div class="sep"></div> <div class="inputs"> <input type="text" placeholder="Name" name="name" autofocus />* <input type="text" placeholder="Surname" name="surname" autofocus />* <input type="text" placeholder="Email" name="email" autofocus />* <input type="password" placeholder="Password" name="password" />* <div class="sep"></div> <div id="messages"></div> <input type="submit" id="submit" name="Submit" value="CONFIRM REGISTRATION"> </div> </form> 后面的视图代码中调用页面导航。此属性绑定到绑定视图中列表视图的 SelectedItem 属性。

INPC实现继承自MainViewModel类,其实现如下,https://gist.github.com/BrianJVarley/4a0890b678e037296aba

问题:

当我从列表视图中选择一个项目时,属性ViewModelBase setter不会触发。此属性绑定到列表视图的 SelectedItem 属性。

调试步骤:

  • 在列表视图属性中检查SelectedItem的绑定名称,该名称与MainViewModel中的属性名称相同。
  • 运行解决方案并检查输出窗口中是否存在任何绑定错误。
  • 在SelectedCouncilItem上放置一个断点,当我从列表视图中选择时,它不会被触发。
  • 检查视图的数据上下文设置,确认视图已设置为MainViewModel的数据上下文。

问题:

有谁知道我在调试问题时可以采取的其他步骤,或者问题可能是什么?

代码:

MainPage - (列表视图)

SelectedCouncilItem

MainViewModel - (摘要)

    <Grid x:Name="ContentPanel"
          Grid.Row="1"
          Margin="12,0,12,0">
        <phone:LongListSelector x:Name="MainLongListSelector"
                                Margin="0,0,-12,0"
                                ItemsSource="{Binding Items}"
                                SelectedItem="{Binding SelectedCouncilItem}">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17">
                        <TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}"
                                   Text="{Binding CouncilAcronym}"
                                   TextWrapping="Wrap" />
                        <TextBlock Margin="12,-6,12,0"
                                   Style="{StaticResource PhoneTextSubtleStyle}"
                                   Text="{Binding CouncilFullName}"
                                   TextWrapping="Wrap" />
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>

ViewModelBase - (详细说明INPC实现)

namespace ParkingTagPicker.ViewModels
{
    public class MainViewModel : ViewModelBase
    {


        //Dependency Injection private instances
        private INavigationCallback _navCallBack = null;

        public MainViewModel()
        {
            this.Items = new ObservableCollection<ItemViewModel>();

        }




        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items  collection.
        /// </summary>
        public void LoadCouncilNamesData()
        {
            this.Items.Add(new ItemViewModel() { ID = "6", CouncilAcronym = "WTC", CouncilFullName = "Wicklow Town Council"});
            this.Items.Add(new ItemViewModel() { ID = "7", CouncilAcronym = "TS", CouncilFullName = "Tallaght Stadium" });
            this.Items.Add(new ItemViewModel() { ID = "8", CouncilAcronym = "GS", CouncilFullName = "Greystones" });

            this.IsDataLoaded = true;
        }



        public ObservableCollection<ItemViewModel> Items { get; private set; }

        public bool IsDataLoaded { get; private set; }

        private ItemViewModel _selectedCouncilItem;
        public ItemViewModel SelectedCouncilItem
        {          
            get
            {
                return this._selectedCouncilItem;
            }
            set
            {
                this.SetProperty(ref this._selectedCouncilItem, value, () => this._selectedCouncilItem);

                if (_selectedCouncilItem != null)
                {
                    _navCallBack.NavigateTo(_selectedCouncilItem.ID);
                }
            }
        }


        public INavigationCallback NavigationCallback
        {
            get { return _navCallBack; }
            set { _navCallBack = value; }
        }


    }
}

1 个答案:

答案 0 :(得分:3)

控件存在问题。请尝试使用自定义LongListSeletor

public class ExtendedLongListSelector : Microsoft.Phone.Controls.LongListSelector
{
    public ExtendedLongListSelector()
    {
        SelectionChanged += LongListSelector_SelectionChanged;
    }

    void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SelectedItem = base.SelectedItem;
    }

    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(LongListSelector),
                            new PropertyMetadata(null, OnSelectedItemChanged));

    private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = (LongListSelector)d;
        selector.SelectedItem = e.NewValue;
    }   


    public new object SelectedItem
    {
        get { return GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }
}

并在XAML中用现有的List替换它。

   xmlns:controls="clr-namespace:ProjectName.FolderName"

   <controls:ExtendedLongListSelector  x:Name="MainLongListSelector"
                                Margin="0,0,-12,0"
                                ItemsSource="{Binding Items}"
                                SelectedItem="{Binding SelectedCouncilItem}">
   </controls:ExtendedLongListSelector>