Mvvm light 如何使用MVVM Light使用新的搜索结果刷新数据网格

Mvvm light 如何使用MVVM Light使用新的搜索结果刷新数据网格,mvvm-light,Mvvm Light,我正在使用最新的MMVM Light windows 8二进制文件和VS 2012最新更新,所以一切都很好。我是MVVM Light框架的新手,所以这是一个调整 我有一个带有网格的客户页面,该页面使用文本框和按钮进行搜索-文本框是绑定的,按钮使用命令。数据在视图模型中显示得很好。我检查了客户列表并设置了客户列表属性-所有操作都很好。问题是,页面不会刷新。当我转到另一个页面并返回到Customers页面时,将显示搜索到的数据 我怀疑视图模型是静态的,需要重新实例化 以下是各自的代码框架: publ

我正在使用最新的MMVM Light windows 8二进制文件和VS 2012最新更新,所以一切都很好。我是MVVM Light框架的新手,所以这是一个调整

我有一个带有网格的客户页面,该页面使用文本框和按钮进行搜索-文本框是绑定的,按钮使用命令。数据在视图模型中显示得很好。我检查了客户列表并设置了客户列表属性-所有操作都很好。问题是,页面不会刷新。当我转到另一个页面并返回到Customers页面时,将显示搜索到的数据

我怀疑视图模型是静态的,需要重新实例化

以下是各自的代码框架:

public partial class ViewModelLocator
{
    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else
        {
            SimpleIoc.Default.Register<IDataService, DataService>();
        }

        // Services
        SimpleIoc.Default.Register<INavigationService, NavigationService>();
        SimpleIoc.Default.Register<IMessenger, Messenger>();

        // View Models
        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<CustomersViewModel>();
        SimpleIoc.Default.Register<CustomerViewModel>(true);
        SimpleIoc.Default.Register<ContactsViewModel>();


    }

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public CustomersViewModel Customers
    {
        get
        {
            return ServiceLocator.Current.GetInstance<CustomersViewModel>();
        }
    }

    public CustomerViewModel Customer
    {
        get
        {
            return ServiceLocator.Current.GetInstance<CustomerViewModel>();
        }
    }

    public ContactsViewModel Contacts
    {
        get
        {
            return ServiceLocator.Current.GetInstance<ContactsViewModel>();
        }
    }


    public static void Cleanup()
    {
    }
}
}

公共类CustomerViewModel:ViewModelBase { 私有只读IDataService(数据服务); 私人导航服务; 私人伊梅森格信使

    public RelayCommand<string> RefreshClickCommand { get; set; }
    public RelayCommand<string> SearchCustomersCommand { get; set; }


    public const string CustomersPropertyName = "Customers";
    private ObservableCollection<Customer> _customers = null;
    public ObservableCollection<Customer> Customers
    {
        get
        {
            return _customers;
        }

        set
        {
            if (_customers == value)
            {
                return;
            }

            _customers = value;

            RaisePropertyChanging(CustomersPropertyName);
        }
    }

    public const string WelcomeTitlePropertyName = "WelcomeTitle";
    private string _welcomeTitle = string.Empty;

    public string WelcomeTitle
    {
        get
        {
            return _welcomeTitle;
        }

        set
        {
            if (_welcomeTitle == value)
            {
                return;
            }

            _welcomeTitle = value;
            RaisePropertyChanged(WelcomeTitlePropertyName);
        }
    }

    public const string CustomerSearchTermPropertyName = "CustomerSearchTerm";
    private string _customerSearchTerm = string.Empty;

    public string CustomerSearchTerm
    {
        get
        {
            return _customerSearchTerm;
        }

        set
        {
            if (_customerSearchTerm == value)
            {
                return;
            }

            _customerSearchTerm = value;

            RaisePropertyChanging(CustomerSearchTermPropertyName);
        }
    }

    public Customer SelectedItem
    {
        set
        {
            Customer customer = value;
            _messenger.Send<Customer>(customer, "Customer");
            _navigationService.Navigate(typeof(CustomerPage));
        }
    }

    public CustomersViewModel(IDataService dataService)
    {
        _navigationService = SimpleIoc.Default.GetInstance<INavigationService>();
        _messenger = SimpleIoc.Default.GetInstance<IMessenger>();
        _dataService = dataService;

        _dataService.GetData(
            (item, error) =>
            {
                if (error != null)
                {
                    // Report error here
                    return;
                }

                WelcomeTitle = item.Title + "Customers";
            });

        GetCustomers();

        InitializeCommands();
    }

    private void InitializeCommands()
    {
        RefreshClickCommand = new RelayCommand<string>((item) =>
        {
            GetCustomers();
        });

        SearchCustomersCommand = new RelayCommand<string>((item) =>
        {
            SearchCustomers();
        });
    }

    private void GetCustomers()
    {
        _customers = _dataService.GetCustomers();
    }

    private void SearchCustomers()
    {
        var cust =  _dataService.GetCustomers();
        List<Customer> customers = (from c in cust
                                    where c.CompanyName.StartsWith(_customerSearchTerm)
                                    orderby c.CompanyName
                                    select c).ToList();

        _customers = new ObservableCollection<Customer>(customers);
    }
}

如果您对此有任何指导,我们将不胜感激

谢谢

<common:LayoutAwarePage x:Class="SalesAccountManager.Views.RelationshipManager.CustomersPage"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:common="using:SalesAccountManager.Common"
                    xmlns:ignore="http://www.ignore.com"
                    xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid"
                    xmlns:WinRtBehaviors="using:WinRtBehaviors"
                    xmlns:Win8nl_Behavior="using:Win8nl.Behaviors"
                    mc:Ignorable="d ignore"
                    d:DesignHeight="768"
                    d:DesignWidth="1366"
                    DataContext="{Binding Customers, Source={StaticResource Locator}}">
<Grid>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                    <TextBlock Text="Customers" FontFamily="Segoe UI" FontSize="38"/>    
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0, 0, 100, 0">
                    <TextBox Height="20" Width="600" Background="White" Text="{Binding CustomerSearchTerm, Mode=TwoWay}" />
                    <Button Background="White" Command="{Binding SearchCustomersCommand}">
                        <Image Source="../../Images/Search.jpg" Height="20" Width="20"></Image>
                    </Button>
                </StackPanel>
            </Grid>