2016-08-31 13 views
0

私はUWPアプリケーションを持っています。 viewModelとDatacontextが正しく設定され、INotifiedPropertyChangedが正しく実装され、PropertyChangedが正しく起動され、バインディングモードがOneWayに設定されますが、プロパティがviewmodelのコンストラクタで変更された場合のみTextboxが更新されます。非同期メソッドloadJSONFromResourcesで変更されても、動作しません。ここでUWPバインディングは非同期メソッドでは機能しません

私のXAMLです:

<Page 
    x:Class="VokabelFileErstellen.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:VokabelFileErstellen" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 
    <Page.Resources> 
     <local:ViewModel x:Name="viewModel"/> 
    </Page.Resources> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource ResourceKey=viewModel}"> 
     <TextBlock Style="{StaticResource HeaderTextBlockStyle}" Margin="20,0,0,0" > 
      <Run Text="Anzahl Vokabeln: "/> 
      <Run Text="{Binding Count, Mode=OneWay}"/> 
     </TextBlock> 
    </Grid> 
</Page> 

これは私のViewModelです:

class ViewModel : INotifyPropertyChanged 
{ 
    private int count; 
    public List<Vokabel> Buecher { get; set; } 
    public int Count 
    { 
     get { return count; } 

     set 
     { 
      if (value != count) 
      { 
       count = value; 
       OnPropertyChanged("Count"); 
      } 
     } 
    } 

    public ViewModel() 
    { 
     Count = 1; 
    } 


    public async void jsonLaden() 
    { 
     FileOpenPicker picker = new FileOpenPicker 
     { 
      SuggestedStartLocation = PickerLocationId.ComputerFolder 
     }; 
     picker.FileTypeFilter.Add(".json"); 
     IStorageFile file = await picker.PickSingleFileAsync(); 
     loadJSONFromResources(file); 
    } 

    public async void loadJSONFromResources(IStorageFile file) 
    { 

     string json = await FileIO.ReadTextAsync(file); 

     Buecher = JsonConvert.DeserializeObject<List<Vokabel>>(json); 
     Count = Buecher.Count; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs("propertyName")); 
    } 

} 

、これは後ろの私のメインページのコードです:

public sealed partial class MainPage : Page 
{ 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     viewModel.jsonLaden(); 
    } 

} 

答えて

1

だけ

PropertyChanged(this, new PropertyChangedEventArgs("propertyName")); 
を変更0

PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 

でそれが働いているが、UIが得る唯一の通知は、 『プロパティ名』という名前のプロパティがinteadあなたがOnPropertyChangedをメソッド

に渡している名前で、変更されたことです