英文:
xamarin baseviewmodel > childviewmodel data bbinding not working
问题
xamarin forms.. i have INotifyPropertyChanged baseviewmodel and foreign childviewmodel.. i call to function from baseviewmodel and then binding to property. everything is ok.. but i call to property from childviewmodel then property already null
thanks all
public class BaseViewModel : INotifyPropertyChanged
{
private string _test;
public string Test{
get {return _test;}
set {setproperty(ref _test,value);}
}
public void callTest(){
Test = "This is string data";
}
}
public class ChildViewModel : BaseViewModel {
CallTest();
string returnedString = Test; // Test already null receiving
}
英文:
xamarin forms.. i have INotifyPropertyChanged baseviewmodel and foreign childviewmodel.. i call to function from baseviewmodel andd then binding to property. everything is ok.. but i call to property from childviewmodel then property already null
thanks all
public class BaseViewModel : INotifyPropertyChanged
{
private string _test;
public string Test{
get {return _test;}
set {setproprty(ref _test,value);}
}
public void callTest(){
Test = " This is string data";
}
}
public class ChildViewModel : BaseViewModel {
CallTest();
string returnedString = Test; // Test already null receiving
}
答案1
得分: 0
Based on your code, I created a demo and it works on my side.
You can refer to the following code:
BaseViewModel.cs
public class BaseViewModel : INotifyPropertyChanged
{
string _test;
public string Test
{
set { SetProperty(ref _test, value); }
get { return _test; }
}
public void callTest()
{
Test = " This is string data";
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
MyViewModel.cs
public class MyViewModel: BaseViewModel
{
public string returnedString;
public MyViewModel() {
callTest();
returnedString = Test;
}
}
Usage example:
private void Button_Clicked(object sender, EventArgs e)
{
MyViewModel myViewModel = new MyViewModel();
System.Diagnostics.Debug.WriteLine("----------> " + myViewModel.returnedString);
}
英文:
Based on your code, I created a demo and it works on my side.
You can refer to the following code:
BaseViewModel.cs
public class BaseViewModel : INotifyPropertyChanged
{
string _test;
public string Test
{
set { SetProperty(ref _test, value); }
get { return _test; }
}
// public BaseViewModel() {
// callTest();
// }
public void callTest()
{
Test = " This is string data";
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
MyViewModel.cs
public class MyViewModel: BaseViewModel
{
public string returnedString;
public MyViewModel() {
callTest();
returnedString = Test;
}
}
Usage example:
private void Button_Clicked(object sender, EventArgs e)
{
MyViewModel myViewModel = new MyViewModel();
System.Diagnostics.Debug.WriteLine("----------> " + myViewModel.returnedString);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论