Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Textblock绑定在运行时不更新_C#_Mvvm_Binding_Uwp_Uwp Xaml - Fatal编程技术网

C# Textblock绑定在运行时不更新

C# Textblock绑定在运行时不更新,c#,mvvm,binding,uwp,uwp-xaml,C#,Mvvm,Binding,Uwp,Uwp Xaml,我是c#UWP开发的新手,我试图在运行时更改TextBlock的值,但绑定无法正常工作 我将XAML中TextBlock的text属性绑定到INotifyPropertyChanged的ViewModel上的一个属性,该值每10秒更改一次 我不知道这样做是否正确,有人能帮我吗 提前谢谢 尝试:Text=“{Binding MyValue,Mode=TwoWay}”在UWP中,与silver light和WPF不同,出于性能原因,默认绑定为一次。绑定只在应用程序启动时发生一次。单向绑定是Win

我是c#UWP开发的新手,我试图在运行时更改TextBlock的值,但绑定无法正常工作

我将XAML中TextBlock的text属性绑定到INotifyPropertyChanged的ViewModel上的一个属性,该值每10秒更改一次

我不知道这样做是否正确,有人能帮我吗

提前谢谢


尝试:
Text=“{Binding MyValue,Mode=TwoWay}”
在UWP中,与silver light和WPF不同,出于性能原因,默认绑定为一次。绑定只在应用程序启动时发生一次。单向绑定是WinRT、Silverlight和wpf的默认设置。这意味着将更新视图,但更新视图不会更新视图模型。双向绑定将同时更新视图和视图模型

因此,对于示例中的
,建议使用
单向绑定

中,建议对用户输入使用
双向
绑定

我发现了几个导致绑定失败的小错误。。。所以我改变了视图模型。。。私人财产被使用,而不是公共财产。由于代码正在更新线程中的值,然后尝试跨线程封送对象,因此添加了一个调度程序。还为所有视图模型添加了公共基类。这使得属性绑定更容易一些,它在重构属性名称时停止了绑定问题

Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync

public class MainPaigeViewModel: ViewModelBase

{


    public MainPaigeViewModel()
    {
        Task.Run(async () =>
        {
            Random random = new Random();
            while (true)
            {
                await Task.Delay(1000);
                int newValue = random.Next(-40, 40);
                try
                {
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () => {
                        MyValue = newValue.ToString();
                    });

                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
                Debug.WriteLine(MyValue);
            }
        });
    }

    //Properties
    private string _MyValue;
    public string MyValue
    {
        get { return _MyValue; }
        set
        {
            _MyValue = value;
            OnPropertyChanged();
        }
    }


}



public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };


    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
我还将视图更改为使用x:binding。我喜欢x:binding而不是旧的数据绑定,因为它在编译时而不是运行时显示绑定问题。除此之外,它还提供了性能增强功能

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <RelativePanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock Text="{x:Bind viewModel.MyValue, Mode=OneWay}" 
                       Width="100"
                       Height="40"
                       TextAlignment="Center"
                       FontSize="20"
            />
        </RelativePanel>
    </Grid>

您能在Raisepropertychange方法中设置断点并检查它是否正在触发吗?请在问题中提供您的代码,而不是作为外部链接。看看这里为什么给出一个命令很重要。@riteshmeher RaisePropertyChanged没有开火,可能是什么问题?谢谢你的回答@D.Nehl,但同样的问题对于文本块来说没有意义。
public class MainPaigeViewModel: ViewModelBase

{


    public MainPaigeViewModel()
    {
        Task.Run(async () =>
        {
            Random random = new Random();
            while (true)
            {
                await Task.Delay(1000);
                int newValue = random.Next(-40, 40);
                try
                {
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () => {
                        MyValue = newValue.ToString();
                    });

                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
                Debug.WriteLine(MyValue);
            }
        });
    }

    //Properties
    private string _MyValue;
    public string MyValue
    {
        get { return _MyValue; }
        set
        {
            _MyValue = value;
            OnPropertyChanged();
        }
    }


}



public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };


    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <RelativePanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock Text="{x:Bind viewModel.MyValue, Mode=OneWay}" 
                       Width="100"
                       Height="40"
                       TextAlignment="Center"
                       FontSize="20"
            />
        </RelativePanel>
    </Grid>
public sealed partial class MainPage : Page
{
    public MainPaigeViewModel viewModel;

    public MainPage()
    {
        this.InitializeComponent();

        viewModel = new MainPaigeViewModel();
    }


}