通过将参数传递给构造函数

时间:2015-09-25 12:26:46

标签: wpf xaml mvvm data-binding mvvm-light

我在某种情况下陷入困境,并且不知道如何解决问题。情况是我有两个视图,每个视图都有相应的ViewModel类。 (让我们说View1和View2以及相应的VM1和VM2类) 情况是View1应该首先启动,我从用户读取一些输入(一些整数值),然后View2必须启动。问题是View2有一个UI,它必须重复用户在文本框中之前的View1启动之前输入整数值1步的次数。因此,您可以看到View2的UI取决于View1启动时的输入。

因此,当用户输入整数值时,VM2必须知道该值,以便VM1初始化VM2类以多次重复该ListBox(包含UI)。

我这样做的方法是这样的(这显然不起作用,这就是为什么我来这里接受经验丰富的MVVM人员的指导)。

我转到View2.cs类,之前在MainWindow()之后调用View1,就在InitializeComponent()之后;功能因此,View1将在View2推出之前启动。当用户进入它时,我从View1获取了值,然后我将该值传递给VM2 constuctor(如下所示:vm = new ViewModel(age);)以便我的UI将重复," age&# 34;可变时间(请参阅下面的代码)

让我说这是我的View2:

<Window x:Class="MVVMDialogWithReturnProperty.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vM="clr-namespace:MVVMDialogWithReturnProperty.ViewModels"
        Title="MainWindow" Height="450" Width="850">
    <Window.DataContext>
        <vM:ViewModel>            
        </vM:ViewModel>
    </Window.DataContext>
    <Grid Name="ButtonsContainer" Width="700" Height="600">
         <ListBox VerticalAlignment="Stretch"   Grid.Row="1" ItemsSource="{Binding lb_GlobalList}" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderBrush="Gray" Margin="0,30,0,0" Grid.RowSpan="2" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        //I have more UI here
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Save" Grid.RowSpan="2" Background="DarkGray" HorizontalAlignment="Center" VerticalAlignment="Top"
            Grid.Row="2"  Width="60px" Height="25px" Name="savebtn" Margin="40,5,0,0"
             Click="savebtn_Click"/>
    </Grid>
</Window>

在我的View2.cs中,我尝试这样做,以便我将获得View1输入的整数值

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyDialog dialog = new MyDialog();  //This will launch another Window which contains two textbox and textblocks.  "Title" and "Age"
            dialog.ShowDialog();
            if (dialog.RetVal != null)
            {
                string name = dialog.RetVal.Title;
                int age = dialog.RetVal.Age;
                ViewModel vm=DataContext as ViewModel;  
                vm = new ViewModel(age);// Here i get the integer variable "age" from View1 and i pass to the constructor of View2's VM2 (which is named as ViewModel in my case)

            }

        }
        private void savebtn_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = DataContext as ViewModel;
            Pyth py = new Pyth(viewModel);
            this.Close();

        }
    }

我正在做这件事vm = new ViewModel(age);因为我认为它会调用VM2(这里是ViewModel),并且会在其构造函数中使用#34; age&#34;(整数值)次数初始化我的ListBox,而不是View2启动,这样它的UI重复次数为& #34;年龄&#34;通过调用VM2的构造函数来完成这些:

  public ViewModel(int numberOfRepeatitions)
        {
            _canExecute = true;
            lb_GlobalList = new ObservableCollection<tablegenerateModel>();
            for (int i = 1; i < numberOfRepeatitions; i++)
            {
                _lb_GlobalList.Add(lb_Global = new tablegenerateModel()
                {
                    Name = "table" + i,
                    //contains other UI also like Textbox, Textblocks etc.
                });

            }
        }

但是我发现UI不会重复那么多次,它只重复了我在无参数的ViewModel构造函数中静态初始化的时间,但没有重复这个&#34; public ViewModel(int numberOfRepeatitions)&#34;。

如何更改代码,以便我将重复用户已输入的View2的UI以及之前View1启动时的#34; age&#34;(整数值)。

0 个答案:

没有答案
相关问题