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

[Bug]: DefaultContent in ViewModelControlHost (Winforms) is Disposed #3260

Open
ismdiego opened this issue May 17, 2022 · 0 comments
Open

[Bug]: DefaultContent in ViewModelControlHost (Winforms) is Disposed #3260

ismdiego opened this issue May 17, 2022 · 0 comments
Labels

Comments

@ismdiego
Copy link

ismdiego commented May 17, 2022

Describe the bug 🐞

The DefaultContent set in ViewModelControlHost (warning! the file is named ViewModelViewHost.cs) is Disposed when clearing the controls in the container. So, next time it should be used is not ok.

Step to reproduce

Configure a simple ReactiveUI project using Winforms:

  • Form1 (two buttons + ViewModelControlHost)
  • TestVM
  • TestV (UserControl)
  • Register the TestV for IViewFor<TestVM> (Locator.CurrentMutable.Register(() => new TestV(), typeof(TestVM));)
  • Form1 ctor: setup the defaultcontent of the ViewModelControlHost to be a simple label with text in it (i.e.: "Nothing here")
  • Form1.button1.Click -> set ViewModelControlHost.ViewModel = new TestVM();
  • Form1.button2.Click -> set ViewModelControlHost.ViewModel = null

Run app:

  • label with text "Nothing here" (of DefaultContent) is shown
  • Click button1, TestV is shown
  • Cliek button2, NOTHING is shown (BUG)

Reproduction repository

https://github.com/ismdiego/reactiveui_bug01

Expected behavior

The label with text "Nothing here" should be seen again when setting null to ViewModelControlHost.ViewModel

Screenshots 🖼️

No response

IDE

No response

Operating system

No response

Version

No response

Device

No response

ReactiveUI Version

Latest as of now (18.0.10), but I think this problem has been there for a long, long time.

Additional information ℹ️

I think the problem is only related to disposing all controls inside container when Content is changed.
This also disposes the DefaultContent control, which it really should not.

With this change in code (file ViewModelViewHost.cs, line 161) from:

// clear out existing visible control view
foreach (Control? c in Controls)
{
    c?.Dispose();
    Controls.Remove(c);
}

to:

// clear out existing visible control view
foreach (Control? c in Controls)
{
    if (c != DefaultContent)
    {
        c?.Dispose();
    }

    Controls.Remove(c);
}

The problem is gone.

RoutedViewHost follows a different strategy, and so it seems to only dispose the last view used, but not the DefaultContent one. So I expect it to be working fine.

@ismdiego ismdiego added the bug label May 17, 2022
@ismdiego ismdiego changed the title [Bug]: DefaultContent in ViewModelViewHost (Winforms) is Disposed [Bug]: DefaultContent in ViewModelControlHost (Winforms) is Disposed May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant