Usercontrol UI nu se schimba

0

Problema

UserControl

  <UserControl x:Class="NolowaFrontend.Views.MainViews.SearchView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:NolowaFrontend.Views.MainViews"
             xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
             xmlns:designTime="clr-namespace:NolowaFrontend.Views"
             mc:Ignorable="d" Name="_this"
             d:DesignHeight="300" d:DesignWidth="350" d:Background="White">   
    <Grid>
        <TextBlock Text="{Binding SearchedUsers.Count, diag:PresentationTraceSources.TraceLevel=High}" Foreground="Red" FontSize="20"/>
    </Grid>
</UserControl>

CodeBehind

public partial class SearchView : UserControl
{
    private readonly SearchVM _searchVM;

    public SearchView(User user)
    {
        InitializeComponent();

        _searchVM = new SearchVM(user); ;

        DataContext = _searchVM;
    }

    public void TimerSearch(string text)
    {
        _searchVM.TimerSearch(text);
    }
}

ViewModel

public class SearchVM : ViewModelBase
{
    private readonly User _user;
    private readonly ISearchService _searchService;

    private ObservableCollection<SearchedUser> _searchedUsers = new ObservableCollection<SearchedUser>();

    public ObservableCollection<SearchedUser> SearchedUsers
    {
        get { return _searchedUsers; }
        set { _searchedUsers = value; OnPropertyChanged(); }
    }

    public SearchVM(User user)
    {
        _user = user;
        _searchService = new SearchService();
    }

    public async void TimerSearch(string text)
    {
        var response = await _searchService.SearchUser(text);

        var data = response.ResponseData;

        SearchedUsers = data.ToObservableCollection(); 
    }
}

bindig log

System.Windows.Data Warning: 67 : BindingExpression (hash=11842506): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=11842506): Found data context element: TextBlock (hash=56511253) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=11842506): Activate with root item SearchVM (hash=52539597)
System.Windows.Data Warning: 108 : BindingExpression (hash=11842506):   At level 0 - for SearchVM.SearchedUsers found accessor RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 104 : BindingExpression (hash=11842506): Replace item at level 0 with SearchVM (hash=52539597), using accessor RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 0 from SearchVM (hash=52539597) using RuntimePropertyInfo(SearchedUsers): ObservableCollection`1 (hash=49313939 Count=0)
System.Windows.Data Warning: 108 : BindingExpression (hash=11842506):   At level 1 - for ObservableCollection`1.Count found accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 104 : BindingExpression (hash=11842506): Replace item at level 1 with ObservableCollection`1 (hash=49313939 Count=0), using accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 1 from ObservableCollection`1 (hash=49313939 Count=0) using RuntimePropertyInfo(Count): '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=11842506): TransferValue - got raw value '0'
System.Windows.Data Warning: 84 : BindingExpression (hash=11842506): TransferValue - implicit converter produced '0'
System.Windows.Data Warning: 89 : BindingExpression (hash=11842506): TransferValue - using final value '0'
System.Windows.Data Warning: 56 : Created BindingExpression (hash=29516363) for Binding (hash=55475379) BindingExpression:Path=SearchedUsers.Count; DataItem=null; 
System.Windows.Data Warning: 58 :  Path: 'SearchedUsers.Count'
System.Windows.Data Warning: 60 : BindingExpression (hash=29516363): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=29516363): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=29516363): Attach to System.Windows.Controls.TextBlock.Text (hash=35990092)
System.Windows.Data Warning: 67 : BindingExpression (hash=29516363): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=29516363): Found data context element: TextBlock (hash=35990092) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=29516363): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=29516363): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=29516363): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=29516363): Found data context element: TextBlock (hash=35990092) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=29516363): Activate with root item SearchVM (hash=64320678)
System.Windows.Data Warning: 107 : BindingExpression (hash=29516363):   At level 0 using cached accessor for SearchVM.SearchedUsers: RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 104 : BindingExpression (hash=29516363): Replace item at level 0 with SearchVM (hash=64320678), using accessor RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 101 : BindingExpression (hash=29516363): GetValue at level 0 from SearchVM (hash=64320678) using RuntimePropertyInfo(SearchedUsers): ObservableCollection`1 (hash=42592467 Count=0)
System.Windows.Data Warning: 107 : BindingExpression (hash=29516363):   At level 1 using cached accessor for ObservableCollection`1.Count: RuntimePropertyInfo(Count)
System.Windows.Data Warning: 104 : BindingExpression (hash=29516363): Replace item at level 1 with ObservableCollection`1 (hash=42592467 Count=0), using accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 101 : BindingExpression (hash=29516363): GetValue at level 1 from ObservableCollection`1 (hash=42592467 Count=0) using RuntimePropertyInfo(Count): '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=29516363): TransferValue - got raw value '0'
System.Windows.Data Warning: 84 : BindingExpression (hash=29516363): TransferValue - implicit converter produced '0'
System.Windows.Data Warning: 89 : BindingExpression (hash=29516363): TransferValue - using final value '0'
System.Windows.Data Warning: 95 : BindingExpression (hash=11842506): Got PropertyChanged event from SearchVM (hash=52539597)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 0 from SearchVM (hash=52539597) using RuntimePropertyInfo(SearchedUsers): ObservableCollection`1 (hash=53428882 Count=2)
System.Windows.Data Warning: 105 : BindingExpression (hash=11842506):   Item at level 1 has same type - reuse accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 104 : BindingExpression (hash=11842506): Replace item at level 1 with ObservableCollection`1 (hash=53428882 Count=2), using accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 1 from ObservableCollection`1 (hash=53428882 Count=2) using RuntimePropertyInfo(Count): '2'
System.Windows.Data Warning: 80 : BindingExpression (hash=11842506): TransferValue - got raw value '2'
System.Windows.Data Warning: 84 : BindingExpression (hash=11842506): TransferValue - implicit converter produced '2'
System.Windows.Data Warning: 89 : BindingExpression (hash=11842506): TransferValue - using final value '2'

Salut! Atunci când, dacă există o funcție care solicită SearchView.TimerSearch("x")

Cred UserControl arată numărul care este returnat de SearchView.TimerSearch("x")

Dar nu e cum mă așteptam.

obligatoriu jurnal spune că s-a schimbat la 2. dar se arată " 0 " și niciodată nu s-a schimbat!

Ma poate ajuta cineva?

data-binding mvvm wpf
2021-11-22 16:05:54
1

Cel mai bun răspuns

1

Vă sunt obligatorii pentru imbricate Count proprietate de colectare. Dar, din moment ce sunt înlocuirea completă colecție, Count proprietate efectiv nu se schimbă și colectarea exemplu, schimbarea nu este detectat de către Binding. Cu alte cuvinte, Binding este de a asculta pentru o Count s-a schimbat de eveniment, dar acest eveniment nu este ridicat, deoarece colectarea exemplu sine s-a schimbat în loc.
În general, este recomandat să utilizați întotdeauna aceeași colecție exemplu, într-o legare de date de context: clar colectarea și adăugați noua gama de produse.

Următorul este metoda asincron TimerSearch: în prezent, nu este așteptat și nu se întoarce o Task!
Amintiți-vă: async void este doar permis pentru stivuitoare eveniment. În caz contrar, o void metoda trebuie să fie întotdeauna convertite pentru a reveni Task.

Atunci când await o metodă trebuie să await complet de apel copac (toate apelanții prea): async Task peste tot.

// Make method return Task
public async Task TimerSearchAsync(string text)
{
    var response = await _searchService.SearchUser(text);

    SearchedUsers.Clear();
    foreach (var data in response.ResponseData)
    {
       SearchedUsers.Add(data);
    }
}

În cazul în care response.ResponseData returnează un List<T>, puteți înlocui foreach bucla cu un apel de List<T>.ForEach metoda de compact cod:

response.ResponseData.ForEach(SearchedUsers.Add);

Apoi, converti metoda de asteptare pentru a reveni un Task de asemenea și await la TimerSearch apel de metodă:

// Declare method async and return a Task
public async Task TimerSearcAsynch(string text)
{
    await _searchVM.TimerSearchAsync(text);
}

În cele din urmă, pentru a evita setarea DataContext în controlul tău. Acest lucru limitează gradul de utilizare a controlului să fie folosit numai în context special.

În general, nu au o vedere modelul de clasă pentru fiecare control, acest lucru nu este modul de control de design. Controalele trebuie să fie dezvoltat independent din punct de vedere modelul de clasă.
În cazul tău, de control ar avea un UserCount dependența de proprietate, care mai târziu poate lega la o sursă externă de vedere modelul. Pe plan intern, TextBox s-ar lega la UserCount dependența de proprietate.

2021-11-22 17:27:32

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................