2017-11-10 6 views
0

프리즘 WPF를 처음 사용하고 응용 프로그램을 구성하는 방법 및 방법을 처음으로 이해했습니다.OnNavigatedTo 메서드를 사용하여 Combobox 바인딩 빈입니다.

'OnNavigatedTo'메서드를 사용하여보기의 컨트롤에 데이터 바인딩에 문제가 있습니다.

접근 한

내가 고객을 채우기 위해 저장소를 호출 할 때 그러나,보기에서 콤보 상자가 비어의 'OnNavigatedTo'메소드가 생성자 후라고 이해합니다.

뷰 모델 :

public class ViewAViewModel : BindableBase, INavigationAware 
{ 
    private readonly IRegionManager _regionManager; 
    private readonly IRepository _repository; 

    public List<Customer> Customers { get; set; } 

    public ViewAViewModel(IRepository repository, IRegionManager regionManager) 
    { 
     _repository = repository; 
     _regionManager = regionManager; 
     Customers = new List<Customer>(); 
    } 

    private string _selectedCustomer; 
    public string SelectedCustomer 
    { 
     get { return _selectedCustomer; } 
     set { SetProperty(ref _selectedCustomer, value); } 
    } 

    public void OnNavigatedTo(NavigationContext navigationContext) 
    { 
     Customers = _repository.GetCustomers(); 
    } 

    public bool IsNavigationTarget(NavigationContext navigationContext) 
    { 
     return true; 
    } 

    public void OnNavigatedFrom(NavigationContext navigationContext) 
    { 

    } 
} 

보기 : 내가 생성자를 통해/채우기 '고객'초기화하면

<UserControl x:Class="ModuleA.Views.ViewA" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:prism="http://prismlibrary.com/"    
      prism:ViewModelLocator.AutoWireViewModel="True"> 
    <Grid> 
     <StackPanel> 
      <ComboBox ItemsSource="{Binding Customers}" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValue="{Binding SelectedCustomer}"></ComboBox> 
     </StackPanel>  
    </Grid> 
</UserControl> 

, 콤보 모듈은 모듈에 추가 될 때,하지만, 작품이 잘 결합 부트 스트 래퍼의 '고객'저장소 메소드가 불필요하게 호출됩니다. 나는 이상하다고 생각하지 않습니다. 내가 사용하는 경우 콤보가 잘 작동에

접근법 2

은 '고객', 다음 데이터 바인딩에 'RaisePropertyChanged'.

뷰 모델 :

public class ViewAViewModel : BindableBase, INavigationAware 
{ 
    private readonly IRegionManager _regionManager; 
    private readonly IRepository _repository; 
    private List<Customer> _customers; 

    public List<Customer> Customers 
    { 
     get { return _customers; } 
     set 
     { 
      _customers = value; 
      RaisePropertyChanged(); 
     } 
    } 

    public ViewAViewModel(IRepository repository, IRegionManager regionManager) 
    { 
     _repository = repository; 
     _regionManager = regionManager; 
     _customers = new List<Customer>(); 
    } 

    private string _selectedCustomer; 
    public string SelectedCustomer 
    { 
     get { return _selectedCustomer; } 
     set { SetProperty(ref _selectedCustomer, value); } 
    } 

    public void OnNavigatedTo(NavigationContext navigationContext) 
    { 
     Customers = _repository.GetCustomers(); 
    } 

    public bool IsNavigationTarget(NavigationContext navigationContext) 
    { 
     return true; 
    } 

    public void OnNavigatedFrom(NavigationContext navigationContext) 
    { 

    } 
} 

은 접근이 취할 수있는 올바른 방법인가? 또는 나는 뭔가를 놓친다.

미리 감사드립니다.

+0

두 번째 방법이 정확합니다. 'Customers' 프라퍼티는 바인딩 소스이기 때문에, 그것의 타겟을 업데이트해야한다는 바인딩에 알리기 위해'PropertyChanged' 이벤트를 발생시켜야합니다. 이것은 실제로 일반적인 데이터 바인딩 주제이며 프리즘이나 탐색 기능과 관련이 없습니다. – dymanoid

+0

감사합니다. 분류가 잘못되어 사과드립니다. 그것의 나의 첫번째 질문. –

답변

0

RaisePropertyChanged를 호출하지 않기 때문에 첫 번째 방법이 잘못되었습니다. 생성자에서 새 빈 목록을 Customers 속성에 할당하여 콤보 상자가 비어있게 시작합니다.

두 번째로 OnNavigatedTo 메서드에서 Populated 목록을 Customers 속성에 할당하지만 Customers 속성은 RaisePropertyChanged를 호출하지 않으므로보기가 변경 내용을 인식하지 못합니다.

일반적으로 올바른 접근 방식 인 두 번째 접근 방식과 똑같이 수정할 수 있습니다.

다른 솔루션은 OnNavigatedTo에 채우는 대신 생성자에 목록을 채우는 것입니다.이 경우 Customers 속성은 RaisePropertyChanged를 호출 할 필요가 없습니다.보기가 업데이트 된 요소로 이미 구성되어 있기 때문입니다.

보기에있을 때 Customers 특성이 더 이상 변경되지 않는 경우에만 의미가 있습니다.

+0

세 번째 접근 방식이 있다는 것을 주목할 필요가 있습니다 :'List '대신'ObservableCollection '을 사용할 수 있습니다.그것은 생성자에서 생성 될 수 있으며 나중에 데이터로 채워질 수 있습니다. 'CollectionChanged' 이벤트를 듣고 있기 때문에 뷰가 업데이트됩니다. – dymanoid

+0

답장을 보내 주셔서 감사합니다. 위의 시나리오에서'고객 '은'일방적 인 '바인딩 소스, 즉보기에서 변경되지 않습니다. 애플리케이션 성능 관점에서, 생성자를 통해'Customers' 데이터를로드하는 것이 이상적이지 않습니까? 위의 질문에서 언급했듯이 모듈이 모듈 카탈로그에로드되면 해당 모듈의 뷰 모델에있는 생성자가 호출되고 (데이터로드 등) 해당 뷰를 탐색 할 때 생성자가 다시 호출됩니다. 어쩌면 바인딩 작업 및/또는 프리즘에 대한 오해가 있습니다. –