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.
Sample project here https://github.com/djrpascu/XFGithub
Listview template displays contents properly and I'm able to pass the object into my command via CommandParameter.
Listview template goes blank.
Nuget Packages:
XF 4.4.0.991265
Prims.Unity.Forms 7.2.0.1422
NETStandard.Library 2.0.3
Affected Devices:
Windows 10 Desktop
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 thanks for verifying. know of any work around i might be able to do in the meantime?
@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 thanks for the suggestion, but it doesn't work. doesn't call the command.
@djrpascu I forgot that you have to specify the Sourceof 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 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 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 this wouldn't be a bug with DelegateCommand. If you have a DelegateCommand
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>
@PureWeen @samhouts would it be possible to get this re-opened?