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

时间:2013-06-15 17:43:51

标签: mvvm-light

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

我有一个带有网格的客户页面,该网格使用文本框和按钮进行搜索 - 文本框已绑定,按钮使用命令。数据显示在视图模型中就好了。我在客户列表上LINQ并设置客户列表属性 - 一切正常。问题是,页面不刷新。当我转到另一个页面并返回“客户”页面时,将显示搜索到的数据。

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

以下是各自的代码框架:

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()
    {
    }
}

}


公共类CustomersViewModel:ViewModelBase     {         private readonly IDataService _dataService;         private INavigationService _navigationService;         私人IMessenger _messenger;

    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>

任何关于此的指导都将受到赞赏......

谢谢!

0 个答案:

没有答案