How service agent get the values from view ?

Jul 13, 2011 at 5:54 AM
Edited Jul 13, 2011 at 5:55 AM

Hello All,

I am a newbie to MVVM. I am currently trying implement a login page where I want take values ( username and password ) from view to the service agent where I will compare the collected values with some stored values. Here I am not clear about how I will get the values from the view model in the service agent, so that I can compare them.

Thanks,

 

Sowmyan

Jul 13, 2011 at 5:14 PM
Typically the service agent will have a Login method that accepts username and password as parameters. There might be a LoginUser method on the ViewModel that you would invoke either from an event trigger or a command. You would call ServiceAgent.Login from ViewModel.LoginUser (example pseudo-code).

Cheers,
Tony
Sent from my iPhone

On Jul 12, 2011, at 11:54 PM, "sowmyancs"<notifications@codeplex.com> wrote:

From: sowmyancs

Hello All,

I am a newbie to MVVM. I am currently trying implement a login page where I want take values ( username and password ) from view to the service agent where I will compare the collected values with some stored values. Here I am not clear about how I will get the values from the view model in the service agent, so that I can compare them.


<button style="width: 711px; height: 357px;">












</button>



Apreciate your help !

Thanks,

Sowmyan

Jul 13, 2011 at 8:02 PM

Hi Tony,

Thank you so much for your reply.

I have tried it but still not getting the values. May be I will be missing something in the application. Is there any samples or links that I can refer.

Apreciate your help.

Regards,

Sowmyan

 

Jul 13, 2011 at 8:14 PM
Getting values to pass to the service agent method is simply a matter of reading properties in the ViewModel to which controls in the View are bound. Take a look at the Main sample Part 1 to see how that is done. There I am reading the selected value from a combo box to get products belonging to the selected category.

Cheers,
Tony
Sent from my iPhone

On Jul 13, 2011, at 3:02 PM, "sowmyancs"<notifications@codeplex.com> wrote:

From: sowmyancs

Hi Tony,

Thank you so much for your reply.

I have tried it but still not getting the values. May be I will be missing something in the application. Is there any samples or links that I can refer.

Apreciate your help.

Regards,

Sowmyan

Jul 13, 2011 at 8:41 PM

Hi Tony,

Thank you so much.

I though that if we just mention the DataContext in the Grid and specify the model attributes to the bindings of each textbox it will automatically take care of setting the corrusponding properties in the model.

Currenlty, I have implemented in a way how it works in normal code behind , reading the values from the controls and passing it to the mock service agent directly. I will look into your sample application as well.

view

======

<phone:PhoneApplicationPage
    x:Class="VisualKB.Views.LoginViewPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   
    xmlns:local="clr-namespace:VisualKB.Views"
   
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
    xmlns:mvvm="clr-namespace:SimpleMvvmToolkit;assembly=SimpleMvvmToolkit-WP7"
   
   
   
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

 
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" HorizontalAlignment="Left" Width="480" DataContext="{Binding Source={StaticResource Locator}, Path=LoginViewModel}">
     <Grid x:Name="PageTitleGrid"
           Background="Transparent" Margin="0,2,0,-2">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
           

        <!--Bind to AppTitle and PageTitle properties -->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Background="Black" >
         <TextBlock x:Name="PageTitle"  Text="Visual KB" FontSize="64" FontFamily="Impact" Foreground="#FF2182E7" Margin="0,0,225,0"/>
        </StackPanel>
  
  </Grid>
     <Grid Height="156" Margin="48,161,101,0" VerticalAlignment="Top">
      <Grid.ColumnDefinitions>
       <ColumnDefinition Width="0.066*"/>
       <ColumnDefinition Width="0.58*"/>
       <ColumnDefinition Width="0.154*"/>
       <ColumnDefinition Width="0.199*"/>
      </Grid.ColumnDefinitions>

            <TextBlock x:Name="lblDomain" Height="30" Margin="3,-32,88,0" TextWrapping="Wrap" Text="Domain" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
            <TextBox x:Name="txtDomain" Text="northamerica" Margin="89,-47,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" DataContext="{Binding Path=Model.Domain, Mode=TwoWay}" />

            <TextBlock x:Name="lblUser" Height="30" Margin="3,15,88,0" TextWrapping="Wrap" Text="User" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
      <TextBlock x:Name="lblPassword" Margin="0,63,91,63" TextWrapping="Wrap" Text="Password" FontSize="16" Grid.ColumnSpan="2"/>
      <TextBox x:Name="txtUser" Text="sowmyans" Margin="89,0,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" DataContext="{Binding Path=Model.Name, Mode=TwoWay}" />
      <TextBox x:Name="txtPassword" Text="test" Margin="90,49,0,51" TextWrapping="Wrap" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" DataContext="{Binding Path=Model.Password, Mode=TwoWay}" />
            <Button x:Name="btnSignIn" Click="btnSignIn_Click"   Content="Sign In" Margin="89,0,-15,0" VerticalAlignment="Bottom" Height="58" FontSize="16" BorderBrush="#FF034786"  Grid.Column="1" >
       <Button.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
         <GradientStop Color="Black" Offset="0"/>
         <GradientStop Color="#FF0C5EA1" Offset="1"/>
        </LinearGradientBrush>
       </Button.Background>

       <!--      <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <mvvm:EventToCommand
                        Command="{Binding GetUserCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers> -->
            </Button>
          
           
          

        </Grid>
     
  
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="#FF53B0D1" Offset="1"/>
                <GradientStop Color="#FF103D4D"/>
                <GradientStop Color="#FF4CA3DD" Offset="0.564"/>
            </LinearGradientBrush>
        </Grid.Background>

       

    </Grid>

  </phone:PhoneApplicationPage>

 

 

using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace VisualKB.Views
{
    public partial class LoginViewPage : PhoneApplicationPage
    {

        public static LoginViewModel viewModel;
        public LoginViewPage()
        {
            InitializeComponent();
          }

        MockLoginServiceAgent oMockServiceAgent = new MockLoginServiceAgent();      

        private void btnSignIn_Click(object sender, RoutedEventArgs e)
        {
             string strDomain = txtDomain.Text;
            string strName = txtUser.Text;
            string strPassword = txtPassword.Text;
            User oUser = new User(strDomain, strName, strPassword);
            bool IsUserExists = oMockServiceAgent.LogingUser(oUser);
            if (IsUserExists)
            {
                NavigationService.Navigate(new Uri("/Views/KBSearch.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("User doesn't exist !");
            }
           
           
        }

       
    }
}

Jul 13, 2011 at 8:50 PM
What does your LoginViewModel look like? By the way, I would suggest binding the Text property of each TextBox, instead of the DataContext. You want instead to use the Grid's DataConext. :)

Cheers,
Tony
Sent from my iPhone

On Jul 13, 2011, at 3:41 PM, "sowmyancs"<notifications@codeplex.com> wrote:

From: sowmyancs

Hi Tony,

Thank you so much.

I though that if we just mention the DataContext in the Grid and specify the model attributes to the bindings of each textbox it will automatically take care of setting the corrusponding properties in the model.

Currenlty, I have implemented in a way how it works in normal code behind , reading the values from the controls and passing it to the mock service agent directly. I will look into your sample application as well.

view

======

<phone:PhoneApplicationPage
x:Class="VisualKB.Views.LoginViewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:VisualKB.Views"

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
xmlns:mvvm="clr-namespace:SimpleMvvmToolkit;assembly=SimpleMvvmToolkit-WP7"



FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">


<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" HorizontalAlignment="Left" Width="480" DataContext="{Binding Source={StaticResource Locator}, Path=LoginViewModel}">
<Grid x:Name="PageTitleGrid"
Background="Transparent" Margin="0,2,0,-2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--Bind to AppTitle and PageTitle properties -->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Background="Black" >
<TextBlock x:Name="PageTitle" Text="Visual KB" FontSize="64" FontFamily="Impact" Foreground="#FF2182E7" Margin="0,0,225,0"/>
</StackPanel>

</Grid>
<Grid Height="156" Margin="48,161,101,0" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.066*"/>
<ColumnDefinition Width="0.58*"/>
<ColumnDefinition Width="0.154*"/>
<ColumnDefinition Width="0.199*"/>
</Grid.ColumnDefinitions>

<TextBlock x:Name="lblDomain" Height="30" Margin="3,-32,88,0" TextWrapping="Wrap" Text="Domain" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtDomain" Text="northamerica" Margin="89,-47,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" DataContext="{Binding Path=Model.Domain, Mode=TwoWay}" />

<TextBlock x:Name="lblUser" Height="30" Margin="3,15,88,0" TextWrapping="Wrap" Text="User" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
<TextBlock x:Name="lblPassword" Margin="0,63,91,63" TextWrapping="Wrap" Text="Password" FontSize="16" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtUser" Text="sowmyans" Margin="89,0,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" DataContext="{Binding Path=Model.Name, Mode=TwoWay}" />
<TextBox x:Name="txtPassword" Text="test" Margin="90,49,0,51" TextWrapping="Wrap" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" DataContext="{Binding Path=Model.Password, Mode=TwoWay}" />
<Button x:Name="btnSignIn" Click="btnSignIn_Click" Content="Sign In" Margin="89,0,-15,0" VerticalAlignment="Bottom" Height="58" FontSize="16" BorderBrush="#FF034786" Grid.Column="1" >
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF0C5EA1" Offset="1"/>
</LinearGradientBrush>
</Button.Background>

<!--

<mvvm:EventToCommand
Command="{Binding GetUserCommand}"/>

-->
</Button>


</Grid>


<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF53B0D1" Offset="1"/>
<GradientStop Color="#FF103D4D"/>
<GradientStop Color="#FF4CA3DD" Offset="0.564"/>
</LinearGradientBrush>
</Grid.Background>

</Grid>

</phone:PhoneApplicationPage>

using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace VisualKB.Views
{
public partial class LoginViewPage : PhoneApplicationPage
{

public static LoginViewModel viewModel;
public LoginViewPage()
{
InitializeComponent();
}

MockLoginServiceAgent oMockServiceAgent = new MockLoginServiceAgent();

private void btnSignIn_Click(object sender, RoutedEventArgs e)
{
string strDomain = txtDomain.Text;
string strName = txtUser.Text;
string strPassword = txtPassword.Text;
User oUser = new User(strDomain, strName, strPassword);
bool IsUserExists = oMockServiceAgent.LogingUser(oUser);
if (IsUserExists)
{
NavigationService.Navigate(new Uri("/Views/KBSearch.xaml", UriKind.Relative));
}
else
{
MessageBox.Show("User doesn't exist !");
}


}


}
}

Jul 13, 2011 at 10:49 PM

Hi,

Thanks for the hint :). As you said i have changed the binding from DataContext to Text property of the textBox and also modified ViewModel --> LoginUser method to take 3 args , Domain , Name and Password. And the same method is trying to invoke the loginUser() of ServiceAgent. The serviceAgent is responsible for authenticating the user.

Now my question is :

How do I get the Domain , Name and password in my viewModel to invoke the LoginUser which take these 3 args ?

How to retrieve it?

I tried debugging , but the values are not getting populated in the viewModel. I also tried passing the values as commandParameter and CommandParameterValues. But  I am not sure how to retrieve these parameter values in the code.

 

Please help.

 

here is my viewModel

======================

 

using System;
using System.Windows;
using System.Windows.Input;
using System.Threading;
using System.Collections.ObjectModel;
using VisualKB.Views;

// Toolkit namespace
using SimpleMvvmToolkit;

// Toolkit extension methods
using SimpleMvvmToolkit.ModelExtensions;

namespace VisualKB
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class LoginViewModel : ViewModelDetailBase<LoginViewModel, User>
    {
        #region Initialization and Cleanup
 
       
        // TODO: Add a member for IXxxServiceAgent
        private ILoginServiceAgent serviceAgent;
      

        // Default ctor
        public LoginViewModel() {
       
           
        }

        // TODO: ctor that accepts IXxxServiceAgent
        public LoginViewModel(ILoginServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;           
        }

        #endregion

        #region Notifications

        // TODO: Add events to notify the view or obtain data from the view
        public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;

        #endregion

        #region Properties
      
        #region User Name

        private string name;
        /// <summary>
        /// The user's name.
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                NotifyPropertyChanged(m => m.name);
            }
        }

        #endregion

        #region Password

        private string password;
        /// <summary>
        /// The user's password.
        /// </summary>
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                NotifyPropertyChanged(m => m.password);
            }
        }

        #endregion

        #region Domain

        private string domain;
        /// <summary>
        /// The user's name.
        /// </summary>
        public string Domain
        {
            get
            {
                return domain;
            }
            set
            {
                domain = value;
                NotifyPropertyChanged(m => m.domain);
            }
        }

        #endregion
       
        #endregion

        #region Methods

        public void LoginUser(string domain, string name ,string password)
        {
            User oUser = new User(domain, name, password);
            bool isUserExists = serviceAgent.LogingUser(oUser);               
           
        }

       public ICommand GetUserCommand
        {
           
            get
            {
                return new DelegateCommand(LoginUser("?","?","?");
            }
        }

        #endregion

        #region Completion Callbacks

        // TODO: Optionally add callback methods for async calls to the service agent
       
        #endregion

        #region Helpers

        // Helper method to notify View of an error
        private void NotifyError(string message, Exception error)
        {
            // Notify view of an error
            Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
        }

        #endregion
    }
}

View

============================================================== 

 

<phone:PhoneApplicationPage
    x:Class="VisualKB.Views.LoginViewPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   
    xmlns:local="clr-namespace:VisualKB.Views"
   
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
    xmlns:mvvm="clr-namespace:SimpleMvvmToolkit;assembly=SimpleMvvmToolkit-WP7"
   
   
   
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

 
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" HorizontalAlignment="Left" Width="480" DataContext="{Binding Source={StaticResource Locator}, Path=LoginViewModel}">
     <Grid x:Name="PageTitleGrid"
           Background="Transparent" Margin="0,2,0,-2">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
           

        <!--Bind to AppTitle and PageTitle properties -->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Background="Black" >
         <TextBlock x:Name="PageTitle"  Text="Visual KB" FontSize="64" FontFamily="Impact" Foreground="#FF2182E7" Margin="0,0,225,0"/>
        </StackPanel>
  
  </Grid>
     <Grid Height="156" Margin="48,161,101,0" VerticalAlignment="Top">
      <Grid.ColumnDefinitions>
       <ColumnDefinition Width="0.066*"/>
       <ColumnDefinition Width="0.58*"/>
       <ColumnDefinition Width="0.154*"/>
       <ColumnDefinition Width="0.199*"/>
      </Grid.ColumnDefinitions>

            <TextBlock x:Name="lblDomain" Height="30" Margin="3,-32,88,0" TextWrapping="Wrap" Text="Domain" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
            <TextBox x:Name="txtDomain" Text="{Binding LoginViewModel.Domain, Mode=TwoWay}" Margin="89,-47,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3"  />

            <TextBlock x:Name="lblUser" Height="30" Margin="3,15,88,0" TextWrapping="Wrap" Text="User" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
      <TextBlock x:Name="lblPassword" Margin="0,63,91,63" TextWrapping="Wrap" Text="Password" FontSize="16" Grid.ColumnSpan="2"/>
            <TextBox x:Name="txtUser" Text="{Binding LoginViewModel.Name, Mode=TwoWay}" Margin="89,0,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3"  />
            <TextBox x:Name="txtPassword" Text="{Binding LoginViewModel.Password,Mode=TwoWay}" Margin="90,49,0,51" TextWrapping="Wrap" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3"  />
            <Button x:Name="btnSignIn"   Content="Sign In" Margin="89,0,-15,0" VerticalAlignment="Bottom" Height="58" FontSize="16" BorderBrush="#FF034786"  Grid.Column="1" >
       <Button.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
         <GradientStop Color="Black" Offset="0"/>
         <GradientStop Color="#FF0C5EA1" Offset="1"/>
        </LinearGradientBrush>
       </Button.Background>

             <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <mvvm:EventToCommand
                        Command="{Binding GetUserCommand}" />                          
                           
                       
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
          
           
          

        </Grid>
     
  
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="#FF53B0D1" Offset="1"/>
                <GradientStop Color="#FF103D4D"/>
                <GradientStop Color="#FF4CA3DD" Offset="0.564"/>
            </LinearGradientBrush>
        </Grid.Background>

       

    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
  <!--  <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar> -->

</phone:PhoneApplicationPage>

Jul 14, 2011 at 3:17 AM
Change LoginUser on the ViewModel to accept no parameters and just pass into the sa methods the properties on the vm.

You need also to bind the button command to the command on the view.

Cheers,
Tony
Sent from my iPhone

On Jul 13, 2011, at 5:49 PM, "sowmyancs"<notifications@codeplex.com> wrote:

From: sowmyancs

Hi,

Thanks for the hint :). As you said i have changed the binding from DataContext to Text property of the textBox and also modified ViewModel --> LoginUser method to take 3 args , Domain , Name and Password. And the same method is trying to invoke the loginUser() of ServiceAgent. The serviceAgent is responsible for authenticating the user.

Now my question is :

How do I get the Domain , Name and password in my viewModel to invoke the LoginUser which take these 3 args ?

How to retrieve it?

I tried debugging , but the values are not getting populated in the viewModel. I also tried passing the values as commandParameter and CommandParameterValues. But I am not sure how to retrieve these parameter values in the code.

Please help.

here is my viewModel

======================

using System;
using System.Windows;
using System.Windows.Input;
using System.Threading;
using System.Collections.ObjectModel;
using VisualKB.Views;

// Toolkit namespace
using SimpleMvvmToolkit;

// Toolkit extension methods
using SimpleMvvmToolkit.ModelExtensions;

namespace VisualKB
{
/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// Use the mvvmprop snippet to add bindable properties to this ViewModel.
/// </para>
/// </summary>
public class LoginViewModel : ViewModelDetailBase<LoginViewModel, User>
{
#region Initialization and Cleanup


// TODO: Add a member for IXxxServiceAgent
private ILoginServiceAgent serviceAgent;

// Default ctor
public LoginViewModel() {


}

// TODO: ctor that accepts IXxxServiceAgent
public LoginViewModel(ILoginServiceAgent serviceAgent)
{
this.serviceAgent = serviceAgent;
}

#endregion

#region Notifications

// TODO: Add events to notify the view or obtain data from the view
public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;

#endregion

#region Properties

#region User Name

private string name;
/// <summary>
/// The user's name.
/// </summary>
public string Name
{
get
{
return name;
}
set
{
name = value;
NotifyPropertyChanged(m => m.name);
}
}

#endregion

#region Password

private string password;
/// <summary>
/// The user's password.
/// </summary>
public string Password
{
get
{
return password;
}
set
{
password = value;
NotifyPropertyChanged(m => m.password);
}
}

#endregion

#region Domain

private string domain;
/// <summary>
/// The user's name.
/// </summary>
public string Domain
{
get
{
return domain;
}
set
{
domain = value;
NotifyPropertyChanged(m => m.domain);
}
}

#endregion

#endregion

#region Methods

public void LoginUser(string domain, string name ,string password)
{
User oUser = new User(domain, name, password);
bool isUserExists = serviceAgent.LogingUser(oUser);

}

public ICommand GetUserCommand
{

get
{
return new DelegateCommand(LoginUser("?","?","?");
}
}

#endregion

#region Completion Callbacks

// TODO: Optionally add callback methods for async calls to the service agent

#endregion

#region Helpers

// Helper method to notify View of an error
private void NotifyError(string message, Exception error)
{
// Notify view of an error
Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
}

#endregion
}
}

View

==============================================================

<phone:PhoneApplicationPage
x:Class="VisualKB.Views.LoginViewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:VisualKB.Views"

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
xmlns:mvvm="clr-namespace:SimpleMvvmToolkit;assembly=SimpleMvvmToolkit-WP7"



FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">


<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" HorizontalAlignment="Left" Width="480" DataContext="{Binding Source={StaticResource Locator}, Path=LoginViewModel}">
<Grid x:Name="PageTitleGrid"
Background="Transparent" Margin="0,2,0,-2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--Bind to AppTitle and PageTitle properties -->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Background="Black" >
<TextBlock x:Name="PageTitle" Text="Visual KB" FontSize="64" FontFamily="Impact" Foreground="#FF2182E7" Margin="0,0,225,0"/>
</StackPanel>

</Grid>
<Grid Height="156" Margin="48,161,101,0" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.066*"/>
<ColumnDefinition Width="0.58*"/>
<ColumnDefinition Width="0.154*"/>
<ColumnDefinition Width="0.199*"/>
</Grid.ColumnDefinitions>

<TextBlock x:Name="lblDomain" Height="30" Margin="3,-32,88,0" TextWrapping="Wrap" Text="Domain" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtDomain" Text="{Binding LoginViewModel.Domain, Mode=TwoWay}" Margin="89,-47,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" />

<TextBlock x:Name="lblUser" Height="30" Margin="3,15,88,0" TextWrapping="Wrap" Text="User" VerticalAlignment="Top" FontSize="16" Grid.ColumnSpan="2"/>
<TextBlock x:Name="lblPassword" Margin="0,63,91,63" TextWrapping="Wrap" Text="Password" FontSize="16" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtUser" Text="{Binding LoginViewModel.Name, Mode=TwoWay}" Margin="89,0,1,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" />
<TextBox x:Name="txtPassword" Text="{Binding LoginViewModel.Password,Mode=TwoWay}" Margin="90,49,0,51" TextWrapping="Wrap" FontSize="16" Grid.Column="1" Grid.ColumnSpan="3" />
<Button x:Name="btnSignIn" Content="Sign In" Margin="89,0,-15,0" VerticalAlignment="Bottom" Height="58" FontSize="16" BorderBrush="#FF034786" Grid.Column="1" >
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF0C5EA1" Offset="1"/>
</LinearGradientBrush>
</Button.Background>



<mvvm:EventToCommand
Command="{Binding GetUserCommand}" />




</Button>


</Grid>


<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF53B0D1" Offset="1"/>
<GradientStop Color="#FF103D4D"/>
<GradientStop Color="#FF4CA3DD" Offset="0.564"/>
</LinearGradientBrush>
</Grid.Background>

</Grid>

<!--Sample code showing usage of ApplicationBar-->
<!-- <phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar> -->

</phone:PhoneApplicationPage>