How to x:Reference "Some Page" from App.xaml ?

CharwakaCharwaka INMember ✭✭✭✭✭
edited February 15 in Xamarin.Forms

Let say i am setting my page name as x:Name="Dashboard",

but my Data template is in App.xaml resources so how do i x:Reference this now ? some thing like this

The below code is in Datatemplate inside App.xaml

<TapGestureRecognizer Command="{Binding Source={x:Reference Dashboard}, Path=BindingContext.OpenImage" />

But My ListView is in Dashboard Page .

Thanks.

Best Answer

  • CharwakaCharwaka IN ✭✭✭✭✭
    edited February 18 Accepted Answer

    Hi @NMackay

    I Solved this problem like this

    public class Person :ViewModelBase
    
    
    public RelayCommand<bool> LikeCommand => new RelayCommand<bool>((e) =>
                                                                       {
                                                                           IsLiked = !e;
                                                                       });
    

    and in App.XAML

    <Image >
                                                <Image.GestureRecognizers>
                                                    <TapGestureRecognizer
                                                            Command="{Binding LikeCommand}" CommandParameter="{Binding IsLiked}"
                                                            NumberOfTapsRequired="1" />
                                              </Image.GestureRecognizers>
    
    </Image >
    

    No need of x:Reference blah blah

Answers

  • deczalothdeczaloth DEMember ✭✭✭
    edited February 15

    I will assume that in the Page Dashboard, you set BindingContext as your view model, where a Command called OpenImage lives.

    If that is true, if i would like to call OpenImage, from another Page, i would create a public static viewModel Property (which should be set to the viewModel you want to use as BindingContext) in Dashboard, and reference it from the other Page. In that case the reference would be done as:

    First, in XAML add the namespace where the page you want to reference lives:

    xmlns:local ="clr-namespace:App1"
    

    Then reference the static view model like:

    <TapGestureRecognizer Command="{Binding Source={x:Static local:MainPage.viewModel}, Path=OpenImage}"/>
    
  • NMackayNMackay GBInsider, University mod

    @Charwaka

    Seen a few ways of achieving this but generally involves a parent binding as you cannot access the Parent from the template bindingcontext.

    https://forums.xamarin.com/discussion/71462/datatemplate-with-templateselector-bind-to-parent-command

    Personally I think your making life difficult by insisting all your templates are in App.xaml, I explained on the other thread why this is a limitation in Xamarin Forms. If the templates have no interaction and the listitem is passed to the vm by EventToCommand ItemTapped etc then sure, put them in App.xaml.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @NMackay said:

    Personally I think your making life difficult by insisting all your templates are in App.xaml,

    I am just trying to find if some has already solution,i cannot assume you advise as final.always there will be a better solution. :)

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @deczaloth said:
    I will assume that in the Page Dashboard, you set BindingContext as your view model, where a Command called OpenImage lives.

    If that is true, if i would like to call OpenImage, from another Page, i would create a public static viewModel Property (which should be set to the viewModel you want to use as BindingContext) in Dashboard, and reference it from the other Page. In that case the reference would be done as:

    First, in XAML add the namespace where the page you want to reference lives:

    xmlns:local ="clr-namespace:App1"
    

    Then reference the static view model like:

    <TapGestureRecognizer Command="{Binding Source={x:Static local:MainPage.viewModel}, Path=OpenImage}"/>
    

    The command is inside Observable collection Model, so how can we bind this now ASPY

  • NMackayNMackay GBInsider, University mod

    @Charwaka said:

    @NMackay said:

    Personally I think your making life difficult by insisting all your templates are in App.xaml,

    I am just trying to find if some has already solution,i cannot assume you advise as final.always there will be a better solution. :)

    You shouldn't I'm just suggesting what you want isn't supported but if you do get a solution then post it hear so we can all learn.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @NMackay said:

    @Charwaka said:

    @NMackay said:

    Personally I think your making life difficult by insisting all your templates are in App.xaml,

    I am just trying to find if some has already solution,i cannot assume you advise as final.always there will be a better solution. :)

    You shouldn't I'm just suggesting what you want isn't supported but if you do get a solution then post it hear so we can all learn.

    Actually i moved all my templates to with in Page, but still not working , but i can see get set properties are hitting while populating list

  • CharwakaCharwaka INMember ✭✭✭✭✭
    edited February 18 Accepted Answer

    Hi @NMackay

    I Solved this problem like this

    public class Person :ViewModelBase
    
    
    public RelayCommand<bool> LikeCommand => new RelayCommand<bool>((e) =>
                                                                       {
                                                                           IsLiked = !e;
                                                                       });
    

    and in App.XAML

    <Image >
                                                <Image.GestureRecognizers>
                                                    <TapGestureRecognizer
                                                            Command="{Binding LikeCommand}" CommandParameter="{Binding IsLiked}"
                                                            NumberOfTapsRequired="1" />
                                              </Image.GestureRecognizers>
    
    </Image >
    

    No need of x:Reference blah blah

  • CharwakaCharwaka INMember ✭✭✭✭✭

    Its Resides in Person Model >.NET STANDARD 2.0 Library

    and My VieModel in Another .NET STANDARD 2.0 Library

  • NMackayNMackay GBInsider, University mod

    @Charwaka said:

    Its Resides in Person Model >.NET STANDARD 2.0 Library

    and My VieModel in Another .NET STANDARD 2.0 Library

    So the bound object has the command?

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @NMackay said:

    @Charwaka said:

    Its Resides in Person Model >.NET STANDARD 2.0 Library

    and My VieModel in Another .NET STANDARD 2.0 Library

    So the bound object has the command?

    Yes

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @NMackay said:

    @Charwaka said:

    Its Resides in Person Model >.NET STANDARD 2.0 Library

    and My VieModel in Another .NET STANDARD 2.0 Library

    So the bound object has the command?

    you can try same with some sample MvvMlight .Net Standard Project with XF 3.4

  • CharwakaCharwaka INMember ✭✭✭✭✭

    I'm sorry to say this but your statement was futile.why would i simply ask a question ? so what made me to ask to this question ?

    because at deep down "command from Tap gesture was not firing " , so after i saw your answer stating x:reference would make it work .where it didnt worked both in app.xaml and in content page itself. and your much aware of this.

  • NMackayNMackay GBInsider, University mod
    edited February 18

    Ok, I give up. Good luck with your app.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @NMackay said:
    Ok, I give up. Good luck with your app.

    Me too :)

Sign In or Register to comment.