好吧,在MVVM中使用UWP模板10.我已經閱讀了很多頁面,儘管每個人都試圖說它非常簡單,但我仍然無法制作這行得通。用於假人的UWP MVVM數據綁定(字符串textbox.text)
把它放到上下文中,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}"
首先,這是行不通的。有人可以試圖證明我出錯的地方嗎?
然後,數據來自服務文件,服務如何更新值?什麼是正確的代碼?
在此先感謝。
你在哪裏創建你的ViewModel?你在哪裏設置你的視圖的DataContext?你如何將數據加載到模型中?當你說「那不起作用」時,你需要更精確地說明什麼是不工作的。 – 2016-08-15 05:24:20
如果你只是改變'TextProccessing'類的'OcrText'屬性,那麼XAML將不會注意到。在你的代碼中,只要數據發生變化,你就必須設置類的新實例。 –