Usercontrol ส่วนติดต่อผู้ใช้ไม่ได้เปลี่ยนแปลง

0

คำถาม

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 ปูมบันทึก

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'

สวัสดี! ตอนที่ถ้ามันมีฟังก์ชันที่เรีย SearchView.TimerSearch("x")

ฉันคิดว่า UserControl แสดงจำนวนนั่นคือกลับมาจาก SearchView.TimerSearch("x")

แต่มันไม่ทำงานยังไงฉันคาดหวังไว้

ถูกเชื่อมโยงปูมบันทึกบอกว่ามันเปลี่ยนแปลงเป็น 2 ต่อได้. แต่มันแสดงค่าเป็น 0 และไม่เคยเปลี่ยนไป!

ใครก็ได้ช่วยฉัน?

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

คำตอบที่ดีที่สุด

1

คุณถูกเชื่อมโยงเพื่อครอบการ Count ทรัพย์สินของคนจะได้ปลอดภัย แต่ตั้งแต่คุณเป็นและเข้าแทนที่สมบูรณ์ชุดสะสมที่ Count ทรัพย์สินรถไม่ได้เปลี่ยนเป็นคลังภาพตัวอย่างการเปลี่ยนแปลงเป็นสิ่งไม่ใช่ตรวจสอบพบโดย Binding. หรือจะพูดอีกอย่าง Binding คือฟังอยู่สำหรับ Count เปลี่ยนเหตุการณ์แต่เหตุการณ์นี้ไม่เคยเลี้ยงเพราะคลังภาพตัวอย่างตัวเองเปลี่ยนแปลงไปแทน
มันคือโดยทั่วไปขอแนะนำให้ให้มันคงใช้คนเดียวกับคลังภาพตัวอย่างอยู่ในข้อมูลถูกเชื่อมโยงคอนเท็กซ์:ลีคลังสื่อและเพิ่มใหม่ของระยะของรายการ.

ต่อไปคือของคุณ async วิธีการ TimerSearchมันคือปัจจุบันที่ไม่ใช่ awaited และไม่กลับมาเป็น Task!
จำได้ไหม: async void เป็น เดียวที่ ได้รับอนุญาตสำหรับเหตุการณ์ว่าประเทศไหน ไม่อย่างนั้นเป็น void วิธีการต้องแปลงที่จะกลับมา Task.

ตอนที่คุณ await เป็นวิธีการคุณต้อง await ที่สมบูรณ์เรียกต้นไม้(ทั้งหมด callers เหมือนกั): async Task อยู่ทุกที่เลย

// 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);
    }
}

ในกรณีนี้ response.ResponseData จะได้ค่าเป็น List<T>คุณสามารถแทนที่ foreach วกับการโทรของ List<T>.ForEach วิธีการที่จะทำโฟลเดอร์ให้กะทัดรังรหัส:

response.ResponseData.ForEach(SearchedUsers.Add);

ต่อไปแปลงที่เรียกวิธีการที่จะกลับมาเป็น Task เหมือนกัน await คน TimerSearch วิธีการเรียก:

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

ในที่สุดก็หลีกเลี่ยงการตั้งค่า DataContext ในการควบคุม นี่ขีดจำกัดที่ simon edwards ของควบคุมที่จะเป็นเพียงเคยอยู่ในพิเศษคอนเท็กซ์.

โดยปกติแล้วคุณไม่มีมุมมองนางแบบชั้นเรียนสำหรับแต่ละความควบคุมนี่มันไม่ใช่วิธีที่ลูกออกแบบควบคุม ควบคุมต้องพัฒนาเป็นอิสระจากมุมมองนางแบบชั้นเรียน
ในกรณีของคุณที่ควบคุมจะมี UserCount การขึ้นแก่กันทรัพย์สินซึ่งคุณทีหลังสามารถให้พวกที่จะเป็นเว็บเบราว์เซอร์ภายนอกมุมมองคุณครูแต่พอกลับมา ภายในร่างกายที่ TextBox จะให้พวไป UserCount การขึ้นแก่กันริมทรัพย์

2021-11-22 17:27:32

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่