Command not working

Topics: Prism v2 - Silverlight 3
Jul 28, 2009 at 11:55 PM

Hello,

I am trying to get a command working in a login screen that is displayed prior to displaying the Shell. Here are the various pieces:

App.xaml.cs:

            Bootstrapper bootstrapper = new Bootstrapper();

            bootstrapper.Run();

 

            var view = new LogOnView();

            var presenter = new LogOnPresenter(view);

            Application.Current.RootVisual = view; // Ask the user to log on

Bootstrapper.cs:

        protected override DependencyObject CreateShell()

        {

            presenter = Container.Resolve<ShellPresenter>();

            IShellView view = presenter.View;

 

            return view as DependencyObject;

        }

 

LogOnPresenter.cs:

 

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

 

        public LogOnPresenter(LogOnView view)

        {

            this.View = view;

            view.Model = this;

            this.LoginCommand = new DelegateCommand<object>(

this.Login, this.CanLogin);

 

        }

LogOnView.xaml.cs:

        public LogOnPresenter Model

        {

            get { return DataContext as LogOnPresenter; }

            set

            {

                DataContext = value;

            }

        }

LogOnView.xaml:

<UserControl x:Class="UITest.LogOnView"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"

    Width="400" Height="300">

    <Grid x:Name="LayoutRoot" Background="White">

        <StackPanel>

            <TextBlock Text="Please log in"/>

            <Button Content="Login"

cal:Click.Command="{Binding LoginCommand}"  />

        </StackPanel>

    </Grid>

</UserControl>

 

I have verified that the data context is being set properly. The issue is that my LoginCommand code is not being called when I click on the button.

Any ideas? Any suggestions for how to debug this?

Sorry about the formatting. Pasting from Word does not work very well.

Thank you,

Rick

Jul 29, 2009 at 12:44 AM

I don't see it here but I am assuming that the this.Login method conforms to the necessary method signature. One thing you should try is to move your setting of the View.Model to be the very last item in construction.

I seem to remember needing to do this for the data binding to work correctly.

As an FYI, my ViewModel (Presenter) base has the following:

        public ViewModelBase(TView view, IEventAggregator aggregator, IUnityContainer container)
        {
            View = view;
            EventAggregator = aggregator;
            Container = container;

            ViewTitle = "Unknown";

            InitializeEvents();

            InitializeCommands();

            InitializeData();
        }

From this you can see that I set the basic items in a particular order and the derived class is responsible for setting the Model = this. I think I have it so I can move that to the base also but it was not a priority yet. 

Jul 29, 2009 at 1:03 PM

Moving the assignment of the Model to last fixed the problem. I understand why but it seems to me that this is pretty stupid and a sign of a poor framework implementation. The order you set the Model should not break functionality. imho

Thank you for the fix.

Rick