Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically changing "ContentPage --> content" property is not updated correctly #45

Closed
albertwoo opened this issue May 15, 2018 · 6 comments

Comments

@albertwoo
Copy link

albertwoo commented May 15, 2018

My demo code is like below

type Msg = Next
type Model = { Count: int }
let init () = { Count = 0 }, Cmd.none
let update msg model =
    match msg with | Next -> { Count = if model.Count > 0 then 0 else 1 }, Cmd.none
let view model dispatch =
    Xaml.ContentPage(
        title = "Demo",
        content =
            if model.Count = 0 then
                Xaml.Button(text = "Next1", command = fun _ -> dispatch Next)
            else
                Xaml.StackLayout(
                    children = [
                        Xaml.Label(text = "Finished")
                        Xaml.Button(text = "Go back", command = fun _ -> dispatch Next) ]
                )
    )

When I click next the Count is change to 1 and program is also running in to else block, but the UI is not updated.

@gibranrosa
Copy link

gibranrosa commented May 15, 2018

I think there is some limitation for the ContentPage to handle updates, but with a slight modification to your view it works:

        Xaml.ContentPage(
            title = "Demo",
            content =
                Xaml.StackLayout(
                        children = [
                            if model.Count = 0 then
                                yield Xaml.Button(text = "Next1", command = (fun _ -> dispatch Next), textColor = Color.Black)
                            else          
                                yield Xaml.Label(text = "Finished")
                                yield Xaml.Button(text = "Go back", command = fun _ -> dispatch Next) ]
                            )
                )

@dsyme
Copy link
Collaborator

dsyme commented May 16, 2018

@gibranrosa Do you know if/where that limitation is discussed? I presume that's a limitation of the Xamarin Forms implementation (see here https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/ContentPage.cs)

Thanks

@albertwoo
Copy link
Author

Thanks for the explanation. But if there is some way to solve this limitation then it will save a lot of time for beginners like me.

I will close this then.

@dsyme dsyme changed the title UI is not updated correctly Dynamically changing "ContentPage --> content" property is not updated correctly May 16, 2018
@dsyme
Copy link
Collaborator

dsyme commented May 16, 2018

@albertwoo I'll keep it open with a different title as it is a definite glitch, though it's likely a limitation in Xamarin.Forms

@dsyme dsyme reopened this May 16, 2018
@gibranrosa
Copy link

gibranrosa commented May 16, 2018

@dsyme , I have found the problem, apparently it is with this library instead. An exception occurs in line 10657, as the TargetTypes are different:
https://github.com/fsprojects/Elmish.XamarinForms/blob/4899cce4b036e7deca795905c3e1c4bad3a29d3f/Elmish.XamarinForms/DynamicXaml.fs#L10653-L10657

I have changed as below and it worked, do you approve?

match prevChildOpt, source.TryContent with
            // For structured objects, dependsOn on reference equality
            | USome prevChild, USome newChild when System.Object.ReferenceEquals(prevChild, newChild) -> ()
            | USome prevChild, USome newChild when prevChild.TargetType = newChild.TargetType ->
                newChild.UpdateIncremental(prevChild, target.Content)
            | USome _, USome newChild                
            | UNone, USome newChild ->
                target.Content <- newChild.CreateAsView()

@albertwoo
Copy link
Author

@gibranrosa great job! I was planning to investigate this today, now my life is saved 👍

@dsyme dsyme closed this as completed in 50803d4 May 17, 2018
dsyme added a commit that referenced this issue May 17, 2018
Fix #45 : Modified Generator to Reference Properties updates  NOT use UpdateIncremental when types of Old and New object differs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants