Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x:Bind in UWP (Universal Windows Platform)

Tags:

c#

mvvm

xaml

uwp

Right. So moving from WPF to UWP, I'm trying to use x:Bind to get compile-time benefits. Simple scenarios work fine; however I have found a number of issues that I was not able to solve. They are all related, so I thought I'd post them in one place:

  1. I haven't been able to make Intellisense work with x:Bind. I have set DataContext (as well as d:DataContext just as we do in WPF) both in XAML and in the constructor, but it won't show members no matter what. Has anyone done this successfully?
  2. Then I read somewhere that in UWP, DataContext is always set to Page's code-behind (really??) and that I need to define a ViewModel type property in the code-behind and then use that property in x:Bind. Is this correct? I tried it and it works but gives rise to the next question.
  3. If I define a property of ViewModel type in Page's code-behind, Any sub-properties that raise PropertyChanged notifications do not update the UI. For example, if the code-behind property is named Game (of type GameVM) and there is a public property in GameVM named Player (of type GamePlayer), and in turn GamePlayer contains a property named Name, the x:Bind path will look like {x:Bind Path=Game.Player.Name}. But if I do this, any change notifications raised from within Name property do not update Page's UI.

One alternate I tried was to listen to PropertyChanged at each level and then bubble it up the hierarchy, but that hasn't worked. Even if it does, doing this seems a bit too much work. In WPF sub-properties like Game.Player.Name work properly without having to doing property change bubbling. Or am I missing something?

like image 983
dotNET Avatar asked Sep 23 '16 09:09

dotNET


People also ask

What is x Bind?

x-bind allows you to set HTML attributes on elements based on the result of JavaScript expressions.

Can you use X bind in WPF?

x:Bind for WPF in . NET Framework and . NET Core is still not supported.

What is data binding in UWP?

Data binding is the process that establishes a connection between the UI layer with our Data Layer. So when you change your Data your GUI is updated and vice versa.

Does UWP use MVVM?

Many of the other UWP app samples also use a basic MVVM architecture, and the Traffic App sample includes both code-behind and MVVM versions, with notes describing the MVVM conversion.


2 Answers

Right. After playing with it for a few days and searching numerous references, here are my findings:

  1. {x:Bind} lacks design-time support. The feature is on the wishlist though. You may want to upvote it there. (The new version of Visual Studio 15.4.4 does support Intellisense in {x:Bind}in the required way.)
  2. {x:Bind} uses code-behind as its DataContext. So you need to define a public property of your ViewModel type in the code-behind and then use it in your {x:Bind} path.
  3. As pointed out by IInspectable, the default mode for {x:Bind} is OneTime, unlike {Binding} which uses OneWay or TwoWay in almost all cases. So you need to explicitly specify Mode in your binding. People coming from WPF should take special care of it.
  4. Sub-properties that implement notification change work perfectly fine in {x:Bind}. There is no need of bubbling these notifications upwards in the property hierarchy. The problem I was facing (#3 in the question) was because my sub-property was of type List<T>. I changed it to ObservableCollection<T> and it started working.

Hope this works somebody down the road.

like image 188
dotNET Avatar answered Oct 17 '22 00:10

dotNET


Well as a beginner, the only question I can answer for you is the first one. Intellisense does not work inside the {x:Bind}. The members are never shown there in UWP for some unknown reasons. As for the next two questions of yours, I am still working on them.

like image 30
Monish Koyott Avatar answered Oct 17 '22 00:10

Monish Koyott