Description
Hello.
Basic Setup: I want to have a Xamarin.Forms.ToolbarItem and a Xamarin.Forms.ControlTemplate in a ContentPage with a ViewModel. The ToolbarItem has a Command which should be executed on Tap.
Problem: The ToolbarItem.Command has a Binding to the ViewModel.Command which does not work.
When I start and click on the ToolbarItem it does not work.
Can someone tell me if I am missing something here, or if this is a real issue?
I am not very experienced with Bug reports so if I need to add/change something pleas tell me.
Steps to Reproduce
Create .net standard Project with Prism.Template
Choose Android Project for execution
Create a new Page with new Item Prism Content Page
Navigate at startup to that page
App.xaml.cs
await NavigationService.NavigateAsync("NavigationPage/DummyPage");
Insert the Code
DummTestPage
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http: //xamarin.com/schemas/2014/forms"
xmlns:x="http: //schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="TestNetCore.Views.DummyPage">
<ContentPage.ControlTemplate>
<ControlTemplate>
<StackLayout>
<Grid>
<BoxView BackgroundColor="Lime" />
<Label>I TEST ControlTemplate</Label>
</Grid>
<ContentPresenter />
</StackLayout>
</ControlTemplate>
</ContentPage.ControlTemplate>
<ContentPage.ToolbarItems>
<ToolbarItem Text="A" Clicked="MenuItem_OnClicked" Command="{Binding ToolbarTappedCommand}"></ToolbarItem>
</ContentPage.ToolbarItems>
<StackLayout>
<Label FontSize="28">I AM AN EMPTY PAGE</Label>
</StackLayout>
</ContentPage>
DummTestPage.cs
public partial class DummyPage : ContentPage
{
#region Public Constructors
public DummyPage()
{
InitializeComponent();
}
#endregion Public Constructors
#region Private Methods
private void MenuItem_OnClicked(object sender, EventArgs e)
{
}
#endregion Private Methods
}
DummyPageViewModel
public class DummyPageViewModel : BindableBase
{
#region Public Constructors
public DummyPageViewModel()
{
ToolbarTappedCommand = new NewCommand();
}
#endregion Public Constructors
#region Public Properties
public ICommand ToolbarTappedCommand { get; set; }
#endregion Public Properties
}
public class NewCommand : ICommand
{
#region Public Events
public event EventHandler CanExecuteChanged;
#endregion Public Events
#region Public Methods
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
}
#endregion Public Methods
}
Set a Breakpoint to
NewCommand.CanExecute
NewCommand.Execute
MenuItem_OnClicked
Execute Programm
Expected Behavior
Execute NewCommand.CanExecute on startup
Execute NewCommand.Execute on Tap
Execute MenuItem_OnClicked on Tap
Actual Behavior
Execute MenuItem_OnClicked on Tap
Basic Information
Version with issue: Prism Template Pack 2.0.8 | Prism.Unit.Forms 7.0.0.362
Last known good version: -
Xamarin.Forms version: 2.5.0.280555
IDE: VS17 15.6.1
Screenshots
Reproduction Link
What exactly do i need to do here?
Solutions
Removeing the lines in the XAML
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
Every thing works as expected now
2. Removeing the ControlTemplate in the XMAL
<ContentPage.ControlTemplate>
<ControlTemplate>
<StackLayout>
<Grid>
<BoxView BackgroundColor="Lime" />
<Label>I TEST ControlTemplate</Label>
</Grid>
<ContentPresenter />
</StackLayout>
</ControlTemplate>
</ContentPage.ControlTemplate>
Works but you dont have a controlTemplate now
3. Switching ControlTemplate and ToolbarItems
<ContentPage.ToolbarItems>
<ToolbarItem Text="A" Clicked="MenuItem_OnClicked" Command="{Binding ToolbarTappedCommand}"></ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.ControlTemplate>
<ControlTemplate>
<StackLayout>
<Grid>
<BoxView BackgroundColor="Lime" />
<Label>I TEST ControlTemplate</Label>
</Grid>
<ContentPresenter />
</StackLayout>
</ControlTemplate>
</ContentPage.ControlTemplate>
CanExecute is not called at beginning
And if you set the ControlTemplate as StaticRessouce (which is what I like to do) this does not work
ControlTemplate="{StaticResource BaseControlTemplate}"
Tags
Xamarin.Froms ControlTemplate ToolbarItem Binding not working