Windows 10 UAP - Compiled Data Bindings - c#

According to the Windows 10 SDK kick start videos (http://www.microsoftvirtualacademy.com/training-courses/a-developers-guide-to-windows-10-preview?prid=ch9courselink) there is a new type of bindings for the Windows 10 universal app platform called "compiled data bindings".
Instead of
"{Binding Path=..."
the new style is
"{x:Bind Path=..."
However this only throws in a compiler error that whatever is behind the = is placed does not exist in the context.
Setting
"{x:Bind Path=DataContext...."
does not work either.
Has anybody managed to get the new bindings to work? Is there ANY documentation on the topic because I don't seem to be able to find anything (not even a sample that you could "reverse engeneer"...
Update:
Thanks to Nick's response I can add the following:
Since I usually insert view models after the Page / UserControl is initialized, the Page / UserControl does not seem to notice the updated ViewModel property (even when the Page / UserControl implements and "fires" INotifyPropertyChanged).
Apparently there is a new field in Pages / UserControls called Bindings which can enforce a reset of all compiled data bindings.
So once you change your ViewModel (or another property referenced by x:Bind) you can simply call:
Binding.UpdateAll()
This way the Page / UserControl reevaluates all compiled data bindings and accepts a "data context switch".

Hope this helps - http://nicksnettravels.builttoroam.com/post/2015/04/26/Compiled-DataBinding-in-Windows-Universal-Applications-(UAP).aspx
This explains what the context is and how some of the compiled bindings are generated

I'll just add these two links to official documentation since it is available now and the content seems relevant to the subject.
1) {x:Bind} markup extension
2) {x:Bind} and {Binding} feature comparison

Related

Resources for working on an app in Xamarin using C# for UI rather than xml?

I am writing an app in xamarin.forms using C# for my UI. However, I am having trouble finding any resources to help me with certain things (like databinding for instance) that are strictly using c# as opposed to both c# and xml. If anyone can point me in the direction of where i could find those resources (if they exist) that would be awesome. (P.S. yes I have googled every single way I can possibly think of but all the keywords you'd use such as xamarin + c# + UI etc always just keep returning more results using xml)
the Xamarin binding docs have samples in C# and XAML
// set the binding context for the page
this.BindingContext = new MyViewModel();
// create a label
Label myLabel = new Label();
// set the binding to a property on the VM
myLabel.SetBinding(Label.TextProperty, "Description");

Navigation between pages in Xamarin Forms

I have the following command inside a VM:
ICommand LaunchGameCommand => new Command(() =>
{
//Navigation.PushAsync(...
});
According to the answers here I should be able to use something akin to the navigation in the commented code; however, the Navigation object seems to reside in Android.Content.Res... which seems to be something else entirely.
Is this still the correct method of navigating between views / viewmodels in Xamarin Forms, or has this now been superseded with an alternate method?
Navigation is part of a page, you can’t find navigation property if you don’t have the reference to a some page, you need to have access to your current page in your view model to see this property, you can have access to your current page using
Application.Current.MainPage.Navigation.Push...
Are you using any particular MVVM framework? Most of these include a way of navigating from VM to VM.
I use FreshMvvm. It allows you to perform the following to navigate between VMs and also pass data:
CoreMethods.PushPageModel<MyNextPageModel>(DataToPass);
More details here

Set ApplicationBarIconButton icon using Bitmap [duplicate]

I have a small C# project that has an ApplicationBar. But I have a small problem: I want 8 icons on the bar, and the ApplicationBar only supports 4. I came up with a solution (in C#): add a small CheckBox to ask if the user wants to use the first or second set of tools.But I'm still not able to change the icons on the ApplicationBar. I tried removing the old ones, first with ApplicationBar.MenuItems.Remove(Button1); and then with ApplicationBar.Buttons.Remove(Button1);
but neither worked. I tried changing the .IconUri property of the button, but that gave me a NullReferenceException.
I don't understand what you mean by changing it from "C#, not Silverlight". C# is a programming language and Silverlight is a framework. Nevertheless, the link you posted to explains exactly how you do it. The ApplicationBar is not a Silverlight control, it's part of the native OS. You can use the code in the link or do something like this:
firstAppBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
firstAppBarButton.Text = "New Text";
firstAppBarButton.IconUri = new Uri("/appbarIcon.png",UriKind.Relative);
You need to get the ApplicationBarIconButton via the index (0 for first one, 1 for second etc..) instead of by name.
You can't refer to the application buttons by name. Try:
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Remove
I would also suggest that you do not present two groups of 4 icons to the user. The limit is 4 for a reason. Any more than that requires a UI re-think. Perhaps divide the functionality over a few pages?
The syntax above gave me a compile error. With some additional research, I got this to work for me:
ApplicationBar.Buttons.Remove((ApplicationBarIconButton) ApplicationBar.Buttons[0]);

How to change the image on the ApplicationBar from C#?

I have a small C# project that has an ApplicationBar. But I have a small problem: I want 8 icons on the bar, and the ApplicationBar only supports 4. I came up with a solution (in C#): add a small CheckBox to ask if the user wants to use the first or second set of tools.But I'm still not able to change the icons on the ApplicationBar. I tried removing the old ones, first with ApplicationBar.MenuItems.Remove(Button1); and then with ApplicationBar.Buttons.Remove(Button1);
but neither worked. I tried changing the .IconUri property of the button, but that gave me a NullReferenceException.
I don't understand what you mean by changing it from "C#, not Silverlight". C# is a programming language and Silverlight is a framework. Nevertheless, the link you posted to explains exactly how you do it. The ApplicationBar is not a Silverlight control, it's part of the native OS. You can use the code in the link or do something like this:
firstAppBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
firstAppBarButton.Text = "New Text";
firstAppBarButton.IconUri = new Uri("/appbarIcon.png",UriKind.Relative);
You need to get the ApplicationBarIconButton via the index (0 for first one, 1 for second etc..) instead of by name.
You can't refer to the application buttons by name. Try:
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Remove
I would also suggest that you do not present two groups of 4 icons to the user. The limit is 4 for a reason. Any more than that requires a UI re-think. Perhaps divide the functionality over a few pages?
The syntax above gave me a compile error. With some additional research, I got this to work for me:
ApplicationBar.Buttons.Remove((ApplicationBarIconButton) ApplicationBar.Buttons[0]);

XMLDataprovider label binding codebehind WPF c#

I am working on a WPF application. I set up a multilanguage with xml files and I use static resource binding in front code to set the corrisponding text. The issue I have is with doing the same thing in the codebehind.
Here you can see how I use it in front code:
<XmlDataProvider x:Key="Lang" Source="/lng/english.xml" XPath="WpfApplication"/>
<Label HorizontalAlignment="Center" Margin="0,10,0,5" Foreground="White" FontWeight="Bold" Content="{Binding Source={StaticResource Lang}, XPath=MenuTextClimate/#Header}"></Label>
I am trying to do the same in codebehind like this:
String selLangFullPath = WpfLibrary.LanguageOptions.getSelLangFullPath();
XmlDataProvider xmlData = (XmlDataProvider)(this.FindResource("Lang"));
xmlData.Source = new Uri(selLangFullPath, UriKind.Relative);
xmlData.XPath = "MenuTextClimate/#Header";
Binding NewBinding = new Binding();
NewBinding.Source = xmlData;
NewBinding.Mode = BindingMode.OneWay;
NewBinding.XPath = "MenuTextClimate";
lblTitle.SetBinding(Label.ContentProperty, NewBinding);
but for some reason it doesent seem to work. Can any one tell me where I went wrong?
The codebehind you've shown doesn't actually do the same thing. It's different in three ways:
You're changing the Source property of the XmlDataProvider
You're providing a different XPath to the XmlDataProvider (MenuTextClimate/#Header instead of WpfApplication).
You're also providing a different XPath in the binding expression.
The problem could simply be that any or all of those things is wrong. (The XPath ones look particularly suspicious, because they look like they presume a completely different XML document structure. Although since you're also providing a different XML document, maybe that's fine. It's impossible to tell from the information provided so far.) So the first thing I'd do is try making your C# do exactly the same as your Xaml - same URI and same XPaths. If that works, it should be easier to see which of the three things that's different is causing the problem.
Alternatively, enable WPF debug output. If you're on .NET 3.5 sp1 or earlier, this is usually on by default for Error level logging of data binding messages. (Data binding errors appear in the Output window.) As of .NET 4.0, Microsoft turned it down so you won't see it unless you ask for it. You turn it on with the 'options' dialog in Visual Studio - it's under Debugging -> Output Window. Ensure that Data Binding is set to show errors. Or for more detail, crank it all the way up and then enable full logging by adding this:
PresentationTraceSources.SetTraceLevel(NewBinding, PresentationTraceLevel.High);
That should show you full gory details of what data binding is attempting to do with your binding, and that's often a pretty good way to find out why things aren't working.

Categories

Resources