UWP MVVM虚拟机数据绑定(文本框).text from String)

本文关键字:text from String 文本 MVVM 虚拟机 数据绑定 UWP | 更新日期: 2023-09-27 18:02:35

嗯,用UWP模板10尝试MVVM。我已经读了很多页了,尽管每个人都试图说它很容易,但我仍然做不到。

把它放到上下文中,OCR正在对图像运行,我希望文本自动显示在文本框中。

这是我的模型:

public class TextProcessing
{
    private string _ocrText;
    public string OcrText
    {
        get { return _ocrText; }
        set
        {
            _ocrText = value;
        }
    }
}
这是我的ViewModel:
public class ScanPageViewModel : ViewModelBase, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private TextProcessing _ocrTextVM;
    public ScanPageViewModel()
    {
        _ocrTextVM = new TextProcessing();
    }
    public TextProcessing OcrTextVM
    {
        get { return _ocrTextVM; }
        set {
            _ocrTextVM = value;
            this.OnPropertyChanged("OcrTextVM");
            }
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

这是我的视图:

<TextBox x:Name="rtbOcr" 
       Text="{Binding OcrTextVM.OcrText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

首先,这是行不通的。有人能试着告诉我哪里错了吗?

那么,数据来自于Services文件,Services如何更新值呢?正确的代码是什么?

UWP MVVM虚拟机数据绑定(文本框).text from String)

以下代码摘自代码。msdn(如何在UWP中实现MVVM设计模式),对你会有帮助:

一步一步检查你的代码

1。ViewModel实现接口INotifyPropertyChanged,并在属性设置方法中调用PropertyChanged,如下所示:

public sealed class MainPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _productName;
    public string ProductName
    {
        get { return _productName; }
        set
        {
            _productName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ProductName)));
            }
        }
    }
}

2。在页面中初始化ViewMode,并将DataContext设置为ViewMode,如下所示:

public sealed partial class MainPage : Page
{
    public MainPageViewModel ViewModel { get; set; } = new MainPageViewModel();
    public MainPage()
    {
        ...
        this.DataContext = ViewModel;
    }
}

3。在xaml中,从viewMode绑定数据,如下所示:

<TextBox Text="{Binding Path=ProductName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="ProductNameTextBox" TextChanged="ProductNameTextBox_TextChanged" />

您的OnPropertyChanged对OcrTextVM的调用实际上并没有在您的情况下调用,因为您将构造函数中的值设置为其支持字段并绕过该属性。

如果你通过属性设置值,它应该工作:

public ScanPageViewModel()
{
    OcrTextVM = new TextProcessing();
}

当然你的视图需要知道ScanPageViewModel是它的DataContext。最简单的方法是在视图背后代码的构造函数中:

public OcrView()
{
    DataContext = new ScanPageViewModel();
    InitializeComponent();
}

假设你的OCR服务在使用时返回一个新的TextProcessing对象,设置OcrTextVM的属性应该足够了:

public class ScanPageViewModel : ViewModelBase, INotifyPropertyChanged
{
    //...       
    private void GetOcrFromService()
    {
        //...
        TextProcessing value = OcrService.Get();
        OcrTextVM = value;  
    }
}

值得注意的是,OcrTextVM名称并不能真正反映属性正在做什么,因为它看起来不像一个视图模型。

实际上,一旦我设法理解它就很容易了。下面是更新TextBox所需的代码。文本

在模型中:

public class DisplayText : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _text;
    public string Text
    {
        get { return _text; }
        set
        {
            _text = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Text)));
        }
    }
}

在XAML文件中:

<TextBox Text="{Binding Helper.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ... />

在ViewModels中:

    private DisplayText _helper = new DisplayText();
    public DisplayText Helper
    {
        get { return _helper; }
        set
        {
            _helper = value;
        }
    }

然后ViewModels中的任何mod:

Helper.Text = "Whatever text, or method returning a string";