Reputation: 41
I have following code in XAML
<Text Box x:Name="Text_Mobile_Number"
Height="70" Width="450"
Margin="24,250,0,0"
Text="{Binding Path=Mobile_No, Mode=Twoway, UpdateSourceTrigger=Explicit}">
I tried to bind text_box with property Mobile_No,which is define in View_Model:
private string _mobile_No;
public string Mobile_No
{
get
{
return _mobile_No;
}
set
{
if(_mobile_No != value)
_mobile_No = value;
RaisePropertyChanged("Mobile_No");
}
}
and in C# i wrote a function where i called a service,through which i will get the mobile no,and i wrote that function in viewmodel too.
but now when i call that function on click event of button the call not goes to the properties,so it didn't set the mobile no in that properties. following is my function call:
private async void Next_AppBarIconButton_Click(object sender, EventArgs e)
{
LogInViewModel view_Model = new LogInViewModel();
var tuple = (Tuple<bool, string>)await view_Model.Get_Verification_Code();
}
but its not working properly,wen i assign actual text box value that is
Mobile_No=Text_box.text
then it works fine,function retrieve that value
My actual function:
public async Task<object> Get_Verification_Code()
{
var flag = false;
string message="";
var result = (Tuple<string, string, string>)await service.Get_Verification_Code(Mobile_No,"sign_in");
}
so please suggest any solution for this,i don't want to manually set text box value to the property Mobile_No.
I have tried all the option and searches all snippets related to text box binding,but not getting where i was wrong in code,please tell me any solution.Thank you
Upvotes: 0
Views: 461
Reputation: 4292
you are creating new instance of the ViewModel in your button click method so this new instance will not have the values.
Add the following 2 lines of code at the end of your view(MainPage.xaml.cs)
public MainPage()
{
InitializeComponent();
LoginViewModel vm = new LoginViewModel();
this.DataContext = vm;
}
Update this method to as follows
private async void Next_AppBarIconButton_Click(object sender, EventArgs e)
{
LogInViewModel view_Model = this.DataContext as LoginViewModel;
//Now you have the instance which binded to your view which contains all the data.
var tuple = (Tuple<bool, string>)await view_Model.Get_Verification_Code();
}
P.S: I would recomend to use GalaSoft MVVM Light framework for you mvvm application
Hope this helps
Upvotes: 1