运行时更改ViewModels的一个视图

时间:2018-01-30 21:42:13

标签: c# mvvm uwp uwp-xaml

我正在做一个商业UWP应用程序,我从各种表中输入大量数据。虽然要填写的数据发生变化,但视图的基本布局始终保持一致。在我看来,加载View应该很容易,然后在运行时只更换VM?

我实际上是在尝试保持同一页面以同样的方式更新多个输入表单,而我需要做的就是更改视图模型并使用模板选择器?

我问,因为我之前从未见过有人这样做,但对我来说,MVVM的一个要点是你可以轻松地交换东西。有人对此有任何意见或经验吗?

1 个答案:

答案 0 :(得分:1)

您只需更改绑定xaml UI的ViewModel即可更新View。这是一个简单的例子来澄清它。

我有TestModel班,

public class TestModel
{
    public string ShowText { get; set; }
}

这是XAML,

<Page.DataContext>
    <viewmodel:TestViewModel/>
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{Binding ViewModelList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding ShowText}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

如果要更改视图数据,只需操作TestViewModel即可更改数据以更新View内容。在此示例中,我使用DispatcherTimer来更改ViewModel,

这是ViewModel类,

public TestViewModel()
{
    ViewModelList = new ObservableCollection<TestModel>();

    ViewModelList.Add(new TestModel { ShowText = "this is first test" });
    ViewModelList.Add(new TestModel { ShowText = "this is second test" });
    ViewModelList.Add(new TestModel { ShowText = "this is third test" });

    //Create a timer to update the data source.
    var dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
    dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, object e)
{
    ViewModelList.Add(new Models.TestModel() { ShowText = "this is the added item" });
}

如果要通过ViewModel类中的属性更改自动更新视图,则可能需要实现INotifyPropertyChanged接口。以下是ViewModel类的示例代码,

public class TestViewModel : INotifyPropertyChanged
{
    public TestViewModel()
    {
        ViewModelList = new ObservableCollection<TestModel>();
        ViewModelList.Add(new TestModel { ShowText = "this is first test" });
        ViewModelList.Add(new TestModel { ShowText = "this is second test" });
        ViewModelList.Add(new TestModel { ShowText = "this is third test" });

        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
        dispatcherTimer.Start();
    }

    int i;

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    private void dispatcherTimer_Tick(object sender, object e)
    {
        i++;
        ViewModelList = new ObservableCollection<TestModel>();
        ViewModelList.Add(new TestModel { ShowText = "this is first test" + ">>" + i });
        ViewModelList.Add(new TestModel { ShowText = "this is second test" + ">>" + i });
        ViewModelList.Add(new TestModel { ShowText = "this is third test" + ">>" + i });
    }

    private ObservableCollection<TestModel> _ViewModelList;
    public ObservableCollection<TestModel> ViewModelList
    {
        get
        {
            return _ViewModelList;
        }
        set
        {
            _ViewModelList = value;
            RaisePropertyChanged("ViewModelList");
        }
    }
}

您可以从Data binding主题和{Binding} markup extension文档中了解更多信息。