开发者

In mvvm can we call viewmodel's methods from view?

开发者 https://www.devze.com 2023-02-17 07:16 出处:网络
I am trying to retrieve uri parameters in my viewmodels. I tried severals things but nothing works as expected... I would like to mimic OnNavigatedTo and OnNavigatedFrom methods of Page so I wrote :

I am trying to retrieve uri parameters in my viewmodels. I tried severals things but nothing works as expected... I would like to mimic OnNavigatedTo and OnNavigatedFrom methods of Page so I wrote :

In my ViewBase class (inherited from Page) :


public ViewModelBase ViewModel
{
    get { return this.D开发者_如何学编程ataContext as ViewModelBase; }
    protected set
    {
        if (value != this.DataContext)
        {
            this.DataContext = value;
        }
    }
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
    this.ViewModel.OnNavigatedFrom(e.Uri);
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    this.ViewModel.OnNavigatedTo(e.Uri);
}

and in my ViewModelBase class :


public virtual void OnNavigatedFrom(Uri uri)
{
}

public virtual void OnNavigatedTo(Uri uri)
{
}

What is wrong if I do that ?

Thanks in advance...


Typically in MVVM you would use some form of commanding or an alternative that many MVVM frameworks provide. I would seriously consider using an MVVM framework such as Prism, MVVM Light Toolkit, or Caliburn.Micro. They all have their own mechanisms for invoking verbs on your view model from the view.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号