sticky
How to resolve the XAML generated code is throwing NotImplementedException when using x:Bind RRS feed

  • General discussion

  • Recently, some developers face the issue when they use 'x:Bind' in their UserControl. In the UserControl, they use 'x:Bind' to bind to the property which is in the UserControl’s code-behind like the following:

    <Page
        x:Class="AppSticky.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:AppSticky"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Grid>
            <TextBlock Text="{x:Bind ViewModel.Text, Mode=OneWay}"/>
        </Grid>
    </Page>
    public sealed partial class MyUserControl : Page
    {
        public MyUserControl()
        {
            this.InitializeComponent();
            this.DataContextChanged += (s, e) => this.Bindings.Update();
        }
        public SampleModel ViewModel => DataContext as SampleModel;
    }
    
    public class SampleModel
    {
        public string Text { get; set; }
    }

    They use this UserControl as other control’s DataTemplate, for example, the ListView’s ItemTemplate.

    <ListView x:Name="SomeListView">
         <ListView.ItemTemplate>
                <DataTemplate>
                    <local:MyUserControl />
                </DataTemplate>
         </ListView.ItemTemplate>
    </ListView>

    Then, once we set a value to the ListView’s ItemsSource like the following: 

    SomeListView.ItemsSource = Enumerable.Range(0, 10).Select(i => new SampleModel { Text = $"Model_{i}" });

    At the same time, the UserControl’s DataContext will be changed. In UserControl’s code-behind, the ViewModel property value will be updated, but on XAML of UserControl, it didn’t get updated. To make the x:Bind work in this case, we need to call the Bindings.Update() method in the 'DataContextChanged' event handler of UserControl. It will force it to update the source.

    Here, you might wonder where the 'Bindings.update ()' method comes from. If you check the document, it has explained:

    >> Pages and user controls that include Compiled bindings will have a "Bindings" property in the generated code. This includes an Update() method. This method will update the values of all compiled bindings. Any one-way/Two-Way bindings will have the listeners hooked up to detect changes.

    Then, if your project’s target version is earlier than 17763, your code does work without problem. But if your project’s target version is 17763, you will get exception:

    'System.NotImplementedException: The method or operation is not implemented.'

    We are aware of the problem and considering a fix for a future version of the Visual Studio product in the XAML Compiler generated code. To workaround the issue, you need to disable the x:Bind diagnostics feature by adding the following into your project (.csproj) file(under the first Propertygroup block).

    <PropertyGroup>  
      <EnableXBindDiagnostics>false</EnableXBindDiagnostics>
    </PropertyGroup>
    The detailed steps: 'Right click your project – Unload Project – Right click your project – Edit your project.csproj'.


    Friday, December 21, 2018 7:16 AM
    Moderator

All replies