Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] [UWP] Template Disappears when Command Parameter Binding Set #8918

Open
djrpascu opened this issue Dec 16, 2019 · 10 comments
Open

[Bug] [UWP] Template Disappears when Command Parameter Binding Set #8918

djrpascu opened this issue Dec 16, 2019 · 10 comments
Labels
4.4.0 regression on 4.4.0 a/listview Problems with the ListView/TableView e/5 🕔 5 help wanted We welcome community contributions to any issue, but these might be a good place to start! i/regression inactive Issue is older than 6 months and needs to be retested p/UWP t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!
Milestone

Comments

@djrpascu
Copy link

djrpascu commented Dec 16, 2019

Description

I have a Listview with simple DataTemplate selector which presents one of either two templates. Once the CommandParameter Binding is defined to the current object ({Binding} or {Binding .}), the entire view for that template goes blank on UWP. Therefore, I'm not allowed to call the command with the current object. This works as expected when deploying out to iOS.

Steps to Reproduce

Sample project here https://github.com/djrpascu/XFGithub

Expected Behavior

Listview template displays contents properly and I'm able to pass the object into my command via CommandParameter.

Actual Behavior

Listview template goes blank.

Basic Information

  • Version with issue: 4.4.0.991265

  • Last known good version: N/A

  • IDE: VS Enterprise 2019 16.4.1

  • Platform Target Frameworks:

    • iOS: 13
    • Android: N/A
    • UWP: 16299
  • Android Support Library Version: N/A

  • Nuget Packages:
    XF 4.4.0.991265
    Prims.Unity.Forms 7.2.0.1422
    NETStandard.Library 2.0.3

  • Affected Devices:
    Windows 10 Desktop

Screenshots

Reproduction Link

https://github.com/djrpascu/XFGithub

@djrpascu djrpascu added s/unverified New report that has yet to be verified t/bug 🐛 labels Dec 16, 2019
@samhouts samhouts added a/listview Problems with the ListView/TableView p/UWP labels Dec 17, 2019
@Gh0stC0de
Copy link

This also happens with ViewCells and ViewCell.ContextActions:

<ListView ItemsSource="{Binding Samples}">
        <ListView.ItemTemplate>
            <DataTemplate>
                 <ViewCell>
                    <ViewCell.ContextActions>
                       <MenuItem Text="Do something"
                                     Command="{Binding Source={x:Reference Page}, Path=BindingContext.FileEditPopupCommand}"
                                     CommandParameter="{Binding .}" />
                    </ViewCell.ContextActions>
                    <StackLayout>
                        <Label Text="{Binding Description}" />
                        <Button Text="Button" Command="{Binding Source={x:Reference Page}, Path=BindingContext.FileEditPopupCommand}"
                                CommandParameter="{Binding .}"/>
                    </StackLayout>
                 </ViewCell>
            </DataTemplate>
       </ListView.ItemTemplate>
   </ListView>

Tested this snippet in the sample provided.

@djrpascu
Copy link
Author

@Gh0stC0de thanks for verifying. know of any work around i might be able to do in the meantime?

@Gh0stC0de
Copy link

@djrpascu I think you could use a command with an object as command parameter and cast it.

Something like:

public DelegateCommand<object> MyCommand = new DelegateCommand(MyNewAction);

MyNewAction(object parameter) => MyOldAction((CastHere) parameter);

@djrpascu
Copy link
Author

djrpascu commented Jan 3, 2020

@Gh0stC0de thanks for the suggestion, but it doesn't work. doesn't call the command.

@Gh0stC0de
Copy link

@djrpascu I forgot that you have to specify the Source of the BindingContext like this:
CommandParameter="{Binding Source={x:Reference Layout}, Path=BindingContext}"

I did the following changes to get it working:
MainPage.xaml

<StackLayout x:Name="Layout">
[...]
    <Button Text="Click"
            Command="{Binding Path=BindingContext.FileEditPopupObjectCommand, Source={x:Reference FilesListView}}"
            CommandParameter="{Binding Source={x:Reference Layout}, Path=BindingContext}" />
[...]

MainPageViewModel:

public DelegateCommand<object> FileEditPopupObjectCommand { get; private set; }

public MainPageViewModel(INavigationService navigationService)
    : base(navigationService)
{
    Title = "Main Page";

    FileEditPopupCommand = new DelegateCommand<Sample>(FileEditPopup);
    FileEditPopupObjectCommand = new DelegateCommand<object>(o => FileEditPopup((Sample) o));
}

@djrpascu
Copy link
Author

djrpascu commented Jan 3, 2020

@Gh0stC0de thanks sooo much! tested and works on iOS as well!

never thought to set bindingcontext to the parent control and retrieve object that way! i was trying to get value through a converter but couldn't figure it out..

thanks again!

@PureWeen PureWeen self-assigned this Jan 10, 2020
@PureWeen PureWeen added the e/5 🕔 5 label Jan 10, 2020
@PureWeen
Copy link
Contributor

@djrpascu This seems like it's a bug with DelegateCommand

If I just change your DelegateCommands to Xamarin.Forms.Command then everything works perfectly with your sample.

It all renderers, I click the command, and the parameter is passed and executed

Using your sample if you enable CLR exceptions you'll get the following exception

System.InvalidCastException: 'Unable to cast object of type 'XFGithub.ViewModels.MainPageViewModel' to type 'XFGithub.Sample'.'

It seems like for some reason the DelegateCommand is trying to pass in 'XFGithub.ViewModels.MainPageViewModel' as a parameter to the DelegateCommand

@dansiegel
Copy link
Contributor

dansiegel commented Jan 12, 2020

@PureWeen this wouldn't be a bug with DelegateCommand. If you have a DelegateCommand and the CommandParameter passed in is not null and not a type of T by design it should throw an InvalidCastException.

This seems to me that it would be a bug in Xamarin.Forms particularly if this is working on iOS and not UWP. The must be something in UWP that is causing the wrong value to be passed in for the Command Parameter. I should add as well that from what I see adding the binding source for the binding of the Command seems like it must be changing the BindingContext of the cell... even if I tried naming the cell and changing the binding of the CommandParameter it produced the same error.

<ViewCell x:Name="cell">
  <Button Command="{Binding BindingContext.MyCommand, Source={x:Reference page}}"
          CommandParameter="{Binding BindingContext, Source={x:Reference cell}}" />
</ViewCell>

@djrpascu
Copy link
Author

@PureWeen @samhouts would it be possible to get this re-opened?

@samhouts samhouts removed the s/unverified New report that has yet to be verified label Feb 18, 2020
@samhouts samhouts added the 4.4.0 regression on 4.4.0 label Jun 17, 2020
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! and removed inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! labels Jul 17, 2020
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! and removed inactive Issue is older than 6 months and needs to be retested labels Jul 31, 2020
@samhouts samhouts added the inactive Issue is older than 6 months and needs to be retested label Sep 18, 2020
@elretseo
Copy link

I am having the same issue on Xamarin.Forms 5.0.0.2578 and UWP.

Using Xamarin.Forms.Command instead of DelegateCommand is not an option since it does not have the method ObservesProperty which I use heavily in my viewmodels.

Has anyone an idea, how to solve the issue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4.4.0 regression on 4.4.0 a/listview Problems with the ListView/TableView e/5 🕔 5 help wanted We welcome community contributions to any issue, but these might be a good place to start! i/regression inactive Issue is older than 6 months and needs to be retested p/UWP t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!
Projects
None yet
Development

No branches or pull requests

6 participants