C# MVVM属性不更新

C# MVVM属性不更新,c#,wpf,mvvm,C#,Wpf,Mvvm,我在财产更新方面有奇怪的问题 public class BalanceViewModel : ViewModelBase { private BalanceTest some; public BalanceTest Some { get { return some; } set { some = value; RaisePropertyChanged("Some"); } } private CentralModel CM;

我在财产更新方面有奇怪的问题

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
我的模型看起来像

using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using TestSome.DataType;
using TestSome.MessageInfrastructure;
using TestSome.WorkingWithNode;

namespace TestSome.Model
{
    class CentralModel
    {
        private BalanceTest someTest;
        public BalanceTest SomeTest
        {
            get { return someTest; }
            set { someTest = value; }
        }
        public void ListenBalance()
        {
            SocketHandler.Socket.Handle.Add("balance", (m) =>
            {
                try
                {
                    BalanceTest bt = m.Message.Json.GetFirstArgAs<BalanceTest>();
                    SomeTest = bt;
                    Messenger.Default.Send(new BalanceCommunicator { TestBalance = bt }, "Token");
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message.ToString());
                }
            });
        }
    }
}
public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
<UserControl x:Class="TestSome.Views.BalanceView"
         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:Test="clr-namespace:TestSome.Model"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Test:CentralModel x:Key="CentralMode"></Test:CentralModel>
</UserControl.Resources>
<Expander ExpandDirection="Right">
<StackPanel Orientation="Horizontal">

        <Label Content="{Binding SomeTest.Balance,Source={StaticResource CentralMode},Mode=TwoWay}"></Label>
</StackPanel>
</Expander>
Xaml看起来像

using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using TestSome.DataType;
using TestSome.MessageInfrastructure;
using TestSome.WorkingWithNode;

namespace TestSome.Model
{
    class CentralModel
    {
        private BalanceTest someTest;
        public BalanceTest SomeTest
        {
            get { return someTest; }
            set { someTest = value; }
        }
        public void ListenBalance()
        {
            SocketHandler.Socket.Handle.Add("balance", (m) =>
            {
                try
                {
                    BalanceTest bt = m.Message.Json.GetFirstArgAs<BalanceTest>();
                    SomeTest = bt;
                    Messenger.Default.Send(new BalanceCommunicator { TestBalance = bt }, "Token");
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message.ToString());
                }
            });
        }
    }
}
public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
<UserControl x:Class="TestSome.Views.BalanceView"
         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:Test="clr-namespace:TestSome.Model"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Test:CentralModel x:Key="CentralMode"></Test:CentralModel>
</UserControl.Resources>
<Expander ExpandDirection="Right">
<StackPanel Orientation="Horizontal">

        <Label Content="{Binding SomeTest.Balance,Source={StaticResource CentralMode},Mode=TwoWay}"></Label>
</StackPanel>
</Expander>

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

为什么不更新?

您没有在SomeTest属性上提升
RaisePropertyChanged(“SomeTest”)
。它为什么会起作用?:)

您需要绑定到BalanceView模型的
部分
,而不是中心模型

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
<Label Content="{Binding Path=Some.Balance,Mode=TwoWay}"></Label>

我建议您在
CentralModel
类中实现一个
事件
,在
BalanceViewModel
中实现一个处理程序,以便向上传输新数据

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
在CentralModel类中:

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
/// <summary>
/// Raised when a new balance is received.
/// </summary>
public event EventHandler<EventArgs> NewBalanceEvent;

public void ListenBalance()
{
    SocketHandler.Socket.Handle.Add("balance", (m) =>
    {
        try
        {
            BalanceTest bt = m.Message.Json.GetFirstArgAs<BalanceTest>();
            SomeTest = bt;
            Messenger.Default.Send(new BalanceCommunicator { TestBalance = bt }, "Token");

            // Raise the event
            EventHandler<EventArgs> RaiseNewBalanceEvent = NewBalanceEvent;
            if (null != RaiseNewBalanceEvent)
            {
                RaiseNewBalanceEvent(this, EventArgs.Empty);
            }
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message.ToString());
        }
    });
}

现在,每当调用
SocketHandler.Socket.Handle.Add
中的Lambda时,
CentralModel
将引发
NewBalanceEvent
,该事件反过来(通过框架)调用
BalanceViewModel
的事件处理程序委托,并更新
Some
属性

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
编辑

public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

此外,提供的是正确的:您需要绑定到视图中的正确属性。

是否初始化了DataContext?public BalanceView(){InitializeComponent();DataContext=new BalanceViewModel();}请不要随意将文本的每一位都斜体化。没用。您似乎没有绑定到ViewModel的
某些属性。另外,
Some
属性仅在
BalanceViewModel
构造函数期间设置。您的代码未显示值的更改位置。SocketHandler.Socket.Handle.Add get的新值。
SomeTest
是模型,我认为它没有帮助。我实现了NotifyPropertyChanged(“SomeTest”);我喜欢做的是将绑定的每个部分添加为textblock的绑定,以查看绑定在哪一点出错。否则,还可以将调试器配置为在output1中显示绑定错误。它是否正确显示起始值?2.你永远不会改变这个值。3.你是否设置了断点来检查发生了什么?我认为你是对的,它只在初始化时被提升。您能否建议如何每次更新此属性?使用定时器还是。。。?谢谢1.正确显示启动值2。它正在更改,但ui不会更新。3/是
public class BalanceViewModel : ViewModelBase
{
    private BalanceTest some;
    public BalanceTest Some
    {
        get { return some; }
        set { some = value; RaisePropertyChanged("Some"); }
    }
    private CentralModel CM;
    public BalanceViewModel()
        {
            try
            {
                CM = new CentralModel();
                CM.ListenBalance();
                Some = CM.SomeTest;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }