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

Resetting Window.DataContext with two-way bound MVVM model raises NotImplementedException #1422

Closed
PatrickHofman opened this issue Jun 18, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@PatrickHofman
Copy link

Description
A NotImplementedException is thrown when binding two-way to a MVVM model, and resetting that model to null. This bug seems related to #1330, but has some specifics that could mean there is a secondary bug, or at least a good test case.

Call stack on this.DataContext = default;:

at Microsoft.Web.WebView2.Wpf.WebView2.SourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

Version
SDK: 1.0.864.35
Runtime: Unknown
Framework: WPF
OS: Win10 21H1 19043.1055

Repro Steps

  • Create a WPF app (net472)
  • Add this code in the MainWindow.xaml:
<wv2:WebView2 x:Name="Browser" Source="{Binding Url, Mode=TwoWay}"></wv2:WebView2>
  • Replace the code in the MainWindow.xaml.cs with this:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        ViewModel m = new ViewModel()
        { Url = new Uri("https://google.com")
        };

        this.InitializeComponent();

        this.DataContext = m;
    }

    /// <summary>
    /// Raises the System.Windows.Window.Closed event.
    /// </summary>
    /// <param name="e">An System.EventArgs that contains the event data.</param>
    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);

        //
        // Reset data context and model to default.
        //
        this.DataContext = default;
    }
}

public class ViewModel : DependencyObject
{
    /// <summary>
    /// Gets the Url dependency property.
    /// </summary>
    public static readonly DependencyProperty UrlProperty = DependencyProperty.Register(nameof(Url), typeof(Uri), typeof(ViewModel));

    /// <summary>
    /// Gets or sets the URL.
    /// </summary>
    public Uri Url
    {
        get { return (Uri)this.GetValue(UrlProperty); }
        set { this.SetValue(UrlProperty, value); }
    }
}

Open the app, close the app after the Google home page has loaded. On closing, an exception is raised.

Expected no exceptions to be raised and the unbound value to be allowed, or rewritten to something like about:blank.

@PatrickHofman PatrickHofman added the bug Something isn't working label Jun 18, 2021
@PatrickHofman PatrickHofman changed the title Resetting Window.DataContext with MVVM model raised NotImplementedException Resetting Window.DataContext with two-way bound MVVM model raises NotImplementedException Jun 18, 2021
@champnic
Copy link
Member

Agreed this looks the same as #1330, but thanks for opening and including the extra test case! I've included a link to this info in our tracking bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants