两个ViewModel之间的c#Prism连接

时间:2018-06-27 15:00:12

标签: c# xamarin mvvm prism viewmodel

我正在开发一个Xamarin Forms应用程序,该应用程序将Prism用于MVVM,但我遇到了一些问题,更确切地说是在两个ViewModel之间建立连接。

在我的主页中,我有一个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;
        }
    }
}

UserControl:

<?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 个答案:

答案 0 :(得分:1)

1-在UserControl中定义名为NumberChosen的可绑定属性

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

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

3-在主页中,如果只想在视图中显示,则可以将控件绑定到控件,如前所述。

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

相关问题