0

I come from a WPF / Prism background but I really like what X:Bind offers. How do I get x:Bind to work with my ViewModel when using Prism.Uno?

I have prism:ViewModelLocator.AutoWireViewModel="True" but I seem to be missing something in my understanding of how it works when designing.

Thanks G

Jérôme Laban
  • 5,224
  • 3
  • 20
  • 17
George Bamber
  • 25
  • 2
  • 2
  • Would you have an example of what's not working when using x:Bind? Also note that your View may not be a UserControl at this time see https://stackoverflow.com/questions/61570953/android-wasm-prismmvvmviewmodellocator-autowireviewmodel-true-is-not-wire-u – Jérôme Laban May 05 '20 at 12:05
  • Hi Jerome - I did see that issue but I'm staying just with UWP for now until I can get my Refit issue sorted. Sorry I should have been clearer in my initial description. This is at design time rather than when debugging. It's complaining about the variable not being in my code behind .xaml.cs file. – George Bamber May 05 '20 at 12:37

1 Answers1

0

The use of x:Bind requires the binding path to be rooted in the View.

To use the DataContext, you'll need make it available typed through the view, like this:

public partial class MyControl : INotifyPropertyChanged
{
#if !HAS_UNO 
        // Uno already defines this event (it will be removed 
        // in the future to be aligned properly with WinUI)
        public event PropertyChangedEventHandler PropertyChanged;
#endif

        public MainPage()
        {
            this.InitializeComponent();

            DataContextChanged += 
                (s, e) => PropertyChanged?.Invoke(
                   this, 
                   new PropertyChangedEventArgs(nameof(ViewModel)));
        }

        public MyViewModel ViewModel => DataContext as MyViewModel;

}
Jérôme Laban
  • 5,224
  • 3
  • 20
  • 17