Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

c# - How to call a method of View in ViewModel?

I need to call a method of the View in the ViewNodel because in the View I can access different things that in the ViewModel not and I need to call that method. Is this possible to do?

Method in View:

  private void MyMethod(object sender, EventArgs e)
    {
        Book book = null;
        if (sender is StackLayout)
        {
            book = ((StackLayout)sender).BindingContext as Book;

            if (null == viewModel.BookSelected || !book.BookId.Equals(viewModel.BookSelected.BookId))
            {
                //Do something
            }
        }
    }
question from:https://stackoverflow.com/questions/65922929/how-to-call-a-method-of-view-in-viewmodel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think it is quite possible to access everything from view to viewmodel. If you are unable to do that in certain situation then, you can use event in your VM and raise that event when you need and you can bind a method that you want to call. Please check my sample code.

    public class View
    {
        ViewModel myVM = null;
        public View()
        {
            myVM=new ViewModel();
            myVM.CallMyMethodEvent += myViewMethod;
        }
        void myViewMethod(bool param)
        {
            //do you thing
        }
    }
    public class ViewModel
    {
        public Action<bool> CallMyMethodEvent;

        private void RaiseEventToCallMethodInView()
        {
            if (CallMyMethodEvent != null)
            {
                CallMyMethodEvent.Invoke(true);
            }
        }
    }

New Update Code as you want to call MyMethod() with 2 parameters. So, the code will look like =>

public class View
    {
        ViewModel myVM = null;
        public View()
        {
            myVM.CallMyMethodWithEventArgEvent += MyMethod;
        }
        private void MyMethod(object sender, EventArgs e)
        {
            if(sender is StackLayout)
            {
                var yourStackLayoutobject = (StackLayout)sender;
            }
        }
    }
    public class ViewModel
    {
        public Action<object, EventArgs> CallMyMethodWithEventArgEvent;
        private void RaiseEventToCallMethodWithEventArgInView()
        {
            if (CallMyMethodWithEventArgEvent != null)
            {
                CallMyMethodWithEventArgEvent.Invoke(new StackLayout(),null);
            }
        }
    }
    public class StackLayout
    {
    }

NOTE: Please check the code and let me know. I have used a dummy class StackLayout you can use your proper class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...