简体   繁体   English

从另一个ViewModel Xamarin.Forms更新ViewModel

[英]Update ViewModel from another ViewModel Xamarin.Forms

I am working on a Xamari.Forms application in which I have a MainPageView.Xaml and MainPageViewModel.cs , also I have stackLayout inside MainPageView.Xaml where I'm loading a view ( binded to another viewmodel ) dynamically. 我正在开发一个Xamari.Forms应用程序,其中我有一个MainPageView.XamlMainPageViewModel.cs ,我在MainPageView.Xaml中有stackLayout,我正在动态加载一个视图( 绑定到另一个viewmodel )。 I have to update some values in second ViewModel which is binded to the view when ever there are some changes in MainPageViewModel.cs . 我必须在第二个ViewModel中更新一些值,当MainPageViewModel.cs中有一些更改时,它会绑定到视图。 I'm using Messaging Center for that now but everytime i cannot use Messaging center because I have to Unsubscribe it else it will get called multiple times. 我现在正在使用Messaging Center,但每次我都无法使用Messaging center,因为我必须取消订阅,否则它将被多次调用。 Is there any optimized way of calling and updating one viewmodel from another without navigating away from the screen. 是否有任何优化的方法从一个视图模型调用和更新一个视图模型而无需离开屏幕。

What you can do is to create a ContentView call it from you MainPageView.xaml and give him the ViewModel as BindingContext . 你可以做的是创建一个ContentView从你MainPageView.xaml调用它,并给他ViewModel作为BindingContext

For example: 例如:

OtherView.xaml

<ContentView>
    <StackLayout>
        <Label Text="{Binding MyText}" />
    </StackLayout>
</ContentView>

OtherViewModel.cs

public class OtherViewModel : INotifyPropertyChanged
{
    // Do you own implementation of INotifyPropertyChanged

    private string myText;
    public string MyText
    {
       get { return this.myText; }
       set
       {
           this.myText = value;
           this.OnPropertyChanged("MyText");
       }
    }

    public OtherViewModel(string text)
    {
        this.MyText = text;
    }
}

MainViewModel.cs

public class MainViewModel: INotifyPropertyChanged
{
    // Do you own implementation of INotifyPropertyChanged

    private OtherViewModel otherVM;
    public OtherViewModel OtherVM
    {
       get { return this.otherVM; }
    }

    public MainViewModel()
    {
        // Initialize your other viewmodel
        this.OtherVM = new OtherViewModel("Hello world!");
    }
}

MainPageView.xaml binding to MainViewModel MainPageView.xaml绑定到MainViewModel

<Page ....>
    <Grid>
        <!-- You have stuff here -->
        <OtherView BindingContext="{Binding OtherVM}" />
    </Grid>
</Page>

With this method you can display your custom view with the binding context you want. 使用此方法,您可以使用所需的绑定上下文显示自定义视图。

PS: this is code hasn't been tested, it's just pure theory. PS:这是代码尚未经过测试,它只是纯粹的理论。

Hope it helps. 希望能帮助到你。

This is a bit of an alternative approach and I only bring this up because you mentioned that you were using the MessagingCenter but you stopped because you were getting multiple events. 这是一种替代方法,我只提出这个问题,因为你提到你使用的是MessagingCenter,但是因为你收到了多个事件而停止了。 In this answer below I described a simple way that you can and easily subscribe and unsubscribe from events in your view model: Object disposing in Xamarin.Forms 在下面的这个答案中,我描述了一种简单的方法,您可以轻松地订阅和取消订阅视图模型中的事件: Xamarin.Forms中的对象处理

Basically I'm building a little bit of infrastructure so that the ViewModel knows when it's appearing (to subscribe to events) and disappearing (to unsubscribe from the events) this makes sure that you don't have multiple instances of your view model in memory which would have been likely causing the multiple events you were seeing. 基本上我正在构建一些基础结构,以便ViewModel知道它何时出现(订阅事件)并消失(取消订阅事件),这可以确保您的内存中没有多个视图模型实例这可能会导致你看到的多个事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM