Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

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

Open djrpascu opened this issue 5 years ago • 10 comments

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 avatar Dec 16 '19 20:12 djrpascu

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.

Gh0stC0de avatar Dec 20 '19 12:12 Gh0stC0de

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

djrpascu avatar Dec 30 '19 20:12 djrpascu

@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);

Gh0stC0de avatar Jan 01 '20 18:01 Gh0stC0de

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

djrpascu avatar Jan 03 '20 20:01 djrpascu

@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));
}

Gh0stC0de avatar Jan 03 '20 20:01 Gh0stC0de

@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!

djrpascu avatar Jan 03 '20 21:01 djrpascu

@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

PureWeen avatar Jan 10 '20 22:01 PureWeen

@PureWeen this wouldn't be a bug with DelegateCommand. If you have a DelegateCommand<T> 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>

dansiegel avatar Jan 12 '20 06:01 dansiegel

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

djrpascu avatar Jan 16 '20 00:01 djrpascu

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?

elretseo avatar Feb 15 '24 11:02 elretseo