1

我正在使用最新的 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 {私有只读IDataService _dataService;私人 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>

对此的任何指导将不胜感激......

谢谢!

4

0 回答 0