laserman
laserman

Reputation: 233

UWP Binding in Textbox works does not work in async method

I have a UWP Application. The viewModel and the Datacontext are set correctly, INotifiedPropertyChanged is implemented correctly, PropertyChanged is firing correctly, Binding Mode is set to OneWay but the Textbox is only updated if the property is changed in the constructor of the viewmodel. If it is changed in the async method loadJSONFromResources it doesn't work.

Here is my 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>

This is my 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"));
    }

}

and this is my MainPage code behind:

public sealed partial class MainPage : Page
{

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

}

Upvotes: 0

Views: 702

Answers (1)

RTDev
RTDev

Reputation: 866

just change

PropertyChanged(this, new PropertyChangedEventArgs("propertyName"));

with

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

it is workin', but the only notification UI gets is that property named "propertyName" has changed, intead of the name you are passing to OnPropertyChanged method

Upvotes: 1

Related Questions