Task is resetting a parameter at each entry to function

erdulterdult Member ✭✭
edited November 2018 in Xamarin.Forms

I have asked same question to StackOverflow, but couldn't get a response. Hopefully you guys can help me out!

I have a Task at each call to the Task the Editor Text is set to empty string. I am using FreshMvvm. Can you explain why at the entry to Task DecreaseDate EditorText is set?

Following is within DayLogPageModel.cs

async Task DecreaseDate()
{
    _dayLogs.SetLog(_logDate.ConvertToIntShort(), EditorText);

    _logDate.AddDays(-1);
    LogDateString = _logDate.ConvertToStrShort();
    EditorText = _dayLogs.GetLog(_logDate.ConvertToIntShort());
}
private string _editorText;
public String EditorText
{
    get => _editorText;
    set
    {
        _editorText = value;
        RaisePropertyChanged("EditorText");
    }
}

and this is DayLogPage.xaml

             xmlns:local="clr-namespace:workoutDiaryGridDesignPage1"
             x:Class="workoutDiaryGridDesignPage1.DayLogPage">
    <ContentPage.Content>
        <Grid x:Name="DayLogTrialGrid" RowSpacing="1" ColumnSpacing="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="100" />
                <RowDefinition Height="200" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>          
           <Editor x:Name="DayLogEditor" Text="{Binding EditorText, Mode=TwoWay}"
                   Grid.Row="1" Grid.Column="0" 
                    FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center" 
                    Keyboard="Chat" HeightRequest="100" WidthRequest="500"/>            
        </Grid>
    </ContentPage.Content>
</ContentPage>

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm going to assume the Editor starts out empty. string.empty. - at app launch

    So the classes that work off of that... are they robust enough to not fail on empty text? Or do they presume they have a valid value?

    From there... Do those methods return valid values if the Editor starts blank? or is it just a cascade of fails/nulls.

    Basically... without steping through or at least seeing the method that handling that text - there's know way to know what the problem is.

  • erdulterdult Member ✭✭
    edited November 2018

    Yes EditorText is empty at the class start. Then I manually fill it through the Simulator, and at that point I debugged the following part of the code, and I see that it has the values I typed on the Simulator, but when I trigger the Task through button click, it is empty

            private String _editorText;
            public String EditorText
            {
                get => _editorText;
                set
                {
                    _editorText = value;
                    //testStr = _editorText;
                    RaisePropertyChanged("EditorText");
                }
    

    For some reason binding does not work well. or maybe multiple instance of class generated. Let me create a solution copy removing all garbage and just leaving the necessary parts to demonstrate the issue.

  • erdulterdult Member ✭✭

    If you start the solution and press forward two times, and put a Debug inside IncreaseDate function, you will see that EditorText is set to empty string after the first forward statement, which is expected. But the screen still displays the "initial text" string which suggests that binding from PageModel to Page is not working, since it didn't update the display value.

  • erdulterdult Member ✭✭

    Any help on this, I am certain that the Binding between the Page and PageModel is not working right. I tried propagating strings from model to view and view to model they both didn't work, but in each case putting Debugger to RaisePropertyChanged I can see EditorText being modified. Is it possible that there are multiple PageModel instance being generated, thus ex; header and editor are linked to separate instances?

  • erdulterdult Member ✭✭

    Yes the other view which binds the DayLogPageModel through below code was creating another instance of DayLogPageModel. So for every view there is another DayLogPageModel then. Is there a way to turn this feature off, so single DayLogPageModel instance is created

           <Grid.BindingContext>
                <local:DayLogPageModel/>
                </Grid.BindingContext>
                <!--  <Label Text="Today"  />  -->
                <Label x:Name="DayLabel"
                Text="{Binding LogDateString}"
                       Grid.Row="0" Grid.Column="1" FontSize="10"
                       HorizontalOptions="Center" VerticalOptions="Center"
                       ></Label>
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Just remove those lines of XAML that create another instance. The DayHeader will inherit its BindingContext from the Page.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @erdult said:
    Is there a way to turn this feature off,

    Turn off this feature? Do you think its something that just happens? Nothing happens in code that someone doesn't write and make happen. If you're getting an additional instance of an object its because your code is creating it. Stop. Audit your code. Find where you are making the instance(s). Change the logic your wrote.

Sign In or Register to comment.