两个 ViewModel 之间的 c# Prism 连接

c# Prism connection between two ViewModels

我正在开发一个 Xamarin Forms 应用程序,它使用 Prism 实现 MVVM,我遇到了一些问题,更准确地说是在建立两个 ViewModel 之间的连接方面。

在我的 MainPage 中,我有一个 UserControl 和一个 Label(TextBlock),UserControl 有一个选择器(ComboBox)。

我想要实现的是在 MainPageViewModels 和 UserControlViewModel 之间建立连接,这样每次用户在选择器中更改选择时,UserControlViewModel 都会通知 MainPageViewModels,然后它可以更改 Label(TextBlock ).

有我的代码:

主页:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:views="clr-namespace:MyNewApp.Views"
            xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
            prism:ViewModelLocator.AutowireViewModel="True"
            x:Class="MyNewApp.Views.MainPage">

    <ContentPage.Content>
        <StackLayout>

            <views:UserControl x:Name="UserControl"/>

            <Label Text="{Binding NumberChosen}"/>

        </StackLayout>
    </ContentPage.Content>

</ContentPage>

MainPageViewModel:

using Prism.Events;
using Prism.Mvvm;

namespace MyNewApp.ViewModels
{
    class MainPageViewModel : BindableBase
    {
        private int _NumberChosen;
        public int NumberChosen
        {
            get { return _NumberChosen; }
            set { SetProperty(ref _NumberChosen, value); }
        }

        public MainPageViewModel()
        {
            // Must Read the value on the picker of the UserControl.
            NumberChosen = 0;
        }
    }
}

用户控件:

<?xml version="1.0" encoding="utf-8" ?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="MyNewApp.Views.UserControl">

    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Label Grid.Row="0" Text="Pick a number:"/>

    <Picker Grid.Row="1" 
            ItemsSource="{Binding NumericSource}"
            SelectedItem="{Binding NumberSelected}"
            />

</Grid>

UserControlViewModel

using Prism.Events;
using Prism.Mvvm;
using System.Collections.ObjectModel;

namespace MyNewApp.ViewModels
{
    public class UserControlViewModel : BindableBase
    {
        public ObservableCollection<int> NumericSource { get; set; }

        private int _NumberSelected;
        public int NumberSelected
        {
            get { return _NumberSelected; }
            set { SetProperty(ref _NumberSelected, value); }
        }

        public UserControlViewModel()
        {
            var source = SourceCreator();

            NumericSource = source;
            NumberSelected = source[0];
        }

        ObservableCollection<int> SourceCreator()
        {
            ObservableCollection<int> sourceCreator = new ObservableCollection<int>();
            for (int i = 21; i < 99; i++)
            {
                sourceCreator.Add(i);
            }
            return sourceCreator;
        }
    }
}

感谢您的帮助。

1- 在名为 NumberChosen

的用户控件中定义可绑定 属性

2-在UserControl的Xaml中,绑定NumberChosen到NumberSelected(你选择器的SelectedItem)(你也可以直接绑定你选择器的SelectedItem)

https://www.c-sharpcorner.com/article/binding-control-to-control-in-xamarin-forms/

3- 在主页中,如果你只想在你的视图中显示,你可以像前面提到的那样绑定控件。

4- 您也可以将用户控件的 ChosenNumber 绑定到主视图模型中的 属性