Data binding - Does it work during run time?

PatrikNymanPatrikNyman SEMember
edited April 2016 in Xamarin.Forms

Didnt really know how to phrase the question, but I'm gonna start by showing you the code then describe the problem and what I want to achieve.

` public class BaseControl : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

    string text = "Hey";

    public string Text
    {
        get { return text; }
        set { text = value; OnPropertyChanged("TextProperty"); }
    }`

This class has a Text property I want to be reflected on a Label.

` protected void DownloadedFeed(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;

        try
        {
            var response = request.EndGetResponse(result);
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            Text = reader.ReadToEnd();

            Debug.WriteLine(text);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }`

This is a callback function inside BaseControl that gets called after I make a HttpWebRequest, I basically just want to print the response on the screen. I have put a breakpoint on the Text = reader.ReadToEnd() and it does return a response.

Now on the View side of things

` public class DashBoardPage : ContentPage
{

    public DashBoardPage()
    {
        BindingContext = new BaseControl();
        GenerateUI();
    }`

`  Label temp = new Label()
        {
            Text = "Empty",
            TextColor = Color.Black
        };

        temp.SetBinding(Label.TextProperty, new Binding("Text"));`

This is just a code snippet in the GenerateUI(), but this is where I setup my binding etc..

Now my question - I expect the Label to change from "Hey" to whatever DownloadedFeed() sets the text property to, but it doesnt work. How do I get the Labels text to reflect whatever the text string in my baseclass() is assigned?

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    It doesn't work. But what does happen?
    Does the text always say 'Empty'? Do you get any error messages?

  • PatrikNymanPatrikNyman SEMember

    No the text says "Hey". But after the label has been added to the page, I can no longer swap the text?

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    No it should be able to update and I don't see anything wrong actually..

  • JacobPoulsen_JacobPoulsen_ DKMember ✭✭✭

    Try attaching the Debugger to both the Get and Set methods of your Property, and see when and with what values the get/set are called with.

    You should be able to see if the Get method returns the new text value after its been downloaded.

  • PatrikNymanPatrikNyman SEMember

    The set gets called first and it returns "Hey".

    Then set gets called and the value is the json code that the HttpRequest got in response

    I can see the text string is changing value, the problem is that my label isn't.

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    What does your GenerateUI method look like?

  • PatrikNymanPatrikNyman SEMember

    #region serviceBoard /* TODO(Nyman): Create a carausel page in the middle of the grid */ //This is temporary Grid serviceBoard = new Grid() { VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.FromRgb(0.94, 0.94, 0.94), RowDefinitions = { new RowDefinition() { Height = GridLength.Auto } }, ColumnDefinitions = { new ColumnDefinition() { Width = GridLength.Auto } } }; Label temp = new Label() { Text = "Empty", TextColor = Color.Black }; temp.SetBinding(Label.TextProperty, new Binding("Text")); serviceBoard.Children.Add(temp, 0, 0); #endregion

    The label is actually just for testing, but I still wanna know why this doesnt work, because I had plans on trying to use this principle later on!

Sign In or Register to comment.