2015-04-28 27 views
2

這是我第一次使用C#以及.net的一般體驗。我試圖理解MVVM以及它如何與我想構建的屏幕一起工作。ViewModels PropertyChanged事件後的屬性始終爲空

當我點擊不同的按鈕後改變它時,我的按鈕文本沒有更新。調試器顯示即使OnPropertyChanged函數被調用,PropertyChanged也始終爲空。

我已經看過一些文檔,並看過其他人的帖子提問類似的問題,但沒有任何解決方案爲我工作。我也在尋找一個解釋,所以我可以理解我做錯了什麼。

代碼:

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)); 
      } 
     } 


    } 
} 

謝謝

+0

你按一下按鈕創建一個新的視圖模型*每次*,設定值,然後將它扔了出去。當然,這不會影響您實際用作數據上下文的完全其他視圖模型。您應該更新該視圖模型。並使用命令在viewmodel *本身上執行數據更改*。 – poke

+0

好吧,這是有道理的,而且這是我的重大監督。因此,如何改變viewModel本身?我見過的一些例子涉及使用DataContext,但目前我似乎無法訪問它。 – Kyle

+1

這就是爲什麼我說使用命令;您將按鈕命令綁定到視圖模型,因此方法會在視圖模型實例本身上執行。因此,您可以在同一個內部執行視圖模型的實例。搜索'RelayCommand'或'DelegateCommand'。 – poke

回答

0

只有創建一個視圖模型,其範圍是頁面的生命(至少)。

你的代碼改成這樣:

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); 
    } 
} 
+0

謝謝你的帖子。它告訴我DataContext在當前上下文中不存在。我在其他地方看到了這個例子,但是我沒有能力引用DataContext? – Kyle

+0

所以它看起來像自從我做一個Xamarin.forms實現也許我應該使用BindingContext而不是DataContext? – Kyle

+0

@凱爾我已經完成了Windows手機,但沒有與Xamarin。數據上下文的概念(在xaml(WPF/Silverlight/WP)中是每一個控件和頁面都有一個上下文,如果沒有指定上下文,那麼它繼承父頁面上下文一直到頁面。只是將頁面的上下文設置爲ViewModel,最終在上述場景中將由控件訪問。 – OmegaMan

相關問題