Calling a Method in View's CodeBehind from Vie

2020-02-26 04:24发布

I have a method within the code behind of my View (this method does something to my UI).

Anyway, I'd like to trigger this method from my ViewModel. How could this be done?

标签: c# wpf mvvm
5条回答
Animai°情兽
2楼-- · 2020-02-26 04:31

You can do it like this in View (code behind).

It casts to an interface to be implemented by the ViewModel, so that you are not constrained to one specific ViewModel type.

    // CONSTRUCTOR
    public SomeView()
    {
        InitializeComponent();

        DataContextChanged += DataContextChangedHandler;
    }

    void DataContextChangedHandler(object sender, DependencyPropertyChangedEventArgs e)
    {
        var viewModel = e.NewValue as IInterfaceToBeImplementedByViewModel;

        if (viewModel != null)
        {
            viewModel.SomeEvent += (sender, args) => { someMethod(); }
        }
    }
查看更多
孤傲高冷的网名
3楼-- · 2020-02-26 04:35

I saw youre reply to the answer above, you are saying that you want your ViewModel to retrieve data and then tell your view to stop the busy indicator.

I'm not sure if my solution would be the best solution, but you can give it a try, and maybe someone can correct if I'm wrong.

So from your view, you would call a method from ViewModel to start reading the dataset, am I right? In this method, you can pass a delegate (pointing to a method that exists in your view) and when your ViewModel finishes reading the dataset from the server, trigger the delegate (from your viewmodel) that is linked to your method in your view that can stop the busy indicator.

so in your view you have

void StopBusyIndicator()
{
    this.BusyIndicator.IsBusy = false;
}

and when you call your ViewModel to read dataset,

call it like this:

ViewModel.ReadDataSet( ()= >StopBusyIndicator)

which will pass the StopBusyIndicator method as a delegate, which you can call at the end of your ReadDataSet.

HTH

查看更多
该账号已被封号
4楼-- · 2020-02-26 04:45

You could write an action class that accepts a Data Transfer object. Within the DTO, add a property called "View" and assign it the current view. Call the action via the controller from within your view's codebehind, unbox the DTO and now you have full control of the view within the action class.

If you truely want to do this in your model, just create the method with a "View" type parameter in your Model and execute it, passing in the current view.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-26 04:52

My (and maybe others?) difficulty with MVVM was to understand a simple thing: View knows about ViewModel. I was using bindings and commands, but they are simple strings in xaml. Because of safe resolving at run-time (safe means you can do a typo, but software will not crash) this makes view decoupled from view-model (at compile time at least). And I was always looking for solution to keep this decoupling, to example, behaviors.

Truth is, you can get access directly to view model, which is typically a DataContext of window/user control:

var vm = (MyViewModel)this.DataContext;

Knowing that, using events probably the best way to call view method from view model, because view model don't know if there is subscriber, it just firing that event and event can be used by view or another view model.

// define in the view model
public delegate void MyEventAction(string someParameter, ...);
public event MyEventAction MyEvent;

// rise event when you need to
MyEvent?.Invoke("123", ...);

// in the view
var vm = (MyViewModel)DataContext;
vm.MyEvent += (someParameter, ...) => ... // do something
查看更多
三岁会撩人
6楼-- · 2020-02-26 04:53

According to MVVM pattern ViewModel is not aware of View, so this is not acceptable. To interact with ViewModel View could trigger a command, also you can use bindings. Moreover, you should not move UI-specific things like BusyIndicator to ViewModel level.

Please provide more details regardign your concrete use case - when you want to call a View's method and what this method does.

查看更多
登录 后发表回答