xamarin baseviewmodel > childviewmodel 数据绑定不起作用

huangapple go评论58阅读模式
英文:

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 = &quot; This is string data&quot;;
        }


        bool SetProperty&lt;T&gt;(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(&quot;----------&gt; &quot; + myViewModel.returnedString);    
    }

huangapple
  • 本文由 发表于 2023年3月23日 10:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定