{"id":58679,"date":"2023-12-25T10:21:41","date_gmt":"2023-12-25T09:21:41","guid":{"rendered":"https:\/\/ar3dp.de\/docs\/programmieren\/xamarin-forms-basis-applikation-app-template\/dipatching-uithread\/"},"modified":"2023-12-25T10:21:41","modified_gmt":"2023-12-25T09:21:41","slug":"dipatching-uithread","status":"publish","type":"docs","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/xamarin-forms-basis-applikation-app-template\/dipatching-uithread\/","title":{"rendered":"Dipatching &#038; UIThread"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Das App-Template verwendet den aktuellen Dispatcher von der Applikation <code>Application.Current.Dispatcher<\/code>, um zum Beispiel den aktuellen Dispatcher in das <strong>ViewModel<\/strong> zu injizieren. Somit steht immer der richtige Dispatcher zur Verf\u00fcgung und Code, welcher auf dem <strong>UIThread<\/strong> ausgef\u00fchrt werden muss, kann einfach \u00fcber unseren <strong>DispatchManager<\/strong> ausgef\u00fchrt werden. Das passiert im <code>BaseViewModel<\/code> und alle weiteren <code>ViewModels<\/code>, welche von dieser Klasse erben, muss der Dispatcher wie unten zugewiesen werden.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Constructor (ctor)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Damit eine View bzw. ein ViewModel auch den Dispatcher empfangen kann, muss der Ctor wie folgt angepasst werden (sofern Ihr eine neue View bzw. ein neues ViewModel anlegt).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">ViewModel<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Das neue <strong>ViewModel<\/strong> sollte immer vom <span class=\"font-weight-semi-bold\">BaseViewModel <\/span>or <strong>AppViewModel<\/strong> erben. Damit erh\u00e4lt es Zugriff auf alle gemeinsam genutzten Funktionen und Methoden. Der Ctor kann als Parameter einen <code>IDispatcher<\/code> annehmen, und diesen auch an die Basis-Klasse weitergeben. Andernfalls wird einfach der aktuelle <code>Dispatcher<\/code> der Applikation gesetzt.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">public partial class BaseViewModel\n{\n    #region Ctor\n    public BaseViewModel()\n    {\n        Dispatcher = Application.Current.Dispatcher;\n    }\n  \n    public BaseViewModel(IDispatcher dispatcher)\n    {\n        Dispatcher = dispatcher;\n    }\n    #endregion\n\n    \/\/ Some code...\n}\n\n\/\/ Custom ViewModel \npublic partial class DashboardPageViewModel : BaseViewModel\n{\n    #region Ctor\n    public DashboardPageViewModel() : base(dispatcher)\n    {\n        Dispatcher = Application.Current.Dispatcher;\n    }\n    #endregion\n\n    \/\/ Some code...\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Views<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In der View muss der Ctor dann das gew\u00fcnschte ViewModel als Parameter \u00fcbergeben werden.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">public partial class DashboardPage : ContentPage\n{\n    public DashboardPage() \n\t{         \n    \tInitializeComponent();\n    \tvar viewModel = new DashboardPageViewModel();\n    \tBindingContext = viewModel;\n\t}\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">DispatchManager<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Der <strong>DispatchManager<\/strong> ist eine Helper-Klasse, welche es Ihnen einfach erm\u00f6glicht Code \u00fcber den <strong>Dispatcher<\/strong> auszuf\u00fchren. Durch das vorgeschaltete <strong>trycatch<\/strong> werden auch direkt Fehler abgefangen und protokoliert (<strong>EventManger<\/strong>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Einfaches Beispiel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hier wird die Eigenschaft &#8220;IsRefreshing&#8221; Thread-Safe auf &#8220;false&#8221; gesetzt.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">DispatchManager.Dispatch(Dispatcher, () =&gt; IsRefreshing = false);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Da es sich bei dieser Eigenschaft um ein Element handelt, welches ein Binding auf ein UI-Element hat, muss dieses zwangsweise am <span class=\"font-weight-semi-bold\">MainThread<\/span> der Applikation ausgef\u00fchrt werden. Andernfalls kann es hier zu einer Ausnahme bzw. einen Crash kommen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CodeSnippet dispatchen<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Um einen ganzen Codeblock zu dispatchen, kann die Funktion wie folgt aussehen.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">DispatchManager.Dispatch(Dispatcher, () =&gt;\n{\n    CurrentJobInfo = DemoManager.JobInfo;\n    PrintersState = DemoManager.PrinterState.Printer;\n    ServerSettings = DemoManager.ServerSettings;\n    Files = DemoManager.Files;\n});<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Dispatching abwarten<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Manchmal kann es n\u00f6tig sein, dass <strong>Dispatching<\/strong> abzuwarten (await). Dazu muss die <strong>DispatchAsync()<\/strong> Methode verwendet werden.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">await DispatchManager.DispatchAsync(Dispatcher, UpdateFromInstanceData);\n\/\/ Or\nawait DispatchManager.DispatchAsync(Dispatcher, () =&gt;\n{\n    IsRefreshing = true;\n    IsReachable = false;\n\n    SelectedServer = Servers.FirstOrDefault(server =&gt; server.Id == LastServerId);\n    SelectedServerAddress = SelectedServer?.FullWebAddress;\n});\nawait CheckOnlineStateSelectedServer();\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Das App-Template verwendet den aktuellen Dispatcher von der Applikation Application.Current.Dispatcher, um zum Beispiel den aktuellen Dispatcher in das ViewModel zu injizieren. Somit steht immer der richtige Dispatcher zur Verf\u00fcgung und Code, welcher auf dem UIThread ausgef\u00fchrt werden muss, kann einfach \u00fcber unseren DispatchManager ausgef\u00fchrt werden. Das passiert im BaseViewModel und alle weiteren ViewModels, welche von&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"parent":58608,"menu_order":3,"comment_status":"open","ping_status":"closed","template":"","meta":{"_featured":false,"_is_vendor_doc":"0","footnotes":""},"doc_tag":[],"class_list":["post-58679","docs","type-docs","status-publish","hentry"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/comments?post=58679"}],"version-history":[{"count":1,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58679\/revisions"}],"predecessor-version":[{"id":58682,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58679\/revisions\/58682"}],"up":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58608"}],"next":[{"title":"Firebase","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/xamarin-forms-basis-applikation-app-template\/firebase\/","href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58684"}],"prev":[{"title":"Navigation","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/xamarin-forms-basis-applikation-app-template\/navigation\/","href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58673"}],"wp:attachment":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/media?parent=58679"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/doc_tag?post=58679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}