Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

TemplatePage - Binding x:Reference

$
0
0

Hello, I wanted to share 2 things that makes me waste a bit of time :

1) TemplatePage - Navigating to new page with Prism
If you ever create a custom template for a page, you might avoid using a TemplatePage as the base class.
Context : I've created a custom page : ContentWithOverlayPage which allows to factorize a popup overlaying the page (without using a modal page which hides the calling page).

First, I was using a TemplatePage and it worked very well. The page that should display a popin (popup inside the page) was inheriting from my templatePage : ContentWithOverlayPage.
But when I needed to navigate to another page, I found that the navigation was brocken.
Switching from a templatePage to a contentPage with a ControlTemplate fix the navigation.

2) Naming control - Binding to relative source (x:Reference)
Some code will explain the problem better.
Let's say you have a ContentView self hosted in a file (MyContentView.xaml & MyContentView.xaml.cs)
The root has a x:Name set to Self / This / That / Me (doesn't matter : read here the kind of name you choose).

You use it inside a ContentPage which has the same name set on the root element. It will "work", I mean, it will show something on the screen and the compiler won't kick you.
My CustomContentView was used in a DataTemplate used in the page.

If you ever try to bind a property of the CustomContentView to a property of the page, you may use something like :
<CustomContentView MyProperty="{Binding SomePropertyOfThePage, Source={x:Reference Self/Me/This/That}"/>
The binding won't work because the reference will target the CustomContentView root element of the same name and not the page. It might not be obvious as you try to reference to a name set only once in your xaml (the page).

The solution here is to have different Name.
I can give another tip : if you don't use a x:Name / don't reference to it anymore, remove it. It may save you times in future because you forget you have a child control in your page that use the same x:Name.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>