这是我第一次使用C#和一般.net的经验。我试图了解MVVM及其如何与我要构建的屏幕一起使用。

单击其他按钮后进行更改时,按钮文本未更新。调试器显示即使调用OnPropertyChanged函数,PropertyChanged始终为null。

我查看了一些文档,并看了其他人问类似问题的帖子,但这些解决方案都不适合我。我也在寻找一种解释,这样我就可以了解我在做错什么。

代码:

MonthViewPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Mobile_Release_POC_4.MonthViewPage"
             xmlns:local="clr-namespace:Mobile_Release_POC_4;assembly=Mobile_Release_POC_4"
             xmlns:telerikInput="clr-namespace:Telerik.XamarinForms.Input;assembly=Telerik.XamarinForms.Input"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">

            <ContentPage.BindingContext>
                 <local:WeekViewDatesViewModel MiddleWeekViewDate='{x:Static sys:DateTime.Now}' />
            </ContentPage.BindingContext>

            <StackLayout>
               <Label Text="Monday, April 27, 2015"
               HorizontalOptions="Center"></Label>

               <Grid Padding="0" ColumnSpacing="-2">

                     <Grid.RowDefinitions>
                           <RowDefinition Height="75" />
                     </Grid.RowDefinitions>

                     <Grid.ColumnDefinitions >
                          <ColumnDefinition Width="50" />
                     </Grid.ColumnDefinitions>

                     <Button x:Name="monthViewDateButton1"
                          Text="{Binding MiddleWeekViewDate}"
                          FontSize="10"
                          Grid.Row="0"
                          Grid.Column="0"
                      />
                     <Button x:Name="monthViewDateButton2"
                     Text="Tue 4/28"
                     FontSize="10"
                     Grid.Row="0"
                     Grid.Column="1"
                     Clicked="OnButtonClicked"
                     />

               </Grid>


         </StackLayout>

MonthViewPage.xaml.cs
using Xamarin.Forms;

namespace Mobile_Release_POC_4
{
public partial class MonthViewPage : ContentPage
{
    public MonthViewPage ()
    {
        InitializeComponent();

    }


    void OnButtonClicked(object sender , EventArgs args){

        WeekViewDatesViewModel dc = new WeekViewDatesViewModel ();

        dc.MiddleWeekViewDate = new DateTime (2014, 2, 2);
    }
}

WeekViewDatesViewModel.cs
    namespace Mobile_Release_POC_4
{
    public class WeekViewDatesViewModel : INotifyPropertyChanged
    {
        DateTime middleWeekViewDate;


        public event PropertyChangedEventHandler PropertyChanged;


        public DateTime MiddleWeekViewDate
        {
            set
            {
                if (middleWeekViewDate != value)
                {
                    middleWeekViewDate = value;
                    OnPropertyChanged("MiddleWeekViewDate");
                }
            }
            get
            {
                return middleWeekViewDate;
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }


    }
}

谢谢

最佳答案

仅创建一个ViewModel,其范围是页面的生命周期(至少)。

将代码更改为此:

public partial class MonthViewPage : ContentPage
{

    public WeekViewDatesViewModel VM { get; set; }

    public MonthViewPage ()
    {
        DataContext = VM = new  WeekViewDatesViewModel();
        InitializeComponent();
    }


    void OnButtonClicked(object sender , EventArgs args){

        VM.MiddleWeekViewDate = new DateTime (2014, 2, 2);
    }
}

关于c# - PropertyChanged事件之后的ViewModels属性始终为Null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29922676/

10-11 15:41