{"id":472,"date":"2017-01-20T09:06:26","date_gmt":"2017-01-20T09:06:26","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=472"},"modified":"2017-01-20T09:06:26","modified_gmt":"2017-01-20T09:06:26","slug":"trigger-a-page-method-from-other-threads-using-databinding","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/windows-and-pages\/databinding\/trigger-a-page-method-from-other-threads-using-databinding","title":{"rendered":"Trigger A Page Method From Other Threads Using DataBinding"},"content":{"rendered":"<p>\n\tThis approach uses a hidden text box you can add to pages you want to be able to trigger methods on from another backgroudn thread in your application. &nbsp;The special class is created globally in your app and then its value is&nbsp;binded to the hidden text box. &nbsp;You can then change the value and the page using the bound text box will get a TextChanged method call in response. Its a hack, but quite a nice one!\n<\/p>\n<p>\n\t<em>IMPORTANT NOTE &#8211; This can cause background processes to stall a bit. &nbsp;We&#039;ve found on a Raspberry Pi 3 using this approach works fine in terms of updating the UI with something, but our SPI port comms would stop for a bit of time and suspect it may be the await call to the UI thread&#8230;<\/em>\n<\/p>\n<h5>\n\tDefine the special class somewhere<br \/>\n<\/h5>\n<pre>\n<code>\n\t\/\/**********************************************************************************************************\n\t\/\/**********************************************************************************************************\n\t\/\/********** OUR SPECIAL DATABINDING CLASS TO ALLOW US TO TRIGGER PAGE METHODS FROM OTHER THREADS **********\n\t\/\/**********************************************************************************************************\n\t\/\/**********************************************************************************************************\n\tpublic class PageUpdateDataBinder : System.ComponentModel.INotifyPropertyChanged\n\t{\n\t\t\/\/ Properties\n\t\tprivate string _NotifyValue;\n \n\t\tpublic string NotifyValue\n\t\t{\n\t\t\tget { return _NotifyValue; }\n \n\t\t\tset { this.SetProperty(ref this._NotifyValue, value); }\n\t\t}\n  \n\t\t\/\/Property Change Logic\n\t\tpublic event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;\n \n\t\tprotected bool SetProperty&lt;T&gt;(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)\n\t\t{\n\t\t\tif (object.Equals(storage, value)) return false;\n\t\t\tstorage = value;\n\t\t\tthis.OnPropertyChaned(propertyName);\n\t\t\treturn true;\n\t\t}\n \n\t\tprivate void OnPropertyChaned(string propertyName)\n\t\t{\n\t\t\tvar eventHandler = this.PropertyChanged;\n\t\t\tif (eventHandler != null)\n\t\t\t\teventHandler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));\n\t\t}\n\t}\n<\/code><\/pre>\n<h5>\n\tIn your apps global class<br \/>\n<\/h5>\n<pre>\n<code>\n\tpublic static PageUpdateDataBinder PageUpdateDataBinder1&nbsp;= null;\n<\/code><\/pre>\n<h5>\n\tIn Your Page .xaml file<br \/>\n<\/h5>\n<pre>\n<code>\n\t&lt;!-- Our databinding so we can be updated from other background processes --&gt;\n\t&lt;!-- N.B. you can&#039;t use Visibility=&quot;Collapsed&quot; as this stops the TextChanged method from being fired, so you&#039;ll need to hide this TextBox behind something... --&gt;\n\t&lt;TextBox  x:Name=&quot;txtOurBackgroundDataBind&quot; Text=&quot;{Binding NotifyValue,Mode=TwoWay}&quot; TextChanged=&quot;txtOurBackgroundDataBind_TextChanged&quot; &gt;&lt;\/TextBox&gt;\n<\/code><\/pre>\n<h5>\n\tIn your Page .xaml.cs file<br \/>\n<\/h5>\n<p>\n\tIn&nbsp;Page_Loading() or&nbsp;Page_Loaded():\n<\/p>\n<pre>\n<code>\n\t\/\/----- SETUP OUR DATABAIND SO OTHER BACKGROUND PROCESSES CAN UPDATE US -----\n\tApMain.PageUpdateDataBinder1 = new PageUpdateDataBinder {NotifyValue = &quot;&quot;};\n \tthis.DataContext = ApMain.PageUpdateDataBinder1;\n<\/code><\/pre>\n<p>\n\tMethod that will get called when the text box value is changed\n<\/p>\n<pre>\n<code>\n\t\/\/*************************************************************************************\n\t\/\/*************************************************************************************\n\t\/\/********** UPDATE FROM SOME OTHER BACKGROUND PROCESSES USING OUR DATABAIND **********\n\t\/\/*************************************************************************************\n\t\/\/*************************************************************************************\n\tprivate void txtOurBackgroundDataBind_TextChanged(object sender, TextChangedEventArgs e)\n\t{\n\t\tif (ApMain.PageUpdateDataBinder1.NotifyValue != &quot;&quot;)\t\t\t\/\/We will be called again when we clear the value!\n\t\t{\n\t\t\t\/\/Do something\n\t\t\ttxtMyTextBox.Text = &quot;UPDATE RECEVIED! &quot; + ApMain.PageUpdateDataBinder1.NotifyValue;\n\t\t}\n\t\tApMain.PageUpdateDataBinder1.NotifyValue = &quot;&quot;;\n\t}\n<\/code><\/pre>\n<h5>\n\tIn your other thread that wants to update the page<br \/>\n<\/h5>\n<pre>\n<code>\n\t\/\/********************************************************\n\t\/\/********************************************************\n\t\/\/********** UPDATE PAGE WE HAVE A DATABIND TO ***********\n\t\/\/********************************************************\n\t\/\/********************************************************\n\tprivate async void SendPageDataBindUpdate(string NotifyWithValue)\n\t{\n\t\tawait Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt;\n\t\t{\n\t\t\tApMain.PageUpdateDataBinder1.NotifyValue = NotifyWithValue; \n\t\t});\n\n\t}\n<\/code><\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This approach uses a hidden text box you can add to pages you want to be able to trigger methods on from another backgroudn thread in your application. &nbsp;The special class is created globally in your app and then its value is&nbsp;binded to the hidden text box. &nbsp;You can then change the value and the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[90],"tags":[],"class_list":["post-472","post","type-post","status-publish","format-standard","hentry","category-databinding"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/472\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}