Can't x:Bind to global ObservableCollection<Type>

296 Views Asked by At

I used Jerry's technique:

App-wide Observable Collection

and got a compilation error CS0176 with the message:

Member 'Page.Items' cannot be accessed with an instance reference; qualify it with a type name instead.

I am trying to x:Bind to an ObservableCollection Items.

Anyone knows how to fix this error.

thanks

1

There are 1 best solutions below

1
Jerry Nixon On

So, I just double-checked, this time with x:bind.

This is the code:

public class x
{
    public static ObservableCollection<string> Items { get; } = new ObservableCollection<string>();
}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        InitializeComponent();
    }

    public ObservableCollection<string> Items { get { return x.Items; } }
}

And this is the XAML:

<!--  page content  -->
<StackPanel Grid.Row="1" VerticalAlignment="Top" Orientation="Horizontal"
            Padding="12,8,0,0">

    <ListView ItemsSource="{x:Bind Items}" />

</StackPanel>

Works like a charm. Looks like this:

enter image description here

So, I am not sure why you are seeing this error. But, to your question, there is no fix because this works. If you figure out the problem you were getting be sure and follow-up with a comment. Otherwise, best of luck!