2016-08-15 89 views
0

好吧,在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}" 

首先,這是行不通的。有人可以試圖證明我出錯的地方嗎?

然後,數據來自服務文件,服務如何更新值?什麼是正確的代碼?

在此先感謝。

+0

你在哪裏創建你的ViewModel?你在哪裏設置你的視圖的DataContext?你如何將數據加載到模型中?當你說「那不起作用」時,你需要更精確地說明什麼是不工作的。 – 2016-08-15 05:24:20

+0

如果你只是改變'TextProccessing'類的'OcrText'屬性,那麼XAML將不會注意到。在你的代碼中,只要數據發生變化,你就必須設置類的新實例。 –

回答

1

事實上,一旦我理解了,這很容易。這是更新文本框所需的代碼。文字

在型號:

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任何國防部:

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

下面的代碼是從code.msdn(How to achieve MVVM design patterns in UWP)引用,這將有利於你:

檢查你的代碼一步一步來。

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.Initialize你VIEWMODE在你頁面,並設置的DataContext作爲視圖模式,就像這樣:

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

3.In您的XAML,從VIEWMODE數據綁定,就像這樣:

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

謝謝你放置鏈接和解釋:) –

+0

@FranckE所以這應該是一個可以接受的答案,它解決了你的問題:) –

+0

你是對的,標記爲解決方案:) –

1

您的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名稱並不真實反映該屬性在做什麼,因爲它看起來不像視圖模型。考慮重命名它。

+0

這並不重要,該屬性是反正讀如果datacontext分配給新創建的視圖模型。他的問題是TextProcessing沒有實現INotifyPropertyChanged和TextProcessing.OcrText應該引發PropertyChanged。 – Haukinger

+0

謝謝你的回覆。我的資產更改使用回覆1進行更新,並感謝@Haukinger提及該模型應實施PropertyChanged。 –