vb.net – 与设计时数据的MVVM绑定在设计时正常工作,但在运行时不能正常工作

vb.net – 与设计时数据的MVVM绑定在设计时正常工作,但在运行时不能正常工作,第1张

概述我有一个简单的MVVM项目,其中包含设计时数据,可以使用主数据和带有每个子项的用户控件的列表框. 但是,当我在运行时实例化类时,主数据显示但子数据不显示(但列表框具有正确的项目数,但它们没有在文本框中显示任何数据). 我注意到Sport上的构造函数被调用了很多次,与我期望的相比(例如在运行时,我希望它只被调用两次,但它似乎被称为更多). 我有班级人员和体育.每个人都喜欢多种运动,并有一个最喜欢的球 我有一个简单的MVVM项目,其中包含设计时数据,可以使用主数据和带有每个子项的用户控件的列表框.
但是,当我在运行时实例化类时,主数据显示但子数据不显示(但列表框具有正确的项目数,但它们没有在文本框中显示任何数据).

我注意到Sport上的构造函数被调用了很多次,与我期望的相比(例如在运行时,我希望它只被调用两次,但它似乎被称为更多).

我有班级人员和体育.每个人都喜欢多种运动,并有一个最喜欢的球队.
我有一个personviewmodel& sportviewmodel,它继承自viewmodelBase.

这是我的VB.net代码

imports System.Collections.ObjectModelPublic Class Person    Public Property Forename As String    Public Property Surname As String    Public Sports As New ObservableCollection(Of Sport)End ClassPublic Class Sport    Public Sub New()        DeBUG.Writeline("test")    End Sub    Public Property Sportname As String    Public Property FavouriteProfessionalTeam As StringEnd Classimports System.Collections.ObjectModelnamespace viewmodel    Public Class Personviewmodel        inherits viewmodel.viewmodelBase        Public Property Person1 As Person        Public Property Sportviewmodels As New ObservableCollection(Of Sportviewmodel)        Public Sub New()            LoadData()        End Sub        ''' <summary>         ''' Loads the data for the application.         ''' </summary>         Private Sub LoadData()            If IsInDesignmodeStatic Then                LoadDesignData()            Else            End If        End Sub        ''' <summary>         ''' Loads temporary data for use in the designer.         ''' </summary>         Private Sub LoadDesignData()            Person1 = New Person            Person1.Forename = "Mickey Run Time"            Person1.Surname = "Mouse Run Time"            Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Man Utd",.Sportname = "Soccer"})            Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "barcelona",.Sportname = "Spanish Soccer"})            Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "ulster",.Sportname = "Rugby"})            For Each sport1 In Person1.Sports                Dim sportVm As New Sportviewmodel With {.Sport1 = sport1}                Sportviewmodels.Add(sportVm)            Next        End Sub    End ClassEnd namespacenamespace viewmodel    Public Class Sportviewmodel        inherits viewmodel.viewmodelBase        Public Property Sport1 As New Sport        Public Property Person1 As Person        Public Sub New()            LoadData()        End Sub        ''' <summary>         ''' Loads the data for the application.         ''' </summary>         Private Sub LoadData()            If IsInDesignmodeStatic Then                LoadDesignData()            Else                ' DeBUG.Writeline(Sport1.Sportname)                ' Load the student data asynchronously                 'StudentContextInstance = New StudentContext                'Dim loadop =                '  StudentContextInstance.Load(StudentContextInstance.                '             GetStudentsquery(),'             AddressOf OnStudentsLoaded,nothing)            End If        End Sub        ''' <summary>         ''' Loads temporary data for use in the designer.         ''' </summary>         Private Sub LoadDesignData()            Sport1 = New Sport With {.Sportname = "Design Time name",.FavouriteProfessionalTeam = "Design Time Team"}        End Sub    End ClassEnd namespaceimports System.ComponentModelnamespace viewmodel    Public Class viewmodelBase        Implements INotifyPropertyChanged        Public Event PropertyChanged(sender As Object,e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged        Private Shared _isInDesignmode As Boolean?        Public Shared Readonly Property IsInDesignmodeStatic As Boolean            Get                If Not _IsInDesignmode.HasValue Then                    _IsInDesignmode = DesignerPropertIEs.GetIsInDesignmode(New DependencyObject)                End If                Return _IsInDesignmode.Value            End Get        End Property        Protected Sub OnPropertyChanged(ByVal propertyname As String)            ' Send an event notification that the property changed             ' This allows the UI to kNow when one of the items changes             If Not String.IsNullOrEmpty(propertyname) Then                RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(propertyname))            End If        End Sub    End ClassEnd namespace

这是我的wain窗口背后的代码,它设置了用于显示的MVVM
    imports System.Collections.ObjectModel
    导入WpfApplicationMVVMTest.viewmodel

Class MainWindow    Public Property Person1 As Person    Public Property Sportviewmodels As New ObservableCollection(Of Sportviewmodel)    Private Sub button_Click_1(sender As Object,e As RoutedEventArgs)        Dim wndPerson As New PersonWindow        Person1 = New Person        Person1.Forename = "Donald"        Person1.Surname = "Duck"        Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Man Utd",.Sportname = "Soccer"})        Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "barcelona",.Sportname = "Spanish Soccer"})        For Each sport1 In Person1.Sports            Dim sportVm As New Sportviewmodel With {.Sport1 = sport1}            Sportviewmodels.Add(sportVm)        Next        Dim vm As New viewmodel.Personviewmodel        vm.Sportviewmodels = Sportviewmodels        vm.Person1 = Person1        wndPerson.DataContext = vm        wndPerson.Show()    End SubEnd Class

这是XAML代码

<Window    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:viewmodel="clr-namespace:WpfApplicationMVVMTest.viewmodel"    xmlns:VIEw="clr-namespace:WpfApplicationMVVMTest" mc:Ignorable="d" x:Class="PersonWindow"    title="PersonWindow" Height="1114.8" WIDth="542">    <Window.Resources></Window.Resources>    <Window.DataContext>        <viewmodel:Personviewmodel />    </Window.DataContext>    <GrID>        <GrID x:name="GrID1" HorizontalAlignment="left" margin="41,39,0" VerticalAlignment="top">            <GrID.ColumnDeFinitions>                <ColumnDeFinition WIDth="auto"/>                <ColumnDeFinition WIDth="auto"/>            </GrID.ColumnDeFinitions>            <GrID.RowDeFinitions>                <RowDeFinition Height="auto"/>                <RowDeFinition Height="auto"/>            </GrID.RowDeFinitions>            <Label Content="Forename:" GrID.Column="0" HorizontalAlignment="left" margin="3" GrID.Row="0" VerticalAlignment="Center"/>            <TextBox x:name="ForenameTextBox" GrID.Column="1" HorizontalAlignment="left" Height="23" margin="3" GrID.Row="0" Text="{Binding Person1.Forename,Mode=TwoWay,NotifyOnValIDationError=true,ValIDatesOnExceptions=true}" VerticalAlignment="Center" WIDth="120"/>            <Label Content="Surname:" GrID.Column="0" HorizontalAlignment="left" margin="3" GrID.Row="1" VerticalAlignment="Center"/>            <TextBox x:name="SurnameTextBox" GrID.Column="1" HorizontalAlignment="left" Height="23" margin="3" GrID.Row="1" Text="{Binding Person1.Surname,ValIDatesOnExceptions=true}" VerticalAlignment="Center" WIDth="120"/>        </GrID>        <ListBox margin="41,108,59,753" ItemsSource="{Binding Path=Sportviewmodels}"  >            <ListBox.ItemTemplate>                <DataTemplate>                    <VIEw:SportUserControl DataContext="{Binding}"  margin="5"/>                </DataTemplate>            </ListBox.ItemTemplate>        </ListBox>    </GrID></Window><UserControl             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:viewmodel="clr-namespace:WpfApplicationMVVMTest.viewmodel" x:Class="SportUserControl"              mc:Ignorable="d"              d:DesignHeight="600" d:DesignWIDth="600">    <UserControl.Resources></UserControl.Resources>    <UserControl.DataContext>        <viewmodel:Sportviewmodel />    </UserControl.DataContext>    <GrID>        <GrID x:name="GrID1" HorizontalAlignment="left" margin="31,26,0" VerticalAlignment="top">            <GrID.ColumnDeFinitions>                <ColumnDeFinition WIDth="auto"/>                <ColumnDeFinition WIDth="auto"/>            </GrID.ColumnDeFinitions>            <GrID.RowDeFinitions>                <RowDeFinition Height="auto"/>                <RowDeFinition Height="auto"/>            </GrID.RowDeFinitions>            <Label Content="Favourite Professional Team:" GrID.Column="0" HorizontalAlignment="left" margin="3" GrID.Row="0" VerticalAlignment="Center"/>            <TextBox x:name="FavouriteProfessionalTeamTextBox" GrID.Column="1" HorizontalAlignment="left" Height="23" margin="3" GrID.Row="0" Text="{Binding Sport1.FavouriteProfessionalTeam,ValIDatesOnExceptions=true}" VerticalAlignment="Center" WIDth="120"/>            <Label Content="Sport name:" GrID.Column="0" HorizontalAlignment="left" margin="3" GrID.Row="1" VerticalAlignment="Center"/>            <TextBox x:name="SportnameTextBox" GrID.Column="1" HorizontalAlignment="left" Height="23" margin="3" GrID.Row="1" Text="{Binding Sport1.Sportname,ValIDatesOnExceptions=true}" VerticalAlignment="Center" WIDth="120"/>        </GrID>    </GrID></UserControl>

这是我的设计时间图像显示3运动(根据我的负载设计时间数据).

这是我的运行时窗口,显示2个运动(虽然他们没有数据 – 这是问题).

@H_502_40@解决方法 您在标记和代码中设置Datacontexts …删除标记并坚持一种模式 总结

以上是内存溢出为你收集整理的vb.net – 与设计时数据的MVVM绑定在设计时正常工作,但在运行时不能正常工作全部内容,希望文章能够帮你解决vb.net – 与设计时数据的MVVM绑定在设计时正常工作,但在运行时不能正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1222270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存